Linux 离线安装 mysql 5.7

mysql

  • 检查自带mariadb数据库 并删除
#查看mariadb
rpm -qa|grep mariadb

#删除mariadb
yum remove mariadb-libs
  • 把下载好的 mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz 放到 /opt 目录下

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RnJj9xRg-1646817124011)(src/image/mysql.png)]

  • 解压,移动并重命名
tar -zxvf /opt/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

mv /opt/mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql
  • 递归修改权限为 mysql组 mysql用户
useradd -g mysql mysql

chown mysql.mysql -R /usr/local/mysql
  • 在/usr/local/mysql/support-files/路径下创建 my.cnf
vim /usr/local/mysql/support-files/my_default.cnf
  • 粘贴复制以下内容
[mysqld]
#设置mysql的安装目录
basedir =/usr/local/mysql
#设置mysql数据库的数据存放目录
datadir = /usr/local/mysql/data
#设置端口
port = 3306

socket = /tmp/mysql.sock
#设置字符集
character-set-server=utf8
#日志存放目录
log-error = /usr/local/mysql/data/mysqld.log
pid-file = /usr/local/mysql/data/mysqld.pid
#允许时间类型的数据为零(去掉NO_ZERO_IN_DATE,NO_ZERO_DATE)
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  • 拷贝覆盖 /etc/my.cnf
cp /usr/local/mysql/support-files/my.cnf /etc/my.cnf
  • 在 /usr/local/mysql/ 目录下 初始化mysql
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
  • 初始化完成之后查看日志,获得初始密码
vim /usr/local/mysql/data/mysqld.log

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5HwdVzx4-1646817124012)(src/image/mysql密码.png)]

  • 启动服务 (/mysql/support-files 目录下 )
[root@seckill support-files]# ./mysql.server start

在这里插入图片描述

  • 登录服务(/mysql/bin 目录下)
[root@seckill bin]# ./mysql -uroot -p
  • 修改初始密码,添加远程访问权限,刷新退出
mysql>set password for root@localhost = password('123456');

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

mysql>FLUSH PRIVILEGES;
mysql>exit;
  • mysql启动项目加入系统服务
# 复制 mysql.server 至/etc/init.d/重命名为 mysqld
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# 添加系统服务(service)启动信息
chkconfig --add mysqld     # 服务名字为mysqld
  • 启动服务并查其状态
systemctl start mysqld
systemctl status mysqld