Docker - 无法连接到 MySQL 实例。如何解决?
Docker - Failed to Connect to MySQL instance. How to solve it?
运行 在 MAC OS.
按照这些 Basic Steps for MySQL Server Deployment with Docker,我正在尝试通过 php 风暴数据库与容器建立连接。
我收到下面图片中的错误:
我可以通过终端访问它:
docker exec -it 0ed bash
bash-4.2# mysql -uroot -pdockerLocal
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.7.25 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.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
mysql>
3306端口没有进程运行。netstat -vanp tcp | grep 3306
什么也没有显示。
我的 Laravel 也无法连接到数据库服务器。
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=root
DB_PASSWORD=dockerLocal
这是容器信息:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0edce223c684 mysql/mysql-server:5.7 "/entrypoint.sh mysq…" 34 minutes ago Up 34 minutes (healthy) 3306/tcp, 33060/tcp stupefied_johnson
如何测试此连接以及如何让 phpstorm 数据库连接正常工作?
更新
端口公开后(问题出在这里)我们无法使用 root@localhost
连接到容器。
SELECT host, user FROM mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
这是错误:
Connection to @0.0.0.0 failed.
[HY000][1130] null, message from server: "Host '172.17.0.1' is not allowed to connect to this MySQL server"
解决方法就在这里post。
Check if the database user exists and can connect
In MySQL, each database user is defined with IP address in it, so you can have for example a root user allowed to connect from localhost (127.0.0.1) but not from other IP addresses. With a container, you never access to the database from 127.0.0.1, it could explain the problem.
简而言之,如果我这样做就可以了:
CREATE USER 'jerry'@'%' IDENTIFIED BY 'jerrypassword';
SELECT host, user FROM mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | jerry |
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
您需要将您的容器端口绑定到您的主机。
而不是
docker run --name=mysql1 -d mysql/mysql-server:tag
做
docker run --name=mysql1 -p 3306:3306 -d mysql/mysql-server:tag
这会将容器的 3306 端口绑定到主机 127.0.0.1 上的 3306 端口。
然后您将能够通过 localhost:3306
进行连接
运行 在 MAC OS.
按照这些 Basic Steps for MySQL Server Deployment with Docker,我正在尝试通过 php 风暴数据库与容器建立连接。
我收到下面图片中的错误:
我可以通过终端访问它:
docker exec -it 0ed bash
bash-4.2# mysql -uroot -pdockerLocal
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.7.25 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.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
mysql>
3306端口没有进程运行。netstat -vanp tcp | grep 3306
什么也没有显示。
我的 Laravel 也无法连接到数据库服务器。
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=root
DB_PASSWORD=dockerLocal
这是容器信息:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0edce223c684 mysql/mysql-server:5.7 "/entrypoint.sh mysq…" 34 minutes ago Up 34 minutes (healthy) 3306/tcp, 33060/tcp stupefied_johnson
如何测试此连接以及如何让 phpstorm 数据库连接正常工作?
更新
端口公开后(问题出在这里)我们无法使用 root@localhost
连接到容器。
SELECT host, user FROM mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
这是错误:
Connection to @0.0.0.0 failed.
[HY000][1130] null, message from server: "Host '172.17.0.1' is not allowed to connect to this MySQL server"
解决方法就在这里post。
Check if the database user exists and can connect
In MySQL, each database user is defined with IP address in it, so you can have for example a root user allowed to connect from localhost (127.0.0.1) but not from other IP addresses. With a container, you never access to the database from 127.0.0.1, it could explain the problem.
简而言之,如果我这样做就可以了:
CREATE USER 'jerry'@'%' IDENTIFIED BY 'jerrypassword';
SELECT host, user FROM mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | jerry |
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
您需要将您的容器端口绑定到您的主机。
而不是
docker run --name=mysql1 -d mysql/mysql-server:tag
做
docker run --name=mysql1 -p 3306:3306 -d mysql/mysql-server:tag
这会将容器的 3306 端口绑定到主机 127.0.0.1 上的 3306 端口。
然后您将能够通过 localhost:3306