环境
下载Mysql源
[root@mysql01 ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
安装Mysql源
[root@mysql01 ~]# sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
检查是否安装成功
[root@mysql01 ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 212
mysql-tools-community/x86_64 MySQL Tools Community 132
mysql80-community/x86_64 MySQL 8.0 Community Server 283
选择Mysql版本
使用MySQL Yum Repository安装MySQL,默认会选择当前最新的稳定版本,例如通过上面的MySQL源进行安装的话,默安装会选择MySQL 8.0版本,如果就是想要安装该版本,可以直接跳过此步骤,如果不是,就需要“切换一下版本”:
查看查看当前MySQL Yum Repository中所有MySQL版本
(每个版本在不同的子仓库中)
[root@mysql01 ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community 禁用
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - So 禁用
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community 禁用
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - So 禁用
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community 禁用
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - So 禁用
mysql-connectors-community/x86_64 MySQL Connectors Community 启用: 212
mysql-connectors-community-source MySQL Connectors Community - Sou 禁用
mysql-tools-community/x86_64 MySQL Tools Community 启用: 132
mysql-tools-community-source MySQL Tools Community - Source 禁用
mysql-tools-preview/x86_64 MySQL Tools Preview 禁用
mysql-tools-preview-source MySQL Tools Preview - Source 禁用
mysql55-community/x86_64 MySQL 5.5 Community Server 禁用
mysql55-community-source MySQL 5.5 Community Server - Sou 禁用
mysql56-community/x86_64 MySQL 5.6 Community Server 禁用
mysql56-community-source MySQL 5.6 Community Server - Sou 禁用
mysql57-community/x86_64 MySQL 5.7 Community Server 禁用
mysql57-community-source MySQL 5.7 Community Server - Sou 禁用
mysql80-community/x86_64 MySQL 8.0 Community Server 启用: 283
mysql80-community-source MySQL 8.0 Community Server - Sou 禁用
切换版本
这里第一种可以通过yum-config-manager来修改,也可以通过手动修改配置文件来修改
通过yum-config-manager修改
安装yum-utils
因为yum-config-manager包含在yum-utils中。
[root@mysql01 ~]# yum -y install yum-utils
切换版本
比如这样是把mysql8.0版本关闭,把mysql5.7版本打开(但是这块我没操作,我还是安装的是8.0版本)
[root@mysql01 ~]# sudo yum-config-manager --disable mysql80-community
[root@mysql01 ~]# sudo yum-config-manager --enable mysql57-community
通过手动修改配置文件
[root@mysql01 ~]# vim /etc/yum.repos.d/mysql-community.repo
检查当前启用的MySQL仓库
如果同时启用了多个仓库,安装时会选择最新版本。
[root@mysql01 ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 212
mysql-tools-community/x86_64 MySQL Tools Community 132
mysql80-community/x86_64 MySQL 8.0 Community Server 283
安装Mysql
该命令会安装MySQL服务器 (mysql-community-server) 及其所需的依赖、相关组件,包括mysql-community-client、mysql-community-common、mysql-community-libs等
如果带宽不够,这个步骤时间会比较长,请耐心等待~
[root@mysql01 ~]# sudo yum install mysql-community-server
查看状态
[root@mysql01 ~]# sudo systemctl status mysqld.service
停止
[root@mysql01 ~]# sudo systemctl stop mysqld.service
重启
[root@mysql01 ~]# sudo systemctl restart mysqld.service
修改密码
查看初始密码
MySQL第一次启动后会创建超级管理员账号root@localhost
,初始密码存储在日志文件中:
[root@mysql01 ~]# sudo grep 'temporary password' /var/log/mysqld.log
2021-07-27T08:29:30.934454Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: &fUPlSRwp2hj
修改默认密码
先连接,密码就是默认密码
[root@mysql01 ~]# mysql -uroot -p
我们测试的话,使用的是简单密码,正式的话需要设置复杂密码,就不存在这个错误,直接修改即可。
mysql> use mysql;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Anin@123';
如果修改的是简单密码,会提示如下图
mysql> use mysql;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
因为MySQL默认的密码策略是要包含数字、字母及特殊字符。如果想要改简单密码需要修改一下密码校验策略。而且这里是5版本和8版本的配置名称是不一样的。
mysql> set global validate_password.policy=0;
mysql> set global validate_password.length=1;
修改远程访问
此时我们的mysql还是不能远程连接的,我们需要开启远程连接权限。
update user set host = '%' where user = 'root';
FLUSH PRIVILEGES;