4

I installed (on Ubuntu 13) MYSQL using

sudo apt-get install mysql

But after running

mysql -u root -p

and entering the password it gives the error

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Please guide...

rlandster
  • 7,294
  • 14
  • 58
  • 96
user3382201
  • 51
  • 1
  • 2
  • 4
  • possible duplicate of [error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'](http://stackoverflow.com/questions/11990708/error-cant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysq) – Nico Mar 10 '14 at 01:16
  • 1
    sometimes I have had problems connecting with socket file, try with `mysql -uroot -p -h 127.0.0.1` or `localhost` instead of `127.0.0.1` – sites Mar 10 '14 at 01:16
  • another thing you can check is `mysqladmin variables | grep -i socket` – sites Mar 10 '14 at 01:17
  • It again gives the same error.. – user3382201 Mar 10 '14 at 01:42
  • Check if the service is even running, `ps aux | grep mysql` – Geoffrey Sep 29 '15 at 16:14

7 Answers7

10

Problem:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Solution:

sudo service mysql start
Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
Banti Narvariya
  • 109
  • 1
  • 3
4

I solved it by stopping the apache server first and restarting the mysql server

  1. sudo service apache2 stop
  2. sudo service mysql stop
  3. sudo service mysql start
  4. sudo service apache2 start
  • 2
    After changing owner for `/var/lib/mysql` and later reverting it to `mysql`, nothing worked. This fixed it. – Hans Oct 14 '20 at 04:42
3

Hope this can help you (Env: docker ubuntu16.04 container):

i run this code each time restart mysql server, it work for me:

  sudo chown -R mysql:mysql /var/lib/mysql /var/run/mysqld
  sudo service mysql start
Jay
  • 90
  • 10
  • 1
    Can you explain why that solves the problem? Why should one disable permission checks? Why not start the deamon the usual way? – Nico Haase Jan 09 '19 at 08:29
  • I had to upgrade old version, hope it can solve your problem – Jay Jan 10 '19 at 01:45
  • This gets even worse: why do you change the file permissions **on each restart**? – Nico Haase Jan 10 '19 at 07:21
  • actually, my environment is base on MySQL developed on docker images, it needs to change the file permissions for Non-root user before start MySQL Daemon. – Jay Jan 11 '19 at 10:01
  • can u share your situation(system,env,mysql version ...) or error log, maybe i can give you some advices for that if under my conrol. – Jay Jan 11 '19 at 10:05
2

Make sure that Mysql is installed, still able to reproduce the issue, Look for the file in sql/my.cnf file.

Verify that the Socket connection path in that file:

  • Location shown in the error is: /var/run/mysqld/mysqld.sock

  • Actually location in my.conf file : /home/user/mysql/tmp/mysqld.sock

    home/sql/bin >> mysql -u root -p --socket=/home/user/mysql/tmp/mysqld.sock

In some version's you may find 'mysql.sock' instead 'mysqld.sock'

Community
  • 1
  • 1
Mano
  • 21
  • 4
0

I know it's too late, but I had same problem today. It solved by removing current mysql and reinstalling mariadb. It worked for me.

sudo apt install mariadb-server

Maede
  • 174
  • 12
0

Step 1. Locate a Unix socket file On the server host in the command line, run the following command: mysql -u root -p -h 127.0.0.1 -e "select @@socket"

Step 2. Use whatever result that is generated above as path to the socket

In the command line, run the following command: **mysql -u root -p -S /var/run/mysqld/mysql.sock**
Note: /var/run/mysqld/mysql.sock path might be different on your machine, mine was different since I am using Xampp, just whatver the command 'mysql -u root -p -h 127.0.0.1 -e "select @@socket"' generated.
-3

Create one micro instance swap space in Ubuntu

dd if=/dev/zero of=/swapfile bs=1M count=1024

mkswap /swapfile

swapon /swapfile

Community
  • 1
  • 1
Bhawna
  • 15
  • 2