I am trying to access parent host MySQL from docker container created in the same machine.
Getting below error:
PDOException: SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) in lock_may_be_available() (line 167 of /var/www/html/apachedev/git/cloud6/server/includes/lock.inc).
I tried below steps to solve the problem:
Step 1:
Used below command to get the docker network gateway IP of my host
$sudo ip addr show docker0
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:64:aa:b0:38 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:64ff:feaa:b038/64 scope link
valid_lft forever preferred_lft forever
Step 2:
Added IP 172.17.0.1 as DB host in MySQL database config, the config file is part of my application that is running from the docker container.
Step 3:
Altered the database user to use mysql_native_password mode
ALTER USER ‘username’@’%’ IDENTIFIED WITH mysql_native_password BY ‘my_password’;
Now, my application running in docker container is able to connect to the MySQL database installed in the parent host.