title: How to connect to MySQL running on Docker from the host machine
body: <p>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.</p>
  
  <p>Basically, I want to create a docker image for mysql and then connect to it from my host machine (Mac OS X).</p>
  
  <p>Based on <a href="https://stackoverflow.com/questions/33001750/connect-to-mysql-in-a-docker-container-from-the-host">this</a> post , I have to share the mysql unix socket with the host. towards this I have done the following steps</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Now I get the error</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But you see that I have provided the password and initialized my database.</p>
  
  <p>All I want is that from my host machine, I can connect to the mysql database running inside docker.</p>
  
  <p>EDIT:: ----- solution which worked ------</p>
  
  <p>Thanks RICO. Finally the steps which worked for me are</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Use the IP address obtained in step 8. port is 3306, user is root, password is password, database is MyDB.</p>
  
  <p>Connection is successful!</p>