title: Docker Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Ubuntu
body: <p>I was playing with docker and mounted my local mysql to a docker container and connected MySql-Workbench so I could view the DB (experimenting) here is the command I ran.</p>
  
  <pre><code>docker run -d --name alldb-mysql -v /var/lib/mysql:/var/lib/mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=password -p 3306:3306 mysql:latest
  </code></pre>
  
  <p>after I stopped my container and removed it, I can't start/restart mysql (local install). when I run <code>sudo /etc/init.d/mysql start</code> it returns </p>
  
  <pre><code>[....] Starting mysql (via systemctl): mysql.serviceJob for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
   failed!
  </code></pre>
  
  <p>so I checked <code>systemctl status mysql.service</code></p>
  
  <pre><code>     mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: activating (start-post) (Result: exit-code) since Mon 2017-04-03 22:26:15 IST; 26s ago
    Process: 5470 ExecStart=/usr/sbin/mysqld (code=exited, status=1/FAILURE)
    Process: 5465 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 5470 (code=exited, status=1/FAILURE);         : 5471 (mysql-systemd-s)
      Tasks: 2
     Memory: 1.6M
        CPU: 222ms
     CGroup: /system.slice/mysql.service
             └─control
               ├─5471 /bin/bash /usr/share/mysql/mysql-systemd-start post
               └─6579 sleep 1
  
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.980564Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explici
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.980614Z 0 [Warning] Can't create test file /var/lib/mysql/n.lower-test
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.980638Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.17-0ubuntu0.16.04.1) starting as process 5470 .
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.981928Z 0 [Warning] Can't create test file /var/lib/mysql/n.lower-test
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.981936Z 0 [Warning] Can't create test file /var/lib/mysql/n.lower-test
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.982122Z 0 [ERROR] failed to set datadir to /var/lib/mysql/
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.982142Z 0 [ERROR] Aborting
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.982162Z 0 [Note] Binlog end
  Apr 03 22:26:15 n mysqld[5470]: 2017-04-03T21:26:15.982213Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
  Apr 03 22:26:15 n systemd[1]: 
  </code></pre>
  
  <p>I also tried to login mysql with my detail:
  <code>mysql -uroot -ppassword1</code> which returned </p>
  
  <pre><code>mysql: [Warning] Using a password on the command line interface can be insecure.
  ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
  </code></pre>
  
  <p>when I run the following command <code>ls -la /var/lib/ | grep mysql</code> on the var lib directory it returns.</p>
  
  <pre><code>drwx------ 12 root          docker        4096 Apr  3 21:17 mysql
  drwx------  2 mysql         mysql         4096 Feb 23 22:35 mysql-files
  drwx------  2 mysql         mysql         4096 Feb 23 22:35 mysql-keyring
  drwxr-xr-x  2 root          root          4096 Jan 18 21:45 mysql-upgrade
  </code></pre>
  
  <p>By the looks of things I (well docker did) messed up my ownership on my mysql directory.</p>
  
  <p>If I run <code>ls -la /var/lib/mysql</code>  it returns </p>
  
  <pre><code>ls: cannot open directory '/var/lib/mysql': Permission denied
  </code></pre>
  
  <p>and running same command with sudo <code>sudo ls -la /var/lib/mysql
  </code> it returns </p>
  
  <pre><code>total 188480
  drwx------ 12 root         docker     4096 Apr  3 21:17 .
  drwxr-xr-x 80 root         root       4096 Mar 29 19:28 ..
  -rw-r-----  1 guest-okauxv docker       56 Feb 23 22:35 auto.cnf
  drwxr-x---  2 guest-okauxv docker     4096 Mar 24 23:51 concretepage
  -rw-r--r--  1 guest-okauxv docker        0 Feb 23 22:35 debian-5.7.flag
  drwxr-x---  2 guest-okauxv docker     4096 Mar 25 00:10 myfidser
  drwxr-x---  2 guest-okauxv docker     4096 Mar  4 00:54 myotherFliDB
  drwxr-x---  2 guest-okauxv docker     4096 Mar  1 12:33 testFFAPI
  -rw-r-----  1 guest-okauxv docker      679 Apr  3 21:16 ib_buffer_pool
  -rw-r-----  1 guest-okauxv docker 79691776 Apr  3 21:17 ibdata1
  -rw-r-----  1 guest-okauxv docker 50331648 Apr  3 21:17 ib_logfile0
  -rw-r-----  1 guest-okauxv docker 50331648 Feb 23 22:35 ib_logfile1
  -rw-r-----  1 guest-okauxv docker 12582912 Apr  3 21:17 ibtmp1
  drwxr-x---  2 guest-okauxv docker     4096 Feb 23 22:35 mysql
  drwxr-x---  2 guest-okauxv docker     4096 Mar 25 16:58 NodeRestDB
  drwxr-x---  2 guest-okauxv docker     4096 Feb 23 22:35 performance_schema
  drwxr-x---  2 guest-okauxv docker    12288 Feb 23 22:35 sys
  drwxr-x---  2 guest-okauxv docker     4096 Mar 29 10:34 testDB
  drwxr-x---  2 guest-okauxv docker     4096 Mar  1 11:52 demoDB
  </code></pre>
  
  <p>By the looks of this, I (well docker did) managed to changed the owner and group of all the directories in mysql directory.</p>
  
  <p><strong>Do I need to do a complete reinstall of MySQL Server?</strong></p>
  
  <p><strong>What is the simplest, easiest way to fix this?</strong></p>
  
  <p>your help will be much appreciated.</p>
  
  <p><strong>Updated with FIX</strong></p>
  
  <blockquote>
    <p>Just what Andy Shinn said in point one, I just ran <code>sudo chown -R
    mysql:mysql /var/lib/mysql</code> to change the owner back and started mysql
    by running <code>sudo /etc/init.d/mysql start</code> and mysql returned </p>
    
    <p><code>[ ok ]Starting mysql (via systemctl): mysql.service.</code></p>
  </blockquote>
  
  <p>G</p>