前言
最近在家里想搭建一个开发环境玩玩,没想到在虚拟机Centos7上使用yum安装mysql8.x竟然折腾了好久。
这次官网的文档都没有拯救我,踩了好多坑,所以把安装过程整理一下,避免以后重复踩坑。
一、官方文档
官网YUM安装:https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html
二、安装步骤
1.安装MySQL Yum Repository
下载地址:https://dev.mysql.com/downloads/repo/yum/
注意:
mysql80-community-release-el7-6.noarch.rpm中的el7表示是操作系统为Centos7。
将下载的rpm文件上传到虚拟机上的指定目录。然后执行yum源仓库的安装命令:
sudo yum install mysql80-community-release-el7-6.noarch.rpm
官网默认的yum源由于是海外的,在国内网络下载安装时限速严重,这里推荐改为OpenTuna源,速度会快很多。
cd /etc/yum.repos.d/ mv mysql-community.repo mysql-community.repo.bak vi mysql-community.repo
# Enable to use MySQL 5.5 [mysql55-community] name=MySQL 5.5 Community Server baseurl=https://opentuna.cn/mysql/yum/mysql-5.5-community-el7-$basearch/ enabled=0 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=https://opentuna.cn/mysql/yum/mysql-5.6-community-el7-$basearch/ enabled=0 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql # Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server baseurl=https://opentuna.cn/mysql/yum/mysql-5.7-community-el7-$basearch/ enabled=0 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql [mysql80-community] name=MySQL 8.0 Community Server baseurl=https://opentuna.cn/mysql/yum/mysql-8.0-community-el7-$basearch/ enabled=1 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql [mysql-connectors-community] name=MySQL Connectors Community baseurl=https://opentuna.cn/mysql/yum/mysql-connectors-community-el7-$basearch/ enabled=1 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql [mysql-tools-community] name=MySQL Tools Community baseurl=https://opentuna.cn/mysql/yum/mysql-tools-community-el7-$basearch/ enabled=1 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql [mysql-tools-preview] name=MySQL Tools Preview baseurl=https://opentuna.cn/mysql/yum/mysql-tools-preview/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql [mysql-cluster-7.5-community] name=MySQL Cluster 7.5 Community baseurl=https://opentuna.cn/mysql/yum/mysql-cluster-7.5-community-el7-$basearch/ enabled=0 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql [mysql-cluster-7.6-community] name=MySQL Cluster 7.6 Community baseurl=https://opentuna.cn/mysql/yum/mysql-cluster-7.6-community-el7-$basearch/ enabled=0 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql [mysql-cluster-8.0-community] name=MySQL Cluster 8.0 Community baseurl=https://opentuna.cn/mysql/yum/mysql-cluster-8.0-community-el7-$basearch/ enabled=0 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
清空并更新缓存:
yum clean all && yum makecache
2.选择要安装的版本
查看所有mysql的yum源仓库:
yum repolist all | grep mysql
禁用和启用可以安装的mysql版本:
$> sudo yum-config-manager --disable mysql57-community $> sudo yum-config-manager --enable mysql80-community
查看可以安装的mysql版本:
yum repolist enabled | grep mysql
3.检查rpm的签名
如果直接进行安装,出现提示获取GPG密钥失败。
sudo yum install mysql-community-server
检查签名:
[root@localhost ~]# cd /root [root@localhost ~]# ls anaconda-ks.cfg mysql80-community-release-el7-6.noarch.rpm [root@localhost ~]# rpm --checksig mysql80-community-release-el7-6.noarch.rpm mysql80-community-release-el7-6.noarch.rpm: RSA sha1 (MD5) PGP md5 不正确
导入密钥:
[root@localhost ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 [root@localhost ~]# rpm --checksig mysql80-community-release-el7-6.noarch.rpm mysql80-community-release-el7-6.noarch.rpm: rsa sha1 (md5) pgp md5 确定
4.安装Mysql
sudo yum -y install mysql-community-server
可以发现下载速度快了很多,并且安装过程中就没有出现签名、秘钥的错误提示了。
5.启动mysql服务
//启动服务 $> systemctl start mysqld //设置开机自启动 $>systemctl enable nginx //查看服务状态 $> systemctl status mysqld
6.修改初始密码
查看初始密码:sudo grep 'temporary password' /var/log/mysqld.log
[root@localhost yum.repos.d]# sudo grep 'temporary password' /var/log/mysqld.log 2022-07-05T15:21:05.759128Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +9ee!Z?esjfe
修改密码:
$> mysql -uroot -p mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'laoWAN@123456';
默认情况下,mysql对密码的安全级别限制较高,所以密码需要设置的复杂点才能修改成功。
如果只是开发环境只用,可以通过降低密码校验级别然后将密码修改的简单点。
查看密码校验级别:
mysql> SHOW VARIABLES LIKE 'validate_password.%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | MEDIUM | | validate_password.special_char_count | 1 | +--------------------------------------+--------+
修改密码校验级别:
//设置密码校验级别 mysql> SET GLOBAL validate_password.policy=LOW; Query OK, 0 rows affected (0.00 sec) //设置密码最短长度 mysql> SET GLOBAL validate_password.length=6; Query OK, 0 rows affected (0.00 sec)
查看是否配置成功:
mysql> SHOW VARIABLES LIKE 'validate_password.%'; +--------------------------------------+-------+ | Variable_name | Value | +--------------------------------------+-------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 6 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | LOW | | validate_password.special_char_count | 1 | +--------------------------------------+-------+ 7 rows in set (0.01 sec)
修改root用户密码:
$> mysql -uroot -p mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
7.创建用户并赋权
CREATE USER 'root'@'%' IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'root'@'%';
其他命令:
//删除用户 mysql> drop user root@'%'; Query OK, 0 rows affected (0.00 sec) //刷新权限 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) //查看用户 use mysql; select user,host from user;
8.关闭防火墙
默认情况下,Centos7会开启firewalld防火墙,屏蔽所有非22端口的网络请求,导致通过mysql客户端连接mysql服务失败。
由于是开发环境,直接关闭firewalld防火墙。
//停止firewall systemctl stop firewalld //禁止firewall开机启动 systemctl disable firewalld
总结
比较关键的几点:
1、不要使用官网默认的yum源,下载速度超级慢,建议切换成OpenTuna源。
2、需要给rpm文件导入GPG密钥,不然会出现获取GPG密钥失败的异常。
3、通过修改默认的密码校验级别,成功设置简单的密码。
4、通过Mysql客户端工具连接失败时,检查Mysql服务是否启动,防火墙是否关闭。