
看到您的连接问题了。这是一个典型的远程连接 MySQL 被拒绝的情况。让我帮您一步步解决。
问题原因分析
连接超时通常是由于以下几个原因:
- MySQL 未允许远程连接
- 防火墙未开放 3306 端口
- MySQL 用户权限限制
- 云服务器安全组配置
解决方案
1. 首先在云服务器上检查 MySQL 容器状态
bash
# 进入容器检查MySQL是否正常运行
docker exec -it mysql8-imooc mysql -uroot -p -e "STATUS;"
2. 检查 MySQL 的绑定地址
bash
# 进入MySQL容器查看绑定配置
docker exec -it mysql8-imooc mysql -uroot -p -e "SHOW VARIABLES LIKE 'bind%';"
如果 bind_address 是 127.0.0.1,需要改为 0.0.0.0:
bash
# 方法一:临时修改
docker exec -it mysql8-imooc mysql -uroot -p -e "SET GLOBAL bind_address='0.0.0.0';"
# 方法二:永久修改(推荐)
# 先停止容器
docker stop mysql8-imooc
# 重新启动容器,添加绑定参数
docker run -d --name mysql8-imooc \
-e MYSQL_ROOT_PASSWORD=您的密码 \
-p 3306:3306 \
mysql:8.4.4 \
--bind-address=0.0.0.0
3. 创建远程访问用户并授权
bash
# 进入MySQL
docker exec -it mysql8-imooc mysql -uroot -p
# 在MySQL中执行以下命令:
# 创建远程访问用户(替换'your_password'为实际密码)
CREATE USER 'root'@'%' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
4. 检查云服务器防火墙
bash
# 检查ufw状态
ufw status
# 如果ufw开启,确保3306端口开放
ufw allow 3306
# 检查iptables
iptables -L
5. 检查云服务商安全组规则
这是最常见的问题!您需要在云服务商控制台配置:
- 登录云服务商控制台 → 安全组 → 添加规则
- 协议类型: TCP
- 端口范围: 3306
- 授权对象 :
0.0.0.0/0(或您的本地IP)
6. 在服务器上测试端口连通性
bash
# 检查3306端口是否监听
netstat -tlnp | grep 3306
# 从服务器本地测试连接
telnet 127.0.0.1 3306
# 检查容器端口映射
docker port mysql8-imooc
7. 完整的诊断脚本
在云服务器上运行这个脚本来检查所有配置:
bash
#!/bin/bash
echo "=== 检查MySQL容器状态 ==="
docker ps | grep mysql
echo "=== 检查端口监听 ==="
netstat -tlnp | grep 3306
echo "=== 检查MySQL绑定地址 ==="
docker exec -it mysql8-imooc mysql -uroot -p -e "SHOW VARIABLES LIKE 'bind%';" 2>/dev/null
echo "=== 检查用户权限 ==="
docker exec -it mysql8-imooc mysql -uroot -p -e "SELECT user, host FROM mysql.user;" 2>/dev/null
echo "=== 检查防火墙状态 ==="
ufw status | grep 3306
推荐的重置方案
如果上述步骤复杂,最简单的方法是:
bash
# 1. 停止并删除现有容器
docker stop mysql8-imooc
docker rm mysql8-imooc
# 2. 重新启动容器,配置远程访问
docker run -d --name mysql8-imooc \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
-v /home/mysql8/data:/var/lib/mysql \
mysql:8.4.4 \
--bind-address=0.0.0.0
请按照以上步骤操作,第5步(云服务商安全组)是最关键的,大多数连接问题都是因为这个没有配置。完成这些配置后,您的 Windows 本地应该就能连接成功了。
ubuntu@VM-0-16-ubuntu:~$ sudo -i
root@VM-0-16-ubuntu:~# ls
snap software
root@VM-0-16-ubuntu:~# cd /home
root@VM-0-16-ubuntu:/home# ls
mysql8 software ubuntu
root@VM-0-16-ubuntu:/home# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 1b44b5a3e06a 2 months ago 10.1kB
mysql 8.4.4 4a8a163431d3 9 months ago 769MB
root@VM-0-16-ubuntu:/home# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ae78011b41f mysql:8.4.4 "docker-entrypoint.s..." 21 minutes ago Up 16 minutes 0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp, 33060/tcp mysql8-imooc
0ea4334dfabd hello-world "/hello" 39 hours ago Exited (0) 39 hours ago wonderful_borg
root@VM-0-16-ubuntu:/home# docker exec -it mysql8-imooc mysql -uroot -p -e "STATUS;"
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
root@VM-0-16-ubuntu:/home# docker exec -it mysql8-imooc mysql -uroot -p -e "STATUS;"
Enter password:
--------------
mysql Ver 8.4.4 for Linux on x86_64 (MySQL Community Server - GPL)
Connection id: 11
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.4.4 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/run/mysqld/mysqld.sock
Binary data as: Hexadecimal
Uptime: 17 min 14 sec
Threads: 3 Questions: 11 Slow queries: 0 Opens: 130 Flush tables: 3 Open tables: 49 Queries per second avg: 0.010
--------------
root@VM-0-16-ubuntu:/home# docker stop mysql8-imooc
mysql8-imooc
root@VM-0-16-ubuntu:/home# docker run -p 3306:3306 --name mysql8-imooc \
-v /home/mysql8/log:/var/log/mysql \
-v /home/mysql8/data:/var/lib/mysql \
-v /home/mysql8/conf:/etc/mysql/conf.d \
-v /home/mysql8/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=Abc_123456789 \
-d mysql:8.4.4 \
--bind-address=0.0.0.0 \
--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
docker: Error response from daemon: Conflict. The container name "/mysql8-imooc" is already in use by container "8ae78011b41f08e73075c479e06d88eab3a8533124bee7d8c4e40113b2f32eab". You have to remove (or rename) that container to be able to reuse that name.
Run 'docker run --help' for more information
root@VM-0-16-ubuntu:/home# docker stop mysql8-imooc
mysql8-imooc
root@VM-0-16-ubuntu:/home# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ae78011b41f mysql:8.4.4 "docker-entrypoint.s..." 24 minutes ago Exited (0) About a minute ago mysql8-imooc
0ea4334dfabd hello-world "/hello" 39 hours ago Exited (0) 39 hours ago wonderful_borg
root@VM-0-16-ubuntu:/home# docker rmi mysql8-imooc
Error response from daemon: No such image: mysql8-imooc:latest
root@VM-0-16-ubuntu:/home# docker rmi 8ae78011b41f
Error response from daemon: No such image: 8ae78011b41f:latest
root@VM-0-16-ubuntu:/home# docker run -p 3306:3306 --name mysql8-imooc \
-v /home/mysql8/log:/var/log/mysql \
-v /home/mysql8/data:/var/lib/mysql \
-v /home/mysql8/conf:/etc/mysql/conf.d \
-v /home/mysql8/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=Abc_123456789 \
-d mysql:8.4.4 \
--bind-address=0.0.0.0 \
--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
docker: Error response from daemon: Conflict. The container name "/mysql8-imooc" is already in use by container "8ae78011b41f08e73075c479e06d88eab3a8533124bee7d8c4e40113b2f32eab". You have to remove (or rename) that container to be able to reuse that name.
Run 'docker run --help' for more information
root@VM-0-16-ubuntu:/home#
root@VM-0-16-ubuntu:/home#
root@VM-0-16-ubuntu:/home# docker rm mysql8-imooc
mysql8-imooc
root@VM-0-16-ubuntu:/home# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ea4334dfabd hello-world "/hello" 39 hours ago Exited (0) 39 hours ago wonderful_borg
root@VM-0-16-ubuntu:/home# docker run -p 3306:3306 --name mysql8-imooc \
-v /home/mysql8/log:/var/log/mysql \
-v /home/mysql8/data:/var/lib/mysql \
-v /home/mysql8/conf:/etc/mysql/conf.d \
-v /home/mysql8/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=Abc_123456789 \
-d mysql:8.4.4 \
--bind-address=0.0.0.0 \
--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
3f6cf4a44feb8e7ac02d6ce70a3880fb8e1524aa3e7a3cd2a63e21113922ff85
root@VM-0-16-ubuntu:/home# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f6cf4a44feb mysql:8.4.4 "docker-entrypoint.s..." 18 seconds ago Up 17 seconds 0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp, 33060/tcp mysql8-imooc
0ea4334dfabd hello-world "/hello" 39 hours ago Exited (0) 39 hours ago wonderful_borg
root@VM-0-16-ubuntu:/home# docker exec -it mysql8-imooc mysql -uroot -pAbc_123456789 -e "SHOW VARIABLES LIKE 'bind%';"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| bind_address | 0.0.0.0 |
+---------------+---------+
root@VM-0-16-ubuntu:/home# docker exec -it mysql8-imooc mysql -uroot -p -e "STATUS;"
Enter password:
--------------
mysql Ver 8.4.4 for Linux on x86_64 (MySQL Community Server - GPL)
Connection id: 9
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.4.4 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/run/mysqld/mysqld.sock
Binary data as: Hexadecimal
Uptime: 4 min 7 sec
Threads: 2 Questions: 10 Slow queries: 0 Opens: 136 Flush tables: 3 Open tables: 55 Queries per second avg: 0.040
--------------
root@VM-0-16-ubuntu:/home# docker exec -it mysql8-imooc mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.4.4 MySQL Community Server - GPL
Copyright (c) 2000, 2025, Oracle and/or its affiliates.
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> CREATE USER 'root'@'%' IDENTIFIED BY 'Abc_123456789';
GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'%'
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> EXIT;
Bye
root@VM-0-16-ubuntu:/home# ufw status
Status: inactive
root@VM-0-16-ubuntu:/home# ufw allow 3306
Rules updated
Rules updated (v6)
root@VM-0-16-ubuntu:/home# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
YJ-FIREWALL-INPUT all -- anywhere anywhere
Chain FORWARD (policy DROP)
target prot opt source destination
DOCKER-USER all -- anywhere anywhere
DOCKER-FORWARD all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain DOCKER (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere 172.17.0.2 tcp dpt:mysql
DROP all -- anywhere anywhere
Chain DOCKER-BRIDGE (1 references)
target prot opt source destination
DOCKER all -- anywhere anywhere
Chain DOCKER-CT (1 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
Chain DOCKER-FORWARD (1 references)
target prot opt source destination
DOCKER-CT all -- anywhere anywhere
DOCKER-ISOLATION-STAGE-1 all -- anywhere anywhere
DOCKER-BRIDGE all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target prot opt source destination
DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere
Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target prot opt source destination
DROP all -- anywhere anywhere
Chain DOCKER-USER (1 references)
target prot opt source destination
Chain YJ-FIREWALL-INPUT (1 references)
target prot opt source destination
REJECT all -- 157.245.67.217 anywhere reject-with icmp-port-unreachable
REJECT all -- 185.156.73.233 anywhere reject-with icmp-port-unreachable
REJECT all -- 80.94.95.115 anywhere reject-with icmp-port-unreachable
REJECT all -- 80.94.95.116 anywhere reject-with icmp-port-unreachable
REJECT all -- 107-173-154-136-host.colocrossing.com anywhere reject-with icmp-port-unreachable
REJECT all -- 117.72.204.255 anywhere reject-with icmp-port-unreachable
REJECT all -- 167.99.38.212 anywhere reject-with icmp-port-unreachable
REJECT all -- 161.132.48.14 anywhere reject-with icmp-port-unreachable
REJECT all -- 203.195.82.113 anywhere reject-with icmp-port-unreachable
REJECT all -- 170.78.230.60 anywhere reject-with icmp-port-unreachable
REJECT all -- 106.36.198.78 anywhere reject-with icmp-port-unreachable
root@VM-0-16-ubuntu:/home# netstat -tlnp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 107495/docker-proxy
tcp6 0 0 :::3306 :::* LISTEN 107502/docker-proxy
root@VM-0-16-ubuntu:/home# telnet 127.0.0.1 3306
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
I
8.4.4
@}@3WM��]k`L mpiZEcaching_sha2_password2#08S01Got timeout reading communication packetsConnection closed by foreign host.
root@VM-0-16-ubuntu:/home# docker port mysql8-imooc
3306/tcp -> 0.0.0.0:3306
3306/tcp -> [::]:3306
root@VM-0-16-ubuntu:/home# #!/bin/bash
echo "=== 检查MySQL容器状态 ==="
docker ps | grep mysql
echo "=== 检查端口监听 ==="
netstat -tlnp | grep 3306
echo "=== 检查MySQL绑定地址 ==="
docker exec -it mysql8-imooc mysql -uroot -p -e "SHOW VARIABLES LIKE 'bind%';" 2>/dev/null
echo "=== 检查用户权限 ==="
docker exec -it mysql8-imooc mysql -uroot -p -e "SELECT user, host FROM mysql.user;" 2>/dev/null
echo "=== 检查防火墙状态 ==="
ufw status | grep 3306
=== 检查MySQL容器状态 ===
3f6cf4a44feb mysql:8.4.4 "docker-entrypoint.s..." 8 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp, 33060/tcp mysql8-imooc
=== 检查端口监听 ===
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 107495/docker-proxy
tcp6 0 0 :::3306 :::* LISTEN 107502/docker-proxy
=== 检查MySQL绑定地址 ===
Enter password:
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| bind_address | 0.0.0.0 |
+---------------+---------+
=== 检查用户权限 ===
Enter password:
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
=== 检查防火墙状态 ===
root@VM-0-16-ubuntu:/home#
