简体   繁体   中英

can not connect to mysql in docker's bridge network

I created a MySQL container but can only connect to the database from the host.

I start the MySQL container in this file:

docker-composer.yml:

version: '3.3'

services:
   db:
     image: mysql:5.7
     ports:
       - "3307:3306"
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}

when run docker-compser up , I got this error:
MySQL Connection Error: (2002) No route to host

But I can succeed to connect to the container from the host, like this: mysql -u root -p -h 127.0.0.1 -P 3307

You can try to connect without a port specifying:

WORDPRESS_DB_HOST: db

It turns out to be a firewall problem. I am using fedora 32 system. And the docker interface using the default zone which will block 80 port and 3306 port. So, after I bind my docker interface to trusted zone and added some port everything worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM