Centos安装MySQL不同版本

红帽系源是默认用mariadb的,而且mariadb也很优秀,但是我们开发是基于MySQL5.7上做的开发,基本10.2起,应该对MySQL5.7所有的特性是完整支持的.

但是为了稳妥,还是决定给客户安装一样的版本.

在官网找文档的时候,发现藏得有点深,所以还是打算记录下.

安装

完整的命令大致如下

1
2
3
4
shell> yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
shell> yum repolist all | grep mysql
shell> sudo yum-config-manager --disable mysql80-community
shell> sudo yum-config-manager --enable mysql57-community

当看到mysql源是57(自己想要的版本的时候就可以直接安装了)

1
2
3
4
5
6
shell> yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 118
mysql-tools-community/x86_64 MySQL Tools Community 95
mysql57-community/x86_64 MySQL 5.7 Community Server 364

shell> yum install mysql-community-server

初始化

MySQL5.6依然可以使用 mysql_secure_installation 脚本初始化,5.7以上则不适用了

我数据目录存放在数据盘挂载的目录,所以启动之前还修改了下配置 vim /etc/my.cnf

1
2
3
4
5
[mysqld]
# 主要修改一下两项,数据和日志存放路径
datadir=/data/mysql/data
log-error=/data/mysql/logs/mysqld.log

如果是自定义的数据目录和日志目录.要注意日志目录要先创建.数据目录要让MySQL自己创建.不然会遇到已经存在数据目录的错误.如下:

1
2
3
4
5
6
7
8
Oct 09 10:40:05 rongyitong systemd[1]: Starting MySQL Server...
Oct 09 10:40:05 rongyitong mysqld_pre_systemd[22988]: 2019-10-09T02:40:05.292304Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp serve...ore details).
Oct 09 10:40:05 rongyitong mysqld_pre_systemd[22988]: 2019-10-09T02:40:05.294633Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
Oct 09 10:40:05 rongyitong mysqld_pre_systemd[22988]: 2019-10-09T02:40:05.294665Z 0 [ERROR] Aborting
Oct 09 10:40:05 rongyitong mysqld[23013]: Initialization of mysqld failed: 0
Oct 09 10:40:05 rongyitong systemd[1]: mysqld.service: control process exited, code=exited status=1
Hint: Some lines were ellipsized, use -l to show in full.

启动服务: systemctl start mysqld.service
自启动: systemctl enable mysqld.service

启动完成后,可以在MySQL错误日志中找到初始化的密码,如:

1
cat /data/mysql/logs/mysqld.log | grep 'temporary password'

修改密码:

1
2
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

完成

tips

如果遇到 -bash: yum-config-manager: command not found

安装 yum -y install yum-utils

参考资料

关注公众号 尹安灿