使用Docker创建Mysql容器,连接到数据库报错

Host 'x.x.x.x' is not allowed to connect to this MySQL server

先登录到数据库

docker exec -it Mysql容器名 bash

在容器中连接到数据库

mysql -u 用户名 -p

输入密码,登上数据库,查看用户名和主机的对应表

SELECT host, user FROM mysql.user;

会显示如下的表

+------------+------------------+
| host       | user             |
+------------+------------------+
| %          | root             |
| 127.0.0.1  | root             |
| ::1        | root             |
| localhost  | mysql.sys        |
| localhost  | root             |
| localhost  | sonar            |
+------------+------------------+

以“root”用户为例,仅当对应的主机有“%”时才允许从任意IP登录。

注:从容器外连接数据库,用户名对应的主机必须有“%”。

Leave a Reply

Your email address will not be published. Required fields are marked *