Dify code node database connection error, parameters are correct

I want to connect Dify to a database and automatically import data into MySQL. The connection parameters are all correct, but it keeps reporting an error:

Data Import Result

:cross_mark: Database import failed: (pymysql.err.OperationalError) (2003, “Can’t connect to MySQL server on ‘111.8.160.135’ ([Errno 101] Network is unreachable)”)
(Background on this error at: https://sqlalche.me/e/20/e3q8)

Dify is deployed in Docker. Running Python code locally to connect to MySQL and test data import is successful. I haven’t been able to resolve this error. Any suggestions?

@Dify_Forum_Helper, please follow up.

从报错信息本身来看:

(pymysql.err.OperationalError) (2003, “Can’t connect to MySQL server on ‘111.8.160.135’ ([Errno 101] Network is unreachable)”)

这不是账号密码之类的参数错误,而是网络根本连不上,并且只在「Dify 所在的 Docker 容器里」出现,本机直接跑 Python 能连上,基本可以判断是容器网络到 111.8.160.135:3306 不通

可以按下面几个方向逐条排查(重点看 1、2 条):


1. 宿主机能否访问这个 MySQL

先在宿主机上确认:

# Execute on the host machine
telnet 111.8.160.135 3306
# Or
nc -vz 111.8.160.135 3306
  • 如果这里就连不上:说明是数据库所在网络/防火墙问题,和 Dify、Docker 无关;
  • 如果 宿主机可以连上:继续看第 2 步(就是 Docker 容器网络问题)。

2. 在 Dify 的容器里测试网络

找到运行 Dify API 的容器,一般名字类似 dify-apidify-server,进入容器里测试:

docker ps  # Find the container ID or name where the API is located
docker exec -it <api_container_name_or_id> bash   # Or sh
# Execute inside the container:
apt-get update && apt-get install -y telnet netcat-openbsd || apk add --no-cache busybox-extras
telnet 111.8.160.135 3306
# Or
nc -vz 111.8.160.135 3306
  • 如果容器里同样是连不上(超时/Network is unreachable),那就可以确定是Docker 容器到外部 MySQL 的路由/防火墙问题
  • 常见原因:
    • 公司/机房的防火墙只允许宿主机 IP 访问,不允许 Docker 桥接网段访问;
    • 云厂商安全组仅允许特定源 IP,Docker 出口 NAT 后 IP 不在白名单中;
    • 宿主机启用了 firewalld / iptables 规则,限制了 Docker 网卡对外访问。

解决思路(示例):

  • 在 MySQL 服务器侧/防火墙/安全组中,允许来自宿主机出口 IP 所在网段或 Docker 网桥网段的访问;
  • 或确认 Docker 使用的是 NAT 出口,实际出去的还是宿主机 IP(多数情况是这样),那就确保 MySQL 那边白名单允许这个 IP;
  • 如果是内网特殊环境(比如只允许一台跳板机访问),可以让 Dify 跑在同一台能直连 DB 的机器上,或配置网络代理/内网穿透。

3. 确认你在代码节点里用的 MySQL 地址

你现在的报错里是:

Can't connect to MySQL server on '111.8.160.135'

再确认几点:

  1. Is the port 3306:
    • 如果 MySQL 不是默认 3306,代码里要显式指定 port=xxxx
  2. If MySQL is on the same host machine, do not use localhost or 127.0.0.1:
    • For a container, 127.0.0.1 refers to the container itself, not the host machine;
    • If MySQL is on the host machine, you should use the IP that the host machine sees on the Docker bridge (e.g., the host machine’s internal IP, or the host machine’s IP in the same local area network), not 127.0.0.1
  3. If MySQL is running in another Docker container and is in the same docker-compose.yml as Dify, it is recommended:
    services:
      dify-api:
        # ...
        depends_on:
          - mysql
        networks:
          - dify_net
    
      mysql:
        image: mysql:8
        # ...
        networks:
          - dify_net
    
    networks:
      dify_net:
        driver: bridge
    
    那么代码节点里 write mysql (service name) for host,and port 3306。

4. Check Docker Container Network Mode

Check your docker-compose.yml to see if the Dify API container has any special settings:

  • If you used:
network_mode: host

Then the container will directly use the host network, at which point:

  • Accessing 111.8.160.135:3306 from within the container will behave almost identically to the host machine;

  • If it’s still network unreachable, it means there’s an issue with this path itself (revert to step 1 for troubleshooting).

  • If it’s the default bridge network:

    • Check if the host firewall allows the docker0 network interface to access the outside;
    • Or open up the source network segment on the MySQL server side.

5. Information for You to Supplement (for further diagnosis)

If the above steps still don’t resolve the issue, you can provide the following key information:

  1. Configuration of Dify-related services (especially the API) in docker-compose.yml;
  2. MySQL deployment location:
    • Public network machine? Cloud RDS? Or a Docker container on the same host machine?
  3. Screenshot or output of nc -vz 111.8.160.135 3306 executed separately on the host machine and inside the Dify container.

Summary

  • The error indicates network unreachable, not an issue with account, database name, or password;
  • If local Python can connect but code in Docker cannot, the root cause is almost certainly network/firewall/routing restrictions from the Docker container to MySQL;
  • First, directly test the connection with telnet / nc inside the container, then adjust security groups, firewalls, or docker-compose network configuration based on reachability.

You can first test according to steps 1 and 2, and post the test results and some configurations, and I can help you figure out how to make the specific changes.

1\. Can the host machine access this MySQL?

I successfully connected on the host machine.

  1. Test network in Dify’s container

I also successfully connected to the dify-api container.

  1. Confirm the MySQL address you are using in the code node

The address and port in the code are fine

  1. Check the Docker container’s network mode

I checked the configuration file for any special configurations

My MySQL deployment location is a Docker container on the same host.

Sandbox has additional restrictions; for details, please refer to the sandbox FAQ.