Sunday 26 March 2017

Connect To MySQL Shell Of A Container | Docker


How to connect to MySQL Shell of MySQL Docker Container?

Connect to MySQL Shell of MySQL Container Listening on Anywhere (0.0.0.0)?

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Docker


If you are running MySQL On a Docker Container and not able to connect to MySQL shell using following command:

$ mysql -hlocalhost -uroot -p
#or 
$ mysql -uroot -p

First check whether the container is listening on anywhere(0.0.0.0) or some specific IP using netstat command:

ajeet@my-host:~/myapp$ sudo netstat -tlupn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1415/sshd
tcp6       0      0 :::3306                 :::*                    LISTEN      20533/docker-proxy
tcp6       0      0 :::80                   :::*                    LISTEN      20703/docker-proxy
tcp6       0      0 :::22                   :::*                    LISTEN      1415/sshd

Here as we can see the port 3306 is listening on all, so we can connect to mysql via localhost, private IP, public IP(if user is allowed to connect from any host).

If you are getting error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

You need to specify the protocol as well because you are running MySQL inside Docker container, socket is not available and you need to connect through TCP. Setting "--protocol" in the mysql command will change that.

Run the following command to connect to MySQL shell:

$ mysql -h localhost -P 3306 --protocol=tcp -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

This will take you to MySQL shell.

If this doesn't work for you and the MySQL container is listening on some other specific IP then get the IP on which the container is listening and then try to connect to that IP by specifying the IP in -h option:


$ docker inspect 0a0a45cdf253     
...  
"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "d4318b368be1f991174e59830a8889a7b02aa866d7eaab1559cc78fd79ba9e0e",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3306"
                    }
                ]
            },
...

Look here for IP MySQL container is listening on and then try to connect:

$ mysql -h<IP> -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>


0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel