<p>One way to achieve this is by using the MySQL socket to connect instead of port. For this you would have bind a mount to your container. You would anyway need a host directory bound to container to persist Database data.</p>
<ul>
<li>First create a directory on host to mount container <code>/var/lib/mysql</code></li>
<li>Use <code>-v</code> option in <code>docker run</code> to attach host path to container</li>
<li>Connect from client using <code>-S</code> and point to <code>mysql.sock</code> socket file</li>
</ul>
<p>Example:</p>
<pre><code>mkdir /home/user/mysql-1
</code></pre>
<p>Then start the container so that <code>/home/user/mysql-1</code> will attach to container's <code>/var/lib/mysql</code>:</p>
<pre><code>docker run --name mysql -d \
-v /home/user/mysql-1:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=password \
mysql/mysql-server 
</code></pre>
<p>Once the container is started, you can connect to it with:</p>
<pre><code>mysql -h localhost -u user -p -S /home/user/mysql-1/mysql.sock
</code></pre>

CC BY-SA 4.0
2020-09-25T05:37:11.067Z
2020-09-25T05:37:11.067Z
seshadri_c
<p>starting up the default <code>mysql</code> server:</p>
<pre><code>root $ 
root $ docker run --name mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql/mysql-server:latest
d2b066e9aa4192b45334e9dada923b6b3dca5703e811733e150333f52849c1a1
root $ 
root $ docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                            PORTS                               NAMES
d2b066e9aa41        mysql/mysql-server:latest   &quot;/entrypoint.sh mysq…&quot;   9 seconds ago       Up 6 seconds (health: starting)   0.0.0.0:3306-&gt;3306/tcp, 33060/tcp   mysql
root $ 
</code></pre>
<p>trying to login to the container, but getting the host <code>MySQL</code> instance:</p>
<pre><code>nicholas $ 
nicholas $ mysql -h 172.17.0.2 -P 3306 --protocol=tcp -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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&gt; 
mysql&gt; SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| %         | user             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)

mysql&gt; 
</code></pre>
<p>I'm trying that explicit <code>IP</code> address because:</p>
<pre><code>root $ 
root $ docker inspect mysql | grep IP
            &quot;LinkLocalIPv6Address&quot;: &quot;&quot;,
            &quot;LinkLocalIPv6PrefixLen&quot;: 0,
            &quot;SecondaryIPAddresses&quot;: null,
            &quot;SecondaryIPv6Addresses&quot;: null,
            &quot;GlobalIPv6Address&quot;: &quot;&quot;,
            &quot;GlobalIPv6PrefixLen&quot;: 0,
            &quot;IPAddress&quot;: &quot;172.17.0.2&quot;,
            &quot;IPPrefixLen&quot;: 16,
            &quot;IPv6Gateway&quot;: &quot;&quot;,
                    &quot;IPAMConfig&quot;: null,
                    &quot;IPAddress&quot;: &quot;172.17.0.2&quot;,
                    &quot;IPPrefixLen&quot;: 16,
                    &quot;IPv6Gateway&quot;: &quot;&quot;,
                    &quot;GlobalIPv6Address&quot;: &quot;&quot;,
                    &quot;GlobalIPv6PrefixLen&quot;: 0,
root $ 
</code></pre>
<p>although I'm also getting the same result for <code>localhost</code> in that I'm still logging into the host instance of <code>MySQL</code> rather than the container.</p>
<pre><code>root $ 
root $ docker exec -ti --user root mysql bash
bash-4.2# 
bash-4.2# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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&gt; 
mysql&gt; SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | healthchecker    |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

mysql&gt; 
</code></pre>
<p>which is virgin, not having user <code>user</code> configured on the container.</p>
<p>Additionally:</p>
<pre><code>root $ 
root $ netstat -peanut | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      0          25585352   488887/docker-proxy 
root $ 
root $ docker inspect mysql | grep password
                &quot;MYSQL_ROOT_PASSWORD=password&quot;,
root $ 
</code></pre>
<p>and so I was expecting that by specifying <code>tcp</code> for <code>localhost</code> from the host I'd be able to connect to the docker container.  Yet that's not happening, I'm getting the host's database:</p>
<pre><code>nicholas $ 
nicholas $ 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 23
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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&gt; 
mysql&gt; SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| %         | user             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)

mysql&gt; 
</code></pre>
<p>I <em>think</em> if I uninstall <code>mysql</code> server from the host I'll be able to connect to the docker database, but surely there's a better approach.</p>

<docker><networking><databases><mysql><ports>
how to connect to a MySQL database in a docker container?
Nicholas Saunders
# how to connect to a MySQL database in a docker container?

(Asked by: username_0 on 2020-09-25)

starting up the default mysql server:
root $ 
root $ docker run --name mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql/mysql-server:latest
d2b066e9aa4192b45334e9dada923b6b3dca5703e811733e150333f52849c1a1
root $ 
root $ docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                            PORTS                               NAMES
d2b066e9aa41        mysql/mysql-server:latest   "/entrypoint.sh mysq…"   9 seconds ago       Up 6 seconds (health: starting)   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
root $ 

trying to login to the container, but getting the host MySQL instance:
nicholas $ 
nicholas $ mysql -h 172.17.0.2 -P 3306 --protocol=tcp -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| %         | user             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)

mysql> 

I'm trying that explicit IP address because:
root $ 
root $ docker inspect mysql | grep IP
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
                    "IPAMConfig": null,
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
root $ 

although I'm also getting the same result for localhost in that I'm still logging into the host instance of MySQL rather than the container.
root $ 
root $ docker exec -ti --user root mysql bash
bash-4.2# 
bash-4.2# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | healthchecker    |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

mysql> 

which is virgin, not having user user configured on the container.
Additionally:
root $ 
root $ netstat -peanut | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      0          25585352   488887/docker-proxy 
root $ 
root $ docker inspect mysql | grep password
                "MYSQL_ROOT_PASSWORD=password",
root $ 

and so I was expecting that by specifying tcp for localhost from the host I'd be able to connect to the docker container.  Yet that's not happening, I'm getting the host's database:
nicholas $ 
nicholas $ 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 23
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| %         | user             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)

mysql> 

I think if I uninstall mysql server from the host I'll be able to connect to the docker database, but surely there's a better approach.

**Tags:** docker, networking, databases, mysql, ports


---

## 1 answers

### Answer by username_1 on 2020-09-25. Score: 1 | Accepted answer

One way to achieve this is by using the MySQL socket to connect instead of port. For this you would have bind a mount to your container. You would anyway need a host directory bound to container to persist Database data.

First create a directory on host to mount container /var/lib/mysql
Use -v option in docker run to attach host path to container
Connect from client using -S and point to mysql.sock socket file

Example:
mkdir /home/user/mysql-1

Then start the container so that /home/user/mysql-1 will attach to container's /var/lib/mysql:
docker run --name mysql -d \
-v /home/user/mysql-1:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=password \
mysql/mysql-server 

Once the container is started, you can connect to it with:
mysql -h localhost -u user -p -S /home/user/mysql-1/mysql.sock


---

