Wordpress: External MySQL connection refused

Created on 25 Jul 2015  路  6Comments  路  Source: docker-library/wordpress

The MySQL connection is refused running Docker Wordpress on a CentOS 6.6 machine. The MySQL server is running on the same machine as Docker. I have a MySQL wordpress user with full privs on the wordpress database from any host and have confirmed that I can connect to the account from the command line.

Any ideas?

docker run -it --name blog -e WORDPRESS_DB_HOST=127.0.0.1:3306 -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=*** -p 9009:80 wordpress:latest

WordPress not found in /var/www/html - copying now...
WARNING: /var/www/html is not empty - press Ctrl+C now if this is an error!
+ ls -A
index.html
+ sleep 10
Complete! WordPress has been successfully copied to /var/www/html

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10

MySQL Connection Error: (2002) Connection refused

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10

MySQL Connection Error: (2002) Connection refused

Most helpful comment

Using 127.0.0.1 makes it so that WordPress attempts to connect to port 3306
_inside_ the container, not to your MySQL container.

You'll likely either want to use --link to link to your MySQL container or
use port mapping to map the MySQL container's port 3306 to a port on the
host and then use the host's actual IP address instead of 127.0.0.1.

All 6 comments

Using 127.0.0.1 makes it so that WordPress attempts to connect to port 3306
_inside_ the container, not to your MySQL container.

You'll likely either want to use --link to link to your MySQL container or
use port mapping to map the MySQL container's port 3306 to a port on the
host and then use the host's actual IP address instead of 127.0.0.1.

I see. Yes, when I replaced 127.0.0.1 with the machine's actual IP it connects, but now the MySQL connection times out. I'm guessing that's because of the firewall we have on the host's 3306 port.

In my case, for a variety of reasons, I'm not running MySQL in a container, nor do I wish to. MySQL is running on the same host as Docker.

I read up on Docker container networking and it seems that I should have --net=host set in the run command. My understanding is that this essentially de-containerizes networking such that 127.0.0.1:3306 in the container maps to port 3306 on the host. That plus a change to the host's firewall to allow connections on 127.0.0.1:3306 got it working.

Thanks for the reply.

I would not recommend using --net host. Doing so can cause all sorts of odd
behavior when the libraries in the container start registering themselves
with network-based services on the host.

Ok. I changed it back to bridge mode. It's working now, just needed to add a firewall rule to allow connections on the host's 3306 port from the docker0 interface. Thanks again.

:+1:

Was this page helpful?
0 / 5 - 0 ratings