欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS7 yum方法安装MySQL5.7并开启root用户远程连接

程序员文章站 2022-03-12 23:19:54
...

1 下载并安装mysql

1.1 下载并安装MySQL官方的 Yum Repository

[aaa@qq.com ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

1.2 下载并安装MySQL的rpm

[aaa@qq.com ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm

1.3 安装MySQL服务器

[aaa@qq.com ~]# yum -y install mysql-community-server

出现下面的提示,说明安装成功:
CentOS7 yum方法安装MySQL5.7并开启root用户远程连接

2 MySQL数据库设置

2.1 启动mysql

[aaa@qq.com src]# systemctl start mysqld  
[aaa@qq.com src]# systemctl status mysqld

2.2 设置开机启动mysql

[aaa@qq.com src]# systemctl enable mysqld
[aaa@qq.com src]# systemctl daemon-reload

2.3 获得mysql初始密码

[aaa@qq.com src]# grep 'temporary password' /var/log/mysqld.log
2020-04-17T10:59:54.668094Z 1 [Note] A temporary password is generated for aaa@qq.com: e;omvyN53CS*

2.4 登录mysql

[aaa@qq.com src]# mysql -uroot -p

用刚才获得的初始密码登录成功后,不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库,而5.7版本的mysql采用了密码机制,默认为medium,要求密码有大写字母及特殊字符。MySQL完整的初始密码规则可以通过如下命令查看:

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| 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      |
+--------------------------------------+--------+

2.5 修改mysql的root密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mypass123!';

到此数据库的密码设置就完成了。
但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:

[aaa@qq.com ~]# yum -y remove mysql57-community-release-el7-10.noarch

3 开启root用户远程连接

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select `user`,authentication_string,`Host` from `user`;
+---------------+-------------------------------------------+-----------+
| user          | authentication_string                     | Host      |
+---------------+-------------------------------------------+-----------+
| root          | *ED6727A34C50DA0C532932E364204F55B6557EAC | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
+---------------+-------------------------------------------+-----------+

可以看到root目前只支持localhost本机连接。
再执行如下语句:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Mypass123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select `user`,authentication_string,`Host` from `user`;
+---------------+-------------------------------------------+-----------+
| user          | authentication_string                     | Host      |
+---------------+-------------------------------------------+-----------+
| root          | *ED6727A34C50DA0C532932E364204F55B6557EAC | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| root          | *ED6727A34C50DA0C532932E364204F55B6557EAC | %         |
+---------------+-------------------------------------------+-----------+

我们看到root目前已支持任意机器连接,最后还要查看防火墙等问题。

相关标签: mysql