CentOS 7 安装并初始化 MySQL

安装 MySQL

在 MySQL 官网下载 mysql-community-server-8.0.23-1.el7.x86_64.rpm 安装包,然后使用

1
rpm -ivh mysql-community-server-8.0.23-1.el7.x86_64.rpm

或者

1
yum install mysql-community-server-8.0.23-1.el7.x86_64.rpm

进行安装,安装的时候会提示缺少依赖,mysql 的依赖包需要我们手动安装,非 mysql 的依赖包可以使用 yum 命令自动安装。
mysql 大概需要安装以下包,安装顺序根据提示进行安装即可:

1
2
3
4
5
mysql-community-server-8.0.23-1.el7.x86_64.rpm
mysql-community-client-8.0.23-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.23-1.el7.x86_64.rpm
mysql-community-common-8.0.23-1.el7.x86_64.rpm
mysql-community-libs-8.0.23-1.el7.x86_64.rpm

我在安装 mysql-community-libs 的时候遇到了以下报错:

1
2
3
4
5
rpm -ivh mysql-community-libs-8.0.23-1.el7.x86_64.rpm 
警告:mysql-community-libs-8.0.23-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
mysql-community-common(x86-64) >= 8.0.11 被 mysql-community-libs-8.0.23-1.el7.x86_64 需要
mariadb-libs 被 mysql-community-libs-8.0.23-1.el7.x86_64 取代

这时我们手动去删除 mariadb-libs 即可:

1
yum erase mariadb-libs

初始化 MySQL

1
mysqld --initialize

权限设置

1
chown mysql:mysql -R /var/lib/mysql

启动 MySQL

1
systemctl start mysqld

查看 MySQL 运行状态

1
systemctl status mysqld

查找 root 用户的默认密码

1
grep "password" /var/log/mysqld.log

登录数据库

1
mysql -u root -p

修改密码

1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

查看数据库

1
mysql> SHOW DATABASES;