简体   繁体   中英

Unable to Connect to Containerized MySQL Database container remotely

Using Docker Desktop, I've started a container of mysql/mysqlserver.

docker run --name=mysqlproject -e MYSQL_ROOT_HOST=% -p 3306:3306 -d mysql/mysql-server

I then went to create some users for my database.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

create user 'dev'@'%' identified by 'password';

grant select on optim.* to 'dev'@'%';

CREATE USER 'root'@'%' IDENTIFIED BY 'root'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION

When attempting to connect to my MySQL database from localhost, I was successful

sudo mysql -h 127.0.0.1 -P 3306 -p -u dev

I can also access the MySQL database from MySQL Workbench using the IP4 Address of my Wireless LAN adapter.

However, other users on the LAN cannot access my MYSQL database remotely

ERROR 2003 (HY000): Can't connect to MySQL server on <my Wireless LAN adapter IP> (110)

When looking at the firewall settings in place, I noticed that there was a rule in place blocking TCP traffic to 'Docker Desktop Backend'. Without the privilege to change the rule, I tried starting the container from Ubuntu instead, which is NOT integrated with Docker Desktop, at least according to Resources>WSL Integration.

docker start project

The issue persists - users cannot access my containerized MySQL database remotely.

You need to try with interactive mode from any host, but you should have the ssh key authentication to where the MySQL docker container running.

In my case, sometimes the developers want to access the DB from their local, the below commands help to connect to the DB interactively but the Mysql should listen to the socket path.

ssh -t "host-ip" \
docker run --rm -it \
--volume /db:/db:rw \
--entrypoint "/bin/bash" mysql-docker-image \
-lic /usr/bin/mysql -U username --host /mysql-db-socket-path/run/filename.socket

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