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

Viewed 2386971

I installed LAMP on Ubuntu 12.04 LTS (Precise Pangolin) and then set root password on phpMyAdmin. I forgot the password and now I am unable to login. When I try to change password through terminal I get:

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

How can I fix this? I am unable to open LAMP, uninstall it or reinstall it.

87 Answers

Check the "bind-adress" parameter in my.cnf.

Else try with the command:

mysql -h 127.0.0.1 -P 3306 -u root -p
  • -h for host 127.0.0.1, that is, localhost

  • -P (notice -P as uppercase) for port 3306, that is, the default port for MySQL

I also facing same problem it will be occur if your mysql server is not running by default it will again stop after some sec so you again run ($ sudo service mysql start ) command you can change if know.

for that use command

$ sudo service mysql start   

(put user password if required because we use sudo ) and then run

$ sudo mysql -u root -p          (put user password if required )

now you got your database

It seems your MYSQL is stopped. use below command to start MySQL again

sudo service mysql start

Fist,try to restart it with

service mysql stop

service mysql start

If above not resolve the issue, now let's go...

Uninstall completely MySQL

sudo apt-get remove --purge mysql\*

reinstall it

sudo apt install mysql-server mysql-client

test if it run

sudo mysql

Install php drivers

sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-cgi php7.4-cli php7.4-common

You can replace php7.4 by php7.x or php8.0.12 or later Very nice !

❗️Be careful, you lose your data if they are not saved. Please backup your data if possible yet.

i solved this problem with restart mysql

/etc/init.d/mysql stop

and

/etc/init.d/mysql start

that's it.

You are missing permission to create /var/run/mysqld directory.So please create and give permission as following.

  • mkdir -p /var/run/mysqld
  • chown mysql:mysql /var/run/mysqld

Try restarting the server with

sudo /usr/local/mysql/support-files/mysql.server start

If there is any error then follow the below steps

  1. mysqld

You will see the the below log. Notice the highlighted portion of the MySQL directory here

mysqld: Can't change dir to '/usr/local/mysql-5.7.14-osx10.11-x86_64/data/' (Errcode: 13 - Permission denied) 2016-10-04T14:09:19.392581Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-10-04T14:09:19.392847Z 0 [Warning] Insecure configuration for --secure-file-priv: Current value does not restrict location of generated files. Consider setting it to a valid, non-empty path. 2016-10-04T14:09:19.392921Z 0 [Note] mysqld (mysqld 5.7.14) starting as process 1402 ... 2016-10-04T14:09:19.397569Z 0 [Warning] Can't create test file
/usr/local/mysql-5.7.14-osx10.11-x86_64/data/Sudharshan.lower-test
2016-10-04T14:09:19.397597Z 0 [Warning] Can't create test file /usr/local/mysql-5.7.14-osx10.11-x86_64/data/Sudharshan.lower-test
2016-10-04T14:09:19.397712Z 0 [ERROR] failed to set datadir to /usr/local/mysql-5.7.14-osx10.11-x86_64/data/
2016-10-04T14:09:19.397776Z 0 [ERROR] Aborting
2016-10-04T14:09:19.397795Z 0 [Note] Binlog end
2016-10-04T14:09:19.397925Z 0 [Note] mysqld: Shutdown complete

  1. sudo chown -R _mysql:_mysql /usr/local/mysql-5.7.14-osx10.11-x86_64

Note the MySQL folder path /usr/local on the the previous log, and in my case it was mysql-5.7.14-osx10.11-x86_64, and you have to update it based on the log you get on your machine to provide read access to the MySQL directory

  1. sudo /usr/local/mysql/support-files/mysql.server start

Starting MySQL
SUCCESS!

I got exactely the same error when I have stopped mysql service, and here how to solve it: by (re)starting mysql using these commands:

sudo systemctl start mysql

or

sudo systemctl restart mysql

For me mysql wasn't running so I fixed it with

sudo /etc/init.d/mysql start

My server storage was full, this was preventing Mysql from starting. Got the idea from here. Increasing the HD and rebooting fixed the issue.

In my case, I'm not able to access mysql and after 3 days research, I have got a solution. This is perfect solution because I have searched /var/run/mysqld/mysqld.sock and I did not find the folder. You can run on putty the commands listed below.

sudo mkdir /var/run/mysqld/
sudo chown -R mysql:mysql /var/log/mysql
sudo mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --socket=/var/run/mysqld/mysqld.sock
sudo /etc/init.d/mysql restart

You will save your valuable time.

In Ubuntu 18:10 Linode 1GB Ram, I experienced this error. After examining /var/log/mysql/error.log , I came across this:

[Note] InnoDB: innodb_empty_free_list_algorithm has been changed to legacy because of small buffer pool size. In order to use backoff, increase buffer pool at least up to 20MB.

I upgraded my linode to 2GB and restarted mariadb with sudo mysql. Next mysql_secure_admin ran, but the root password did not set for the user-as ususl unitl changing the root user to use the mysql_native_password plugin. Not sure, but it appears that the sock was created, but the server shutdown due to the lack of memory in my VPS.

In my case, i had files corrupted in my folder of mysql data

mv /var/lib/mysql /var/lib/mysql_old

so as i had a backup, i moved the directory to "_old", and started the docker again. it created a new folder mysql with clean data, and the socket worked.

Installing mysql-server solved the issue

sudo apt-get install mysql-server

In my case my problem was that I was trying to access from my linux terminal, and not from my docker container with the mysql.

If your server/db was working fine and suddenly this issue appear, then just try restarting your db server. For Centos i do with below command and my issue 'connection refused' 'Cann't connect to local mysql through socket' fixed

sudo systemctl start mysqld

This answer is likely to drown here, but maybe somebody stumbles upon it accidentally.

In my case, SELinux prevented the user/application from connecting to the MySQL (MariaDB) server socket. On RHEL, check /var/log/audit/audit.log if you have SELinux enabled.

SOLUTION 1 Purge out (or remove if you want to keep databases) any mysql packages to repeat the installation anew:

sudo apt purge "mysql*"

-Autoremove packages

sudo apt autoremove

-Stop the apparmor service

sudo systemctl stop apparmor

-Make apparmor drop its profiles (I thought with stop it was enough, but for me it wasn't. With systemctl it doesn't work)

sudo service apparmor teardown

-Reinstall mysql-server

sudo apt install mysql-server

-Install apparmor-utils, to create a profile for mysql in apparmor that allows mysql to run

sudo apt install apparmor-utils

-Check the status of mysql-server (must be Active(running))

sudo systemctl status mysql

-Generate a profile for mysql in apparmor

sudo aa-genprof mysql

-In other terminal run mysql (enter root password)

mysql -u root -p

-do things in MySQL while apparmor is generating the profile in the other terminal

mysql> CREATE DATABASE fooDB

-Swith to the other terminal and press "s" (the prompt tells you it's for "scan")

-Say yes to the policies from apparmor you see fit (I guess all of them for mysql), say yes pressing "a" for Allow

-Press "f" to Finish the apparmor profile

-Restart the apparmor service

sudo systemctl start apparmor

-Check to see if you still can use mysql in the other terminal

mysql>exit

mysql -u root -p

If all is well you can use mysql from the command line.

In my case I was missing mysql-server. So after installing it via sudo apt-get install mysql-server I was able to connect again.

Another cause of this issue, surprisingly, is the deletion of the folder /var/log/mariadb This was my case after i deleted this log folder. After the creation, the error disappeared.

I had previously this problem in Kubuntu 20.04. How It solved for me was to just run:

sudo dpkg --configure -a

The cause was that dpkg had crashed.

I had the problem other time, and I solved it by running:

sudo /etc/init.d/mysql start

Think the service was stopped.

For me, on CentOS, I had to restart the mariadb service like this :

systemctl start mariadb.service

I faced same issue and the problem was that mysql-server was installed but it was not running.

Try, service mysql start Or systemctl start mysql

You can check the logs /var/log/mysql.log

for me it was not working bcs of this error:

Cannot allocate memory for the buffer pool error

I added this line in /etc/my.cnf and started service:

innodb_buffer_pool_size=100M

This issue happen because MySQL server was not started. check active status using below command.

service mysql status

If the active status is stop, then ry starting the MySQL server,

service mysql start

I was facing this issue in termux for mariadb, which resolved my issue is these following two lines of command -

pkill mysql

mysqld_safe -u root

Related