解决Host 'X.X.X.X' is not allowed to connect to this MySQL server

在 CentOS7 上装了MySQL,安装成功后,修改完登录密码,然后用工具连接该服务器,报了一个错误。尝试用终端去连接 MySQL 数据库。同样报了一个错误。

➜  ~ mysql -h 192.168.20.163 -u root
ERROR 1130 (HY000): Host '192.168.118.118' is not allowed to connect to this MySQL server

这个说明了MySQL出于安全性考虑只允许本地进行命令访问。

那就需要登录到服务器上,连接到本地数据库。

执行以下命令。

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> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

mysql> select host,user from user;
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| %         | root      |
| localhost | mysql.sys |
+-----------+-----------+
2 rows in set (0.00 sec)

再次使用工具连接MySQL数据库,就可以了。

posted @ 2018-08-01 16:34  zacky31  阅读(353)  评论(0编辑  收藏  举报