首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker -连接MySQL实例失败。如何解决?

Docker -连接MySQL实例失败。如何解决?
EN

Stack Overflow用户
提问于 2019-04-19 01:51:34
回答 1查看 4.2K关注 0票数 0

在MAC OS中运行。

根据这些Basic Steps for MySQL Server Deployment with Docker,我试图通过php storm数据库与容器建立连接。

我在图片中看到了下面的错误:

我可以通过终端访问它:

代码语言:javascript
复制
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也不能连接到数据库服务器。

代码语言:javascript
复制
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=root
DB_PASSWORD=dockerLocal

容器信息如下:

代码语言:javascript
复制
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连接到容器。

代码语言:javascript
复制
SELECT host, user FROM mysql.user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+

这是错误:

代码语言:javascript
复制
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中。

代码语言:javascript
复制
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.

简而言之,如果我这样做,它就会起作用:

代码语言:javascript
复制
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          |
+-----------+---------------+
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-19 02:00:58

您需要将容器端口绑定到主机上。

而不是做

代码语言:javascript
复制
docker run --name=mysql1 -d mysql/mysql-server:tag

代码语言:javascript
复制
docker run --name=mysql1 -p 3306:3306 -d mysql/mysql-server:tag 

这将容器的端口3306绑定到主机的127.0.0.1上的端口3306。

然后,您将能够通过localhost:3306进行连接

参考:Docker Run -- Publish or expose port (-p, --expose)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55751353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档