使用Docker Compose搭建Presto集群时无法连接Hive Metastore求助
我帮你排查下这个连接拒绝的问题,核心原因其实是Docker容器间的网络通信配置错误,咱们一步步来解决:
错误根源分析
你看到的关键错误日志:
coordinator | Caused by: org.apache.thrift.transport.TTransportException: localhost:9083: java.net.ConnectException: Connection refused (Connection refused)
这是因为在Docker容器内部,localhost指向的是容器自身,而不是宿主机或者Hive Metastore容器。Presto Coordinator容器尝试连接自己的9083端口,但这个端口并没有监听Hive Metastore服务,所以自然连接失败。
具体修复步骤
1. 修改Presto的hive.properties配置
把hive.metastore.uri从thrift://localhost:9083改成使用Docker Compose服务名的地址:
connector.name=hive-hadoop2 hive.metastore.uri=thrift://hive-metastore:9083 # 用服务名替代localhost hive.metastore.username=hive hive.allow-drop-table=true
Docker Compose会自动在默认网络中把服务名hive-metastore解析为对应容器的IP,这样Presto就能正确定位到Hive Metastore服务。
2. 调整Hive Metastore的metastore-site.xml配置
确保Metastore监听容器的所有网卡,而不是只绑定内部的localhost:
<property> <name>metastore.thrift.uris</name> <value>thrift://0.0.0.0:9083</value> # 改成0.0.0.0允许外部容器访问 <description>Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore.</description> </property>
这样Hive Metastore会绑定到容器的所有网络接口,确保其他容器能正常建立连接。
3. 确保Hive Metastore服务完全就绪
Docker Compose的depends_on只保证容器启动顺序,不保证服务完全就绪。你可以给hive-metastore服务添加健康检查,确保服务就绪后再让Presto尝试连接:
修改docker-compose.yml中的hive-metastore部分:
hive-metastore: image: "hive:3.0.0" ports: - "9083:9083" depends_on: - mariadb healthcheck: test: ["CMD", "nc", "-z", "localhost", "9083"] interval: 10s timeout: 5s retries: 5
这样Compose会等待Metastore的9083端口就绪后,再认为容器处于健康状态。
验证修复
重启整个集群:
docker-compose down && docker-compose up -d
等所有容器启动完成后,访问Presto的Web UI(http://localhost:8080)查看Hive连接器状态,或者执行简单的SQL查询验证连接是否正常。
内容的提问来源于stack exchange,提问作者vinny

