Connecting to a MySQL container in one container

Good day,

I use Kubernetes to run containers in the google container engine.

The idea is to run two containers in a container. One container uses the mysql docker image, the other uses php, laravel, nginx and composer.

Locally it works. The idea is that php can connect to the database on localhost, and this should work if both containers are in the same container. However, when pod is running, we see the following message in the log:

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

The only difference is that in local testing, I change localhost to the docker internal ip address.

Thanks and good afternoon

+4
source share
2 answers

On Unix, MySQL programs handle the localhost host name in a way that is likely to be different than expected compared to other network programs. To connect to localhost, MySQL programs try to connect to the local server using a Unix socket file. This happens even if the option is specified --portor -Pto indicate the port number. To have the client establish a TCP / IP connection to the local server, use --hostor -hto specify the host name value 127.0.0.1or IP address or local server name. You can also specify the connection protocol explicitly, even for localhost, using the parameter --protocol=TCP. For example:

shell> mysql --host=127.0.0.1
shell> mysql --protocol=TCP

--protocol , .

Kubernetes Volume, . /etc/mysql/my.cnf socket MySQL , .

+4

MySQL , pipe , , my.cnf...

#bind-address = 127.0.0.1

...

bind-address = {your network ip here}

, , mysql, , , , root , IP-, MySQL , , IP-, my.cnf.

, root , root ( ).

0

Source: https://habr.com/ru/post/1627168/


All Articles