简体   繁体   English

无法连接到在 docker 中运行的 MySQL

[英]Can't connect to MySQL running in docker

I'm trying to build docker image with some legacy LAMP development stack for development purposes.我正在尝试使用一些旧的 LAMP 开发堆栈构建 docker 映像以用于开发目的。 Basically I'm taking ubuntu image and installing bitnami LAMP stack.基本上我正在使用 ubuntu 映像并安装 bitnami LAMP 堆栈。 Here's Dockerfile I have so far:这是我到目前为止的 Dockerfile:

FROM ubuntu

EXPOSE 80 443 3306

WORKDIR /opt

COPY setup.sh .
RUN chmod +x setup.sh
RUN ./setup.sh      # this bash script downloads and runs installer

CMD /opt/bitnami/ctlscript.sh start && tail -f /opt/bitnami/apache2/logs/access_log

Then I'm running that container like this:然后我像这样运行那个容器:

docker run --name dev -d -p 8080:80 -p 3307:3306 -v "C:\\dev\\project:/opt/bitnami/apache2/htdocs" aburov/lamp5.6

All works as expected (app from c:\\dev\\project is accessible throug localhost:8080 and it can access database) except the fact tha I can't connect to MySQL from the host using mapped 3307 port.一切都按预期工作(来自c:\\dev\\project应用程序可以通过localhost:8080访问并且它可以访问数据库),除了我无法使用映射的 3307 端口从主机连接到 MySQL 的事实。

I've tried connect from MySQL Workbench and JetBrains' DataGrip both failing with similar error:我试过从 MySQL Workbench 和 JetBrains 的 DataGrip 连接都失败了,并出现了类似的错误:

Communications link failure with primary.与主要的通信链路故障。 No active connection found for master.找不到主节点的活动连接。 java.io.EOFException: unexpected end of stream, read 0 bytes from 4 (socket was closed by server). java.io.EOFException:流意外结束,从 4 读取 0 个字节(套接字已被服务器关闭)。

I've tried:我试过了:

  1. Using map to another host's port (3306, 3308, 10123) assuming there some conflicts;假设存在一些冲突,使用映射到另一台主机的端口(3306、3308、10123);
  2. Using different MySQL drivers.使用不同的 MySQL 驱动程序。

MySQL version is 5.6. MySQL 版本是 5.6。

What I'm missing?我缺少什么? Thank you in advance!先感谢您!

After checking if the ports are mapped correctky and also verified da alocal mysqlclient can connect to the server, there is another possibiliry.在检查端口映射是否正确并验证本地 mysqlclient 可以连接到服务器后,还有另一种可能性。

MySQL is in default configuration as security measure , only accepting access from the localhost. MySQL 处于默认配置作为安全措施,只接受来自本地主机的访问。

So you have to control and change the following parameter in the my.cnf所以你必须控制和更改my.cnf的以下参数

[mysqld] 
bind-address=0.0.0.0

That would allow access from every ip这将允许从每个ip 访问

Additionally by default root has privileges for localhost only so it's not allowed to connect with that user from another hosts.此外,默认情况下root仅具有localhost权限,因此不允许从其他主机与该用户连接。 To fix that we can execute following SQL commands from within container:为了解决这个问题,我们可以从容器内执行以下 SQL 命令:

GRANT CREATE USER ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;

Another option would be to create separate user (by runnin corresponding SQL from within container).另一种选择是创建单独的用户(通过从容器内运行相应的 SQL)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM