ShardingSphere-Proxy执行DistSQL注册存储单元遇通信链路失败错误
Docker中ShardingSphere-Proxy注册MySQL存储单元失败:通信链路故障
我在Docker运行的ShardingSphere-Proxy中,通过DistSQL尝试将本地MySQL的db0和db1注册为存储单元,执行命令如下:
REGISTER STORAGE UNIT ds_0 ( HOST="127.0.0.1", PORT=3306, DB="db0", USER="root", PASSWORD="blah" ),ds_1 ( HOST="127.0.0.1", PORT=3306, DB="db1", USER="root", PASSWORD="blah" );
直接通过终端连接本地MySQL完全正常,但通过Docker内的ShardingSphere-Proxy执行上述命令时,触发报错:
ERROR 19000 (44000): Can not process invalid storage units, error message is: [Invalid data source ds_0, error message is: Communications link failure, Invalid data source ds_1, error message is: Communications link failure]
重现步骤
- 本地数据库操作:
mysql -h 127.0.0.1 --user=root --password=blah mysql>create database db0; mysql>create database db1;
- 创建并连接ShardingSphere-Proxy容器:
docker run -d -v /Users/pavankrn/Documents/tech/sspheredock/pgsphere/apache-shardingsphere-5.3.1-shardingsphere-proxy-bin/conf:/opt/shardingsphere-proxy/conf -v /Users/pavankrn/Documents/tech/sspheredock/pgsphere/apache-shardingsphere-5.3.1-shardingsphere-proxy-bin/ext-lib:/opt/shardingsphere-proxy/ext-lib -e PORT=3308 -p13308:3308 apache/shardingsphere-proxy:latest mysql --host=127.0.0.1 --user=root -p --port=13308 sharding_db
- 在ShardingSphere-Proxy的MySQL终端执行:
use sharding_db; REGISTER STORAGE UNIT ds_0 ( HOST="127.0.0.1", PORT=3306, DB="db0", USER="root", PASSWORD="blah" ),ds_1 ( HOST="127.0.0.1", PORT=3306, DB="db1", USER="root", PASSWORD="blah" );
问题原因与解决办法
核心问题是:Docker容器内的127.0.0.1指向容器自身,而非宿主机的MySQL服务,导致无法建立连接。
1. 替换HOST地址
将DistSQL中的HOST="127.0.0.1"替换为宿主机的实际IP(可通过ifconfig/ip addr查看),或在Docker Desktop(Mac/Windows环境)中使用host.docker.internal直接映射到宿主机:
REGISTER STORAGE UNIT ds_0 ( HOST="host.docker.internal", PORT=3306, DB="db0", USER="root", PASSWORD="blah" ),ds_1 ( HOST="host.docker.internal", PORT=3306, DB="db1", USER="root", PASSWORD="blah" );
2. 授权MySQL跨IP访问
确保宿主机MySQL允许Docker容器所在IP段的连接,执行以下SQL(生产环境请限制具体IP段,不要用%):
GRANT ALL PRIVILEGES ON db0.* TO 'root'@'%' IDENTIFIED BY 'blah'; GRANT ALL PRIVILEGES ON db1.* TO 'root'@'%' IDENTIFIED BY 'blah'; FLUSH PRIVILEGES;
3. 检查防火墙设置
确认宿主机防火墙未阻止3306端口的入站请求,允许Docker容器访问该端口。
内容的提问来源于stack exchange,提问作者navule
相关产品推荐
相关产品推荐

