Host ‘x.x.x.x.’ is not allowed to connect to this MySQL server.

在局域网中使用Docker远程连接数据库报错:
Host ‘x.x.x.x.’ is not allowed to connect to this MySQL server.在这里插入图片描述
原因:未给root用户赋予远程登录权限

解决办法:
1、查看当前运行的docker容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                    PORTS                      
                         NAMES
4b789c107052        mysql/mysql-server   "/entrypoint.sh mysq…"   29 minutes ago      Up 29 minutes (healthy)   0.0.0.0:32769->3306/tcp, 0.
0.0.0:32768->33060/tcp   mysql03
010a32224776        mysql/mysql-server   "/entrypoint.sh mysq…"   40 minutes ago      Up 40 minutes (healthy)   0.0.0.0:3306->3306/tcp, 330
60/tcp                   mysql02

2、在mysql02容器中以root用户登录MySQL

[root@localhost ~]# docker exec -it mysql02 mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 92
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

3、切换到mysql数据库

mysql> show databses;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right synt
ax to use near 'databses' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.16 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

4、查看用户表,发现root用户的访问权限是localhost,需要修改host为%

mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | healthchecker    |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

5、将host修改为%,使root用户可以在任意主机登录

mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.08 sec)
Rows matched: 1  Changed: 1  Warnings: 0

6、刷新数据库

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

7、验证,已经修改好了

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

之后就可以进行远程登录了。

你可能感兴趣的:(Debug,MySQL)