MYSQL 2002: No route to host

i tried to install my first wordpress with docker but everytime i compose it in yml file like docker-compose up . it says mysql no route to host .
some error is

[07-Mar-2020 13:38:16 UTC] PHP Warning:  mysqli::__construct(): (HY000/2002): No route to host in Standard input code on line 22

"WARNING: unable to establish a database connection to 'db:3306'"
continuing anyways (which might have unexpected results)

AH00558: apache2: Could not reliably determine the servers fully qualified domain name, using 172.20.0.3. Set the 'ServerName' directive globally to suppress this message

yml files code is below

version: '3.3'

services:
   db:
     image: mysql:5.7
     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: {}

Just call it db, without a port.

Sorry its not working.
Code is fully work in ubuntu, windows but Problem is not only working in Centos 8

when i run with

docker run busybox nslookup google.com

it also says that NO ROUTE TO HOST

but in ubuntu its reply’s with perfect result.
Problem is within Centos 8

So i have the same problem.

I have Centos8 and i trying to deploy wordpress and Mysql and i can’t connect to DB. I was able to solve some part of the problem.

To connect the wordpress to mysql i nee to change the firewall rules in centos8 with this command:

firewall-cmd --permanent --zone=public --add-rich-rule=‘rule family=ipv4 source address=172.17.0.0/16 accept’ && firewall-cmd --reload

Where address is the network ip where your docker containers are running.

Moreover, i need to change the wordpress config.php file to:

/** MySQL hostname */
define( ‘DB_HOST’, ‘172.17.0.1:3306’ );

Where the ip address is the ip of the gateway of the network where the containers are running!!!

So, this work. I was able to run the wordpress and configure the website. But i think that i cant have connection inside the wordpress container to the internet. I think this, because in same pages, should show some content from wordpress server and don’t show anything. show a error. Moreover, when cant import some content from internet sources.

I thinking to change my OS to centos7 to see if this is some problem of the OS

when i type

firewall-cmd --permanent --zone=public --add-rich-rule=‘rule family=ipv4 source address=172.17.0.0/16 accept’ && firewall-cmd --reload

There are some error.

usage: see firewall-cmd man page
firewall-cmd: error: unrecognized arguments: family=ipv4 source address=172.17.0.0/16 accept’

do you run the command on the host with root permissions?

Yes with

Sudo su

And also without sudo su

That means trying both. But did not work

First i enter as root on the host, and them perform the command. Look to the image. i’m in [root@localhost], in this way i don’t need to “su” or “sudo”

I really don’t know why you are getting that error. you can try to turn off your firewall to test.

sudo systemctl disable firewalld

This work to have DNS in the container.

EDIT: try to see if you have any typo in the command

Thanks. I successfully run your given code for firewall you provided with an image. But still, getting error when running docker-compose up

The error is

Mysql Connection Error: (2002) No route to host

You will get the error when you run docker-compose up.

Try do this:

docker-compose up -d

this will run the containers in detach mode.

After that, go to the volume where are the wordpress files and look for the wp-config.php file (see image bellow)

open the file, look for the

/** MySQL hostname */
define( ‘DB_HOST’, ‘db:3306’);

and change for

define( ‘DB_HOST’, ‘172.17.0.1:3306’ );

The ip, is the gateway ip from the network where the container is running. You need to verify what is in your docker.

This is a workaround, this is not a definitive solution, you probably will need to change this ip if you stop your containers or if you change the containers network. in the same way you will need to change and run the firewall command.

did but the error is

“Error establishing a database connection”

well
 i will put here my docker-compose file. I have some things commented due to some debugging. I was able to put the this working using this yml file and the procedures explained before.

> version: '3' #version of YML file
> 
> services:
>   # Database
>   db:
>     image: mysql:5.7
>     #command: --default-authentication-plugin=mysql_native_password
>     container_name: database
>     volumes:
>       - db_data:/var/lib/mysql
>     restart: always
>     ports:
>       - '3306:3306'
>       - '33060:33060'
>     environment:
>       MYSQL_USER: wordpress
>       MYSQL_PASSWORD: wordpress
>       MYSQL_DATABASE: wordpress
>       MYSQL_ROOT_PASSWORD: password
>     #networks:
>       #- wpsite
>   # phpmyadmin
>  # phpmyadmin:
>   #  depends_on:
>    #   - db
>     #image: phpmyadmin/phpmyadmin
>     #restart: always
>     #ports:
>     #  - '8080:80'
>     #environment:
>      #   PMA_HOST: db
>         #PMA_PASSWORD: password
>         #MYSQL_ROOT_PASSWORD: password
>     #networks:
>       #- wpsite
>   # Wordpress
>   wordpress:
>     depends_on:
>       - db
>     links:
>       - db
>     container_name: wordpress
>     image: wordpress:latest
>     ports:
>       - '8080:80'
>     restart: always
>    # volumes: ['./wp-content:/var/www/html/wp-content']
>     environment:
>       WORDPRESS_DB_HOST: db:3306
>       WORDPRESS_DB_USER: wordpress
>       WORDPRESS_DB_PASSWORD: wordpress
>     #networks:
>       #- wpsite
> #networks:
> #  wpsite:
> volumes:
>   db_data: #o mesmo colume que usamos na base de dados

Try to use mariadb instead mysql. I deploy new containers with mariadb and works perfectly without any problem.

Last comment here. I found the problem and the solution.

Problem: Centos 8 firewall

Best Solution: add the docker networks/interfaces to a trusted zone.

In the host:

1: Find the network interfaces used by docker - run the command:

nmcli

and will show all network interfaces in the computer. The docker interfaces are: docker0 and br-xxxxxxxx (i think that depends on how many networks you have in the cluster)

For me, was 3 networks.

2- Add all interfaces to trusted zone with the command:

firewall-cmd --zone=trusted --change-interface=docker0 --permanent
firewall-cmd --zone=trusted --change-interface=brxxxxx --permanent
firewall-cmd --zone=trusted --change-interface=bryyyyy --permanent

3- Reload the firewall with the command:

firewall-cmd --reload

And will work :slight_smile:

If you want to test if the problem is the firewall or not you can disable the all the firewall with:

systemctl disable firewalld

But you need to restart the host (centos8) after that!!! If you don’t restar, will not work!!!

I lost days in this, good luck for everyone who land here!!

3 Likes

sorry dear, i was not available for no internet connection in my house for corona situation.

But I tried, but not working with the last comment of Centos 8 firewall solution that you provide.

did you test it with my yml file? I think that it’s all ok with your yml file, but you can give a try with my.

Can you post some prints of your steps?

@nasilva, Thank you so much for the detailed explaination!
Thank you again for all your effort in digging the root cause in centos


Thanks a lot dear . your solution paid off my effort , I was searching everywhere for solution.
Main culprit was firewall .

Thanks again

Excellent! It solved the issue, thank you very much

it solved my issue thanks :slight_smile: