Question

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

  • Posted June 18, 2019
  • MySQL

ramana_sharma@ramana:~$ mysql --version mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using EditLine wrapper ramana_sharma@ramana:~$ mysql -uroot -proot0 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) ramana_sharma@ramana:~$ mysql -u root -p Enter password: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) ramana_sharma@ramana:~$ services mysql start No command ‘services’ found, did you mean: Command ‘service’ from package ‘init-system-helpers’ (main) services: command not found ramana_sharma@ramana:~$ /etc/init.d/mysql start bash: /etc/init.d/mysql: No such file or directory ramana_sharma@ramana:~$ #/usr/sbin/mysqld --defaults-file=/etc/mysql/my.cnf --basedir=/usr --datadir=/var/lib/mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock ramana_sharma@ramana:~$ /etc/init.d/mysql start bash: /etc/init.d/mysql: No such file or directory ramana_sharma@ramana:~$ sudo service mysql restart [sudo] password for ramana_sharma: Failed to restart mysql.service: Unit mysql.service not found. ramana_sharma@ramana:~$ sudo nano /etc/mysql/my.cnf ramana_sharma@ramana:~$ systemctl enable mysql Failed to execute operation: No such file or directory

Subscribe
Share

So I’ve had this issue in my local machines many times, and I reached to a conclusion that: In many cases the socket configuration actually exist with the installation, but the problem is that the process of mysql doesn’t see that or let’s say it isn’t allowed to see it.

To solve this issue you need to stop mysql process either through:

systemctl stop mysql

or

service mysql stop

after than you’ll need to expose the directory which has the socket configurations to the user who is running the process, this can happen through:

sudo usermod -d /var/lib/mysql/ mysql

then go back and start / restart mysql process again through:

systemctl start mysql

or

service start mysql

and you are good to go


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

After tried all options exposed here and a lot hours surfing on Google, I fixed it installing Xampp, 5 minutes and finally I could get a mysql database connect with tableplus from Ubuntu 20.04, in my case I just need MySQL for a development environment. (=P)

This issue happen because MySQL or MariaDB server was not started.

service mysql status

It will show stop. Try starting.

service mysql start

mysql -h 127.0.0.1 -P 3306 -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 8.0.16 MySQL Community Server - GPL

Copyright © 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> show databases; ±-------------------+ | Database | ±-------------------+ | information_schema | | kataragamaKiosk | | mysql | | performance_schema | | sys | ±-------------------+ 5 rows in set (0.63 sec)

mysql>

Hi there @ramanakarasarma,

It sounds like possibly you only have the mysql client installed and not the server:

This should be able to be solved by installing mysql-server, so make sure that you have installed the mysql-server, not the mysql-client or something else.

The error you are seeing means that the file /var/run/mysqld/mysqld.sock doesn’t appear to exist, if you didn’t install mysql-server, then the file would not exist.

You should be able to quickly verify if the mysql server is running locally by doing the following two things:

First, attempt to login to the server with the localhost IP and specific mysql port: mysql -h 127.0.0.1 -P 3306 -u root -p <database>

You can also use telnet to see if anything is being served locally on the mysql port at all: telnet 127.0.0.1 3306

If those don’t work, it is very likely that mysql-server isn’t installed or running. You can install it by using sudo apt-get install mysql-server or similar depending on the linux distro you are using.

However, if the mysql-server is already installed and is running, then you need to check the config files to ensure they are looking for the right sock file:

/etc/my.cnf
/etc/mysql/my.cnf
/var/lib/mysql/my.cnf

In /etc/my.cnf, the socket file may be configured to use /tmp/mysql.sock or similar, but in the /etc/mysql/my.cnf file, the socket file may be set to /var/run/mysqld/mysqld.sock.

So you may also be able to resolve this by removing or renaming /etc/mysql/my.cnf, which will then let mysql use /etc/my.cnf instead.

Hope that helps! - Matt.