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

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

docker exec -it sql-db bash

mysql -u root -p


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

Tags:

Docker

Mysql