简体   繁体   中英

Can't connect to MySQL running in docker

I'm trying to build docker image with some legacy LAMP development stack for development purposes. Basically I'm taking ubuntu image and installing bitnami LAMP stack. Here's Dockerfile I have so far:

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.

I've tried connect from MySQL Workbench and JetBrains' DataGrip both failing with similar error:

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).

I've tried:

  1. Using map to another host's port (3306, 3308, 10123) assuming there some conflicts;
  2. Using different MySQL drivers.

MySQL version is 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.

MySQL is in default configuration as security measure , only accepting access from the localhost.

So you have to control and change the following parameter in the my.cnf

[mysqld] 
bind-address=0.0.0.0

That would allow access from every ip

Additionally by default root has privileges for localhost only so it's not allowed to connect with that user from another hosts. To fix that we can execute following SQL commands from within container:

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

Another option would be to create separate user (by runnin corresponding SQL from within container).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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