5

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...

5
  • 1
  • 2
    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
    Commented Mar 10, 2014 at 1:16
  • 1
    another thing you can check is mysqladmin variables | grep -i socket
    – sites
    Commented Mar 10, 2014 at 1:17
  • 1
    It again gives the same error.. Commented Mar 10, 2014 at 1:42
  • 1
    Check if the service is even running, ps aux | grep mysql
    – Geoffrey
    Commented Sep 29, 2015 at 16:14

7 Answers 7

10

Problem:

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

Solution:

sudo service mysql start
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
1
  • 2
    After changing owner for /var/lib/mysql and later reverting it to mysql, nothing worked. This fixed it.
    – Hans
    Commented Oct 14, 2020 at 4: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
5
  • 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
    Commented Jan 9, 2019 at 8:29
  • I had to upgrade old version, hope it can solve your problem
    – Jay
    Commented Jan 10, 2019 at 1:45
  • This gets even worse: why do you change the file permissions on each restart?
    – Nico Haase
    Commented Jan 10, 2019 at 7: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
    Commented Jan 11, 2019 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
    Commented Jan 11, 2019 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'

1

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.
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

-4

Create one micro instance swap space in Ubuntu

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

mkswap /swapfile

swapon /swapfile

1
  • 1
    Can you explain how this is related to the question after all?
    – Nico Haase
    Commented Jan 9, 2019 at 8:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.