[mysql] error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' -- Missing /var/run/mysqld/mysqld.sock

My problem started off with me not being able to log in as root any more on my mysql install. I was attempting to run mysql without passwords turned on... but whenever I ran the command

# mysqld_safe --skip-grant-tables &

I would never get the prompt back. I was trying to follow these instructions to recover the password.

The screen just looks like this:

root@jj-SFF-PC:/usr/bin# mysqld_safe --skip-grant-tables
120816 11:40:53 mysqld_safe Logging to syslog.
120816 11:40:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

and I don't get a prompt to start typing the SQL commands to reset the password.

When I kill it by pressing CTRL + C, I get the following message:

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

Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

If I retry the command and leave it long enough, I do get the following series of messages:

root@jj-SFF-PC:/run/mysqld# 120816 13:15:02 mysqld_safe Logging to syslog.
120816 13:15:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120816 13:16:42 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

[1]+  Done                    mysqld_safe --skip-grant-tables
root@jj-SFF-PC:/run/mysqld#

But then if I try to log in as root by doing:

# mysql -u root

I get the following error message:

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

I checked and /var/run/mysqld/mysqld.sock file doesn't not exist. The folder does, but not the file.

Also, I don't know if this helps or not, but I ran find / -name mysqld and it came up with:

/var/run/mysqld - folder
/usr/sbin/mysqld - file
/run/mysqld - folder

I'm new to Linux and MySQL, so I don't know if this is normal or not. But I'm including this info just in case it helps.

I finally decided to uninstall and reinstall mysql.

apt-get remove mysql-server
apt-get remove mysql-client
apt-get remove mysql-common
apt-get remove phpmyadmin

After reinstalling all packages again in the same order as above, during the phpmyadmin install, I got the same error:

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

So I tried again to uninstall/reinstall. This time, after I uninstalled the packages, I also manually renamed all mysql files and directories to mysql.bad in their respective locations.

/var/lib/mysql 
/var/lib/mysql/mysql
/var/log/mysql
/usr/lib/perl5/DBD/mysql
/usr/lib/perl5/auto/DBD/mysql
/usr/lib/mysql
/usr/bin/mysql
/usr/share/mysql
/usr/share/dbconfig-common/internal/mysql
/etc/init.d/mysql
/etc/apparmor.d/abstractions/mysql
/etc/mysql

Then I tried to reinstall mysql-server and mysql-client again. But I've noticed that it doesn't prompt me for a password. Isn't it supposed to ask for an admin password?

This question is related to mysql linux ubuntu

The answer is


you can find mysqld.sock in /var/run/mysqld if you have already installed mysql-server by sudo apt-get install mysql-server


This was mentioned a couple of times already, but this worked immediately for me:

service mysql restart


on archlinux I faced this error and the problem was that the mysqld-service wasn't running.

However I couldn't enable the mysql-service with systemctl start mysqld as instructed in the Archlinux documentation. I think the error was something like

mysqld.service not found

I first needed to restart the system.

Then I entered the command again (and entered systemctl enable mysqld) and now mysql was able to find the created mysql.sock.


If you have a lot of databases and tables on your system, and if you have innodb_file_per_table set in my.cnf, then your mysql server might have run out of opened objects / files (or rather the descriptors for these objects) Set a new max number with

open-files-limit = 2048

and restart mysql. This approach might help when the socket is not created at all, but really this might not not be the real problem, there is an underlying problem.


In My case two mysqld processes were running.. killed the optional processs by using pkill -9 mysqld



There is a lots of reason for this issue, but sometimes just restart the mysql server, it will fix the issue.

sudo service mysql restart

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

solutions

finally uninstall and reinstall mysql. **

  • sudo apt-get remove mysql-server
  • sudo apt-get remove mysql-client
  • sudo apt-get remove mysql-common
  • sudo apt-get remove phpmyadmin

then install again by

  • sudo apt-get install mysql-server-5.6

After this operation, 164 MB of additional disk space will be used.

  • Do you want to continue? [Y/n] Y press YES for complete installations

...... .......

  • At last you will get these lines....

    Setting up libhtml-template-perl (2.95-1) ...

    Setting up mysql-common-5.6 (5.6.16-1~exp1) ... Processing triggers for libc-bin (2.19-0ubuntu6) Processing triggers for ureadahead (0.100.0-16) ...

  • And then

    root@ubuntu1404:~# mysql -u root -p (for every password first u should use )

  • Enter password:

  • Note :Entered password should be same as the installation time password of mysql(like .root,system,admin,rahul etc...)

    Then type

  • USE rahul_db(database name);

Thanks.**


locate the my.cnf file with the mysql config mine was under /etc/mysql/my.cnf (change the location where it says socket accordingly)

port            = 3306
socket          = /tmp/mysql.sock

Here is entries for some specific programs The following values assume you have at least 32M ram

This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]
socket          = /tmp/mysql.sock
nice            = 0

I faced same error and found that it was due to upgradation of packages, So after restarting my system I resolved error.

I think due to sql libraries/ packages update that error occured, So try this if you are doing some upgrading :)


I tried almost all the listed solutions, none worked for me until I restarted the machine and then mysql server restarted when I issued the command "service mysql restart".


First create dir /var/run/mysqld

with command:

mkdir -p /var/run/mysqld

then add rigths to the dir

chown mysql:mysql /var/run/mysqld

after this try

mysql -u root

I am using XAMPP on Ubuntu. I found this error when connecting database through terminal. I solve it without any configuration because default socket file path in XAMPP is written in "/opt/lampp/etc/my.cnf" as following:

[client]
#password   = your_password
port        = 3306
socket      = /opt/lampp/var/mysql/mysql.sock

now you can connect just by giving this socket path parameter with mysql command on terminal like:

mysql -u root --socket /opt/lampp/var/mysql/mysql.sock

and it's done without any configuration.

If you don't want to type socket path everytime, then go for changing default path in my.cnf by "/var/run/mysqld/mysqld.sock". Provide permissions and restart mysql server.


There is a bug on Ubuntu with MySQL 5.6 and 5.7 where var/run/mysqld/ would disappear whenever MySQL service stopped or is rebooted. This prevents MySQL from running at all. Found this workaround, which isn't perfect, but at least it gets it running after stopping/reboot:

mkdir /var/run/mysqld/
chown mysqld /var/run/mysqld/

Why getting this error

I received new updates of mysql libraries so i updated my Kubuntu OS after that getting these errors.


Commands i tried and how i fixed it.

MySql-server is running correctly but when i tried to connect its giving

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'.

I checked /var/run/mysqld/mysqld.sock'. this directory. My files did not existed.

I also tried these commands to connect but did not worked for me.

 mysql -h 127.0.0.1 -P 3306 -u root -p

 sudo service mysql start

After wasting round about 2 hours i found the solution

sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -f

After that everything fixed for me.


Make sure your inaccessible socket file path is same as '/var/run/mysqld/mysqld.sock', otherwise change the path as yours. Stop the mysqld

$ sudo /etc/init.d/mysqld stop

If the process still runing;

$ sudo pkill -9 mysqld

Remove the mysql directory where socket going to create. For me it did not allowed to remove, so I had to forcefully remove.

$ sudo mkdir -p /var/run/mysqld

Set the ownership to the dirctory

$ sudo chown mysql:mysql /var/run/mysqld

Start mysql

$ sudo /etc/init.d/mysql start

Trying to connect mysql

$ sudo mysql -u dbuser -p

I think your MySQL server has not started. So start the server using one of the following commands.

#services mysql start

or

#/etc/init.d/mysql start

The solution is way easier.

  1. First, you have to locate(in Terminal with "sudo find / -type s") where your mysql.sock file is located. In my case it was in /opt/lampp/var/mysql/mysql.sock
  2. Fire up Terminal and issue sudo Nautilus
    This starts your Files manager with super user privileges
  3. From Nautilus navigate to where your mysql.sock file is located
  4. Right click on the file and select Make Link
  5. Rename the Link File to mysqld.sock then Right click on the file and Cut it
  6. Go to /var/run and create a folder called mysqld and enter it
  7. Now right click and Paste the Link File
  8. Voila! You will now have a mysqld.sock file at /var/run/mysqld/mysqld.sock :)

I would also check the mysql configuration. I was running into this problem with my own server but I was a bit too hasty and misconfigured the innodb_buffer_pool_size for my machine.

