CentOS 8 安装 MySQL 5.7 或 MySQL 8

CentOS 8 默认是可以安装 MySQL 8。



https://dev.mysql.com/downloads/repo/yum/

https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/


第一步,添加源文件


手动添加文件 /etc/yum.repos.d/mysql-community.repo,然后加入下面内容到文件里。


安装 MySQL 8.0

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


安装 MySQL5.7

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


检查源

yum repolist enabled | grep mysql
dnf repolist enabled | grep mysql


第二步,安装 MySQL


关闭默认 MySQL 模块

sudo yum module disable mysql
sudo dnf module disable mysql


安装

sudo dnf install mysql-community-server
sudo dnf install mysql-community-server --nobest


如果提示

Curl error (37): Couldn't read a file:// file for file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql]

解决方法

第一种方案,设置gpgcheck为0,就是不需要检查gpgkey。

# enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

第二种方案 下载gpgkey

//下载RPM-GPG-KEY-mysql
wget http://repo.mysql.com/RPM-GPG-KEY-mysql -O /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


第三步,修改 root 密码

官方文档提示 MySQL 5.7 和更高版本不要使用 mysql_secure_installation

Do not run mysql_secure_installation after an installation of MySQL 5.7 or higher, as the function of the program has already been performed by the Yum repository installation.


查看临时密码

grep 'temporary password' /var/log/mysqld.log 


使用临时密码登录并修改密码

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



第四步,修改 MySQL 编码


$ mysql> show variables like 'character%';


修改 /etc/my.cnf 在 [mysqld]  下,加入 character_set_server=utf8,重启 MySQL 生效。