You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ubuntu系统用MySQL 5.7客户端连接Docker CE中的MySQL 8+?

Troubleshooting Connection Issues to Dockerized MySQL from Ubuntu Host

Hey there, let's work through why you can't connect to your Docker MySQL container from your Ubuntu host. Here are the most common fixes to try, ordered by how likely they are to resolve your issue:

1. Add Port Mapping (Most Common Fix)

Your initial docker run command didn't map the container's MySQL port (3306) to your host machine. While connecting directly to the container's internal IP is possible, port mapping simplifies this and avoids most network-related hurdles.

Step-by-Step Fix:

First, stop and remove your existing container:

docker stop test-mysql
docker rm test-mysql

Re-run the container with port mapping (I've also specified the 5.7.11 image to match your host's MySQL version, to avoid version mismatches):

docker run --detach --name=test-mysql --env="MYSQL_ROOT_PASSWORD=password" -p 3306:3306 mysql:5.7.11

Now connect from your Ubuntu host using:

mysql -h 127.0.0.1 -u root -p

Enter your root password (password) when prompted, and you should be in.

2. Fix MySQL User Permissions

By default, the root user in the Docker MySQL container might be restricted to only local connections (localhost), which means it won't accept connections from your host machine.

Step-by-Step Fix:

Enter the container's MySQL shell:

docker exec -it test-mysql mysql -u root -ppassword

Run these SQL commands to grant root access from any IP address (replace password with your actual root password):

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

Try connecting from your host again—this should resolve permission-related blocks.

3. Check Ubuntu's UFW Firewall

If you were trying to connect directly to the container's internal IP (from docker inspect), your Ubuntu host's UFW firewall might be blocking traffic to that IP and port.

Step-by-Step Fix:

First, test if disabling UFW temporarily fixes the issue:

sudo ufw disable

If you can connect after this, re-enable UFW and add a rule to allow traffic to the container's IP:

sudo ufw enable
sudo ufw allow from <your-container-ip> to any port 3306

Replace <your-container-ip> with the IP you retrieved from docker inspect test-mysql | grep IPAddress.

4. Verify MySQL is Listening on All Interfaces

Occasionally, the MySQL config in the container might be set to only listen on the loopback address (127.0.0.1), which prevents external connections.

Step-by-Step Check & Fix:

Enter the container:

docker exec -it test-mysql bash

Check the bind-address setting in the config:

cat /etc/mysql/my.cnf | grep bind-address

If it shows bind-address = 127.0.0.1, create a custom config file on your host and mount it into the container to override this:

# On your host, create a custom config file
echo "bind-address = 0.0.0.0" > ~/mysql-custom.cnf
# Re-run the container with the custom config mounted
docker run --detach --name=test-mysql --env="MYSQL_ROOT_PASSWORD=password" -v ~/mysql-custom.cnf:/etc/mysql/conf.d/custom.cnf -p 3306:3306 mysql:5.7.11

内容的提问来源于stack exchange,提问作者Christian Eriksson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:42:04