innodb_buffer_pool_size = 4096M

It usually runs fine up to 2048 but I guess I don't have the memory necessary to support 4 gigs.

I imagine this could also happen with other mysql configuration settings.


Just Need to Start MySQL Service after installation:

For Ubuntu:

sudo service mysql start;

For CentOS or RHEL:

sudo service mysqld start;

Using XAMPP on ubuntu:

  1. Create a folder called mysqld inside /var/run directory. You can accomplish that using the command sudo mkdir /var/run/mysqld.

  2. Create a symbolic link to mysql.sock file that is created by the XAMPP server when it is started. You can use the command sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock.

Note: The mysql.sock file is created when the server is started and removed when the server is stopped, so sometimes the link you created might appear to be broken but it should work as long as you have started the server using either sudo /opt/lampp/lampp start or any other means.

  1. Start the server if it's not already running and try executing your program again.

Good luck! I hope you'll get away with it this time.


In my case the problem was the bind addresses in /etc/mysql/my.cnf, then:

nano /etc/mysql/my.cnf

search bind addresses and remove the specific with the host ip (not the 127.0.0.1)


My solution;

Ubuntu 18.04 (WSL)

/etc/mysql/my.cnf

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

/etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306

I changed the port. It's worked for me. You can write another port. Example 3355


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

Sometimes it is due to when mysql server goes down , in live server i think you should have to restart server and also in localhost you need to restart mysql server .


In lampp as i recognize it does not identify mysql commands. try with

sudo /opt/lampp/bin/mysql

it will open the mysql terminal where you can communicate with databases..


I had this type of problem when i resize the droplet on digitalocean. I had upgrade the mysql version and works for me. Here is the tutorial what i applied - https://serverpilot.io/community/articles/how-to-upgrade-mysql-5.5-to-5.6-on-ubuntu-14.04.html


Okay just copy and paste these codes: This should be done in the terminal, inside a server, when your mysql database is not properly installed, and when you are getting this error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.

Stop MySql

sudo /etc/init.d/mysqld stop

Restart it or start it

sudo /etc/init.d/mysqld restart or sudo /etc/init.d/mysqld start

Make a link like this and give it to the system

ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

Run a secure installation which guides all the process you need to do to configure mysql

/usr/bin/mysql_secure_installation

my situation is that the mysql in the /etc/ directory is renamed mysql_bk, just copy mysql_bk

sudo cp -r /etc/mysql_bk/ /etc/mysql/

then

service mysql restart

I had the exactly same issue. After struggling for an hour, I found a way of correcting it without reinstalling mysql-common, mysql-client, mysql-server.

First of all, go to "/var/run/mysqld". You will find that the mysql.sock does not exist. Simply remove the entire mysqld directory and recreate it and provide it necessary privileges.

# rm -rf /var/run/mysqld && mkdir /var/run/mysqld && chown mysql /var/run/mysqld/

Now, Kill the mysql process in it's entirety. It might be possible that it will show you "waiting for page cleaner" on running "/etc/init.d/mysql status" command even after shutting down the service.

To completely close the service, use

# pkill -9 mysqld

Once the process is killed, try starting it again using

# /etc/init.d/mysql start

And you will see that it works good! And also there will be no issue in stopping it too.


Try this command,

sudo service mysql start

This error occurs due to multiple installations of mysql. Run the command:

ps -A|grep mysql

Kill the process by using:

sudo pkill mysql

and then run command:

ps -A|grep mysqld

Also Kill this process by running:

sudo pkill mysqld

Now you are fully set just run the following commands:

service mysql restart
mysql -u root -p

Have very well working mysql again


The answer of the user load step worked for me. Sometimes is need edit the file in /etc/mysql/my.cnf add line to client

[client]
password = your_mysql_root_password
port  = 3306
host  = 127.0.0.1
socket  = /var/lib/mysql/mysql.sock

This solution can help others, sometimes the fix is very simple.

In your live environment restart mysql server and apache

sudo service mysql restart
sudo apache services restart

this is a common problem when you install a plugin or execute any database commands.


The mysql.sock file is created when MariaDB starts and is removed when MariaDB is shutdown. It won't exist if MariaDB is not running. maybe you didn't install MariaDB. YOU COULD FOLLOW THE INSTRUCTION BELOW: https://www.linode.com/docs/databases/mariadb/how-to-install-mariadb-on-centos-7 BEST


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

in /etc/my.cnf add this lines:

[client]
socket=/var/lib/mysql/mysql.sock <= this path should be also same as is[mysqld]

And restart the service with: service mysql restart

this worked for me


Same here on Ubuntu 18.04

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

  1. mysqld.sock= actually resides under folder /run/mysqld/
  2. ?> / ll /var/run
    lrwxrwxrwx 1 root root 4 Nov 1 2018 /var/run -> /run/

hmmm... Does that mean a symbolic link won't work? That's weird...


Temporary Solution

Maybe someone facing this problem. I am using Mysql Workbench on Ubuntu 14 and got this error.

mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect

Find your socket file by running sudo find / -type s, in my case it was /run/mysqld/mysqld.sock

So, I just created a link to this file in tmp directory.

sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

Please note that this is a temporary solution since the file created will be under /tmp. See other answers for a permanent solution.


I had similar problem on a CentOS VPS. If MySQL won't start or keeps crashing right after it starts, try these steps:

1) Find my.cnf file (mine was located in /etc/my.cnf) and add the line:

innodb_force_recovery = X

replacing X with a number from 1 to 6, starting from 1 and then incrementing if MySQL won't start. Setting to 4, 5 or 6 can delete your data so be carefull and read http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html before.

2) Restart MySQL service. Only SELECT will run and that's normal at this point.

3) Dump all your databases/schemas with mysqldump one by one, do not compress the dumps because you'd have to uncompress them later anyway.

4) Move (or delete!) only the bd's directories inside /var/lib/mysql, preserving the individual files in the root.

5) Stop MySQL and then uncomment the line added in 1). Start MySQL.

6) Recover all bd's dumped in 3).

Good luck!


Try the following at a terminal prompt:

sudo mysql

Once that lets you in, you can create a new user and grant privileges that you want on the specific database they need access to.

Mysql 5.7 changed some things and by default uses the auth_socket plugin (as opposed to mysql_native_password) for root to protect the account from getting hacked. You can override this by setting the plugin field for root, but unless you have a very good reason you probably shouldn't circumvent the protection. Especially when sudo mysql is easier than mysql -u root -p anyway.

I found out this info - of all places - from a Raspberry Pi help site. Worked like a charm after Lubuntu 18.04 annoyed the heck out of me for a couple hours.


Sometimes it's just that you don't have enough space on server, just free up some space (like clearing log files), and start mysql:

/etc/init.d/mysql start


Changing the host to 127.0.0.1 worked for me.

Edit the file in /etc/mysql/my.cnf and add the below mentioned line to the section: client

[client]
port  = 3306
host  = 127.0.0.1
socket  = /var/lib/mysql/mysql.sock

After you are done with it. Execute the following command.

sudo service mysql start

I just had this problem on Ubuntu 14.10

Turns that mysql-server was no longer installed (somehow it had been removed) but I couldn't just install it because there were some broken packages and dependency issues/conflicts.

In the end I had to reinstall mysql

sudo apt-get remove mysql-client
sudo apt-get install mysql-server

I run my MySQL on a virtual machine in Ubuntu, So what had happened was when I restarted my host and the VM, The IP had changed. I had configured mysql to run on IP 192.168.0.5 and now due to dynamic allocation of IP, my new IP was 192.168.0.8

If you have the same problem just check your ip with the command ifconfig.

Check your MySQL binding with the command cat /etc/mysql/my.cnf | grep bind-address

If both IP are the Same, then reinstall your mysql server

If not, then change your IP in /etc/network/interfaces using nano, vi, vim or anything of your preference.

I prefer sudo nano /etc/network/interfaces

and enter the following

auto eth0
iface eth0 inet static
address 192.168.0.5
netmask 255.255.255.0

Save the interfaces file, restart your interface sudo ifdown eth0 && sudo ifup eth0 replace "eth0" with your network interface

Restart MySQL sudo service mysql stop followed by sudo service mysql start

If you have the same issue as mine, You are good to go!


Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to ubuntu

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found