安装MySQL的yum仓库
1.下载MySQL5.7的rpm仓库
[root@linux zhengbh]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2.安装MySQL5.7的yum 仓库
[root@linux zhengbh]# yum -y install mysql57-community-release-el7-10.noarch.rpm
3.查看是否安装成功
[root@linux zhengbh]# yum repolist all | grep "mysql*"
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 212
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 132
mysql-tools-community-source MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 524
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disabled
可以看到,这里已经有enabled状态的mysql57仓库,8.0版本默认没有开启。
安装MySQL
[root@linux zhengbh]# yum -y install mysql-community-server
这里可能需要等待2分钟时间。
启动并配置MySQL
1.启动MySQL
[root@linux zhengbh]# systemctl start mysqld.service
2.查看是否启动成功
[root@linux zhengbh]# ps -aux | grep mysql
mysql 18107 2.3 17.2 1121344 173192 ? Sl 00:41 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root 18136 0.0 0.0 112644 948 pts/1 R+ 00:42 0:00 grep --color=auto mysql
3.配置root账户的密码
默认安装后,root账户密码是随机生成的,我们需要改这个密码。
- 查看随机密码
[root@linux zhengbh]# grep "password" /var/log/mysqld.log
2021-09-12T04:41:57.170414Z 1 [Note] A temporary password is generated for root@localhost: d2t5AFX_jNv?
可以看到密码是 d2t5AFX_jNv?
2.用随机密码登录数据库
[root@linux zhengbh]# mysql -uroot -pd2t5AFX_jNv?
3.修改root账户密码
在此之前,我们需要改一下mysql对于密码安全性的策略。不然mysql不会允许我们自己设置的密码过于简单。
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
好了,可以设置我们的密码了。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
4.设置root账户远程连接
第一次登录可能报如下错误。
mysql默认root账户只能本机连接使用。这里我们需要设置它远程连接的权限。
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
刷新权限,立即生效。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
再次登录,就可以连上了。
删除已安装的MySQL yum仓库
这里我看到有博客说,需要删除最开始安装的yum仓库,这里其实删不删不重要。除非你以后刻意去使用yum命令更新它。
[root@linux zhengbh]# yum -y remove mysql57-community-release-el7-10.noarch