ERROR 1130 (HY000): Host 'x.x.x.x' is not allowed to connect to this MySQL server

MySQL 远程登录报错如下:

1
2
mysql -h x.x.x.x -u root -p
ERROR 1130 (HY000): Host 'x.x.x.x' is not allowed to connect to this MySQL server

原因是 MySQL 出于安全考虑,默认只允许本地登录数据库服务器。我们进行以下修改,重启 MySQL 即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
show databases;
use mysql;
select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+

update user set host='%' where user='root';
select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+------------------+

重启 MySQL。

1
systemctl restart mysqld