0

I have a LAMP container. I want to run WordPress, but it cannot connect to Mysql. I set the container port mapping to 3307:3306 because another container use the default mysql port. I tried to connect from host to the container's mysql server to test it, mysql -u xxxx -h 127.0.0.1:3307 but the result is:

ERROR 2005 (HY000): Unknown MySQL server host '127.0.0.1:3307' (0)

I tried to change the bind address 127.0.0.1 to 0.0.0.0 in the container's mysql config but nothing changed.

What's wrong with this?

aynber
  • 22,380
  • 8
  • 50
  • 63
Peter
  • 1
  • Please share the docker run command you have used to start mysql & the output of `netstat -anp | grep LISTEN | grep 3307` – Rajiv Aug 20 '16 at 06:53
  • It works! I made some mistakes. – Peter Aug 20 '16 at 14:29
  • First, >mysql syntax wasn't correct. The correct syntax is: mysql -u xxxx -h 127.0.0.1 -P3307 Second, I didn't configure the mysql user to connect from another host. The dockerized mysql functioned well from the first moment. – Peter Aug 20 '16 at 14:37

2 Answers2

0

You have to link database container to wordpress container. I do not know what do you use for containers and how you orchestrate them, but in the simplest case you have to add something like this:

--link some_mysql_container:mysql

when you are trying to run wordpress container. Of course first you need to create a database container. Let it has "some-mysql-for-wordpress" name (image mysql:latest). When you will have this, now you can run a wordpress container:

docker run --name some-wordpress --link some-mysql-for-wordpress:mysql -d wordpress

And that is all. :)

ampH
  • 1
0

In my case, in php file, you can just use your container instead of host name.


function connect_db() {
   $dbhost = "database"; // <= like this
   $dbuser = "docker";
   $dbpass = "docker";
   $dbname = "dbname";
   $database_class = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
   $database_class->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   return $database_class;
}

this work for me. hope this help for other later 5 years.

praHoc
  • 357
  • 4
  • 16