How to connect to MySQL running on Docker from the host machine

11/19/2015

I have already googled on this subject and found few threads. Based on these threads I have followed the following steps. But I am facing a problem.

Basically, I want to create a docker image for mysql and then connect to it from my host machine (Mac OS X).

Based on this post , I have to share the mysql unix socket with the host. towards this I have done the following steps

1. Start docker quick terminal
2. docker run --name mysql -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
3. docker exec -it mysql bash
4. mysql -uroot -p
5. create database MyDB;
6. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';
7. exit;
8. mkdir /Users/abhi/host
9. docker run -it -v /host:/shared mysql/mysql-server:latest

Now I get the error

MacBook-Pro:~$ docker run -it -v /Users/abhi/host:/shared mysql/mysql-server
error: database is uninitialized and password option is not specified
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

But you see that I have provided the password and initialized my database.

All I want is that from my host machine, I can connect to the mysql database running inside docker.

EDIT:: ----- solution which worked ------

Thanks RICO. Finally the steps which worked for me are

1. Start docker quick terminal
2. docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
3. docker exec -it mysql bash
4. mysql -uroot -p
5. create database MyDB;
  
  or:
  CREATE USER 'root'@'%' IDENTIFIED BY 'root';
  GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

6. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';
7. exit;
8. docker-machine env default

Use the IP address obtained in step 8. port is 3306, user is root, password is password, database is MyDB.

Connection is successful!

-- Knows Not Much
docker
kubernetes
mysql

2 Answers

11/19/2015

So you basically you need to expose the mysql port to your host:

docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest

Then you can access from your host using the mysql command line:

mysql -h127.0.0.1 -ppassword -uroot

Not sure why you are trying to run another container to connect (perhaps you meant linking two containers)

If you are using Mac (or Windows) with docker-machine you want to connect to the IP address of your docker-machine VM. For example:

$ docker-machine ssh default
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.9.0, build master : 16e4a2a - Tue Nov  3 19:49:22 UTC 2015
Docker version 1.9.0, build 76d6bc9
docker@default:~$ ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 08:00:27:E6:C7:20
          inet addr:192.168.99.100  Bcast:192.168.99.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fee6:c720/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:18827 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10280 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1791527 (1.7 MiB)  TX bytes:2242596 (2.1 MiB)

Then connect to:

mysql -h192.168.99.100 -ppassword -uroot
-- Rico
Source: StackOverflow

12/24/2019

docker run -e MYSQL_ROOT_PASSWORD=pass --name sql-db -p 3306:3306 mysql

docker exec -it sql-db bash

mysql -u root -p

-- Ajay Singh
Source: StackOverflow