Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Asked Answered
S

38

314

I am getting the following error when I try to connect to mysql:

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

Is there a solution for this error? What might be the reason behind it?

Sensational answered 15/12, 2010 at 9:33 Comment(2)
I have answered this here, please check the below link: https://mcmap.net/q/66347/-error-39-can-39-t-connect-to-local-mysql-server-through-socket-39-var-run-mysqld-mysqld-sock-39-2-39-missing-var-run-mysqld-mysqld-sockAsbestos
Just got such and issue. reboot fixed it. However, I couldn't understand it.Preside
B
275

Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.

Bailly answered 15/12, 2010 at 9:47 Comment(7)
In that case, you should probably check if your MYSQL server is actually running.Gallion
You can also force using a socket with the socket parameter (-S with /usr/bin/mysql) and force TCP/IP by providing a port (-P with /usr/bin/mysql.)Boisterous
Using a fully qualified network name other than localhost worked for me. Just make sure it's declared in the client side 'hosts' file.Hanoi
Note to cygwin users: if the cygwin mysql client tries to hook to a local WAMP mysql server for instance, use -h <hostname other than localhost>. Don't try to create a mysql.sock file and declare it in whatever my.cnf file, it won't work.Hanoi
A wrong hostname did give me the exact same error. Looking at the /etc/hosts file in my docker container showed me the MySql hostname to be mysql. Using this same hostname in my connect configuration resolved the issue.Felsite
Above answer didn't help me . So if you have mariadb on Centos, like me, run service mariadb startAntibes
perfect answer, thanks! I'm running mariadb in a container with a non standard host port mapping. Even though I specified the port to use in .my.cnf, the client still tried using the socket. Changing host from localhost to 127.0.0.1 in .my.cnf was the fix I needed.Breda
S
212

Ensure that your mysql service is running

service mysqld start

Then, try the one of the following following:

(if you have not set password for mysql)

mysql -u root

if you have set password already

mysql -u root -p
Sequential answered 13/12, 2012 at 14:1 Comment(2)
on newer releases of debian: service mariadb start because mariadb.org/debian-9-released-mariadb-mysql-variantEichelberger
This was the answer for me. Since i'm using mariadb the solution was to run the following: "sudo systemctl start mariadb". All was well after that.Flowery
V
34

If your file my.cnf (usually in the etc folder) is correctly configured with

socket=/var/lib/mysql/mysql.sock

you can check if mysql is running with the following command:

mysqladmin -u root -p status

try changing your permission to mysql folder. If you are working locally, you can try:

sudo chmod -R 777 /var/lib/mysql/

that solved it for me

Ventriloquist answered 10/11, 2011 at 19:4 Comment(4)
It works for me also, but is after i upgraded to the lastest version then appear this problem, may i know why this happen?Cummerbund
sudo chmod -R 777 /var/lib/mysql/ Worked for me with an issue I had in starting a docker container mysqlPhonography
You should not make your mysql data directory world writeable! /var/lib/mysql should be 750 and move the socket file to another directory (eg. set socket=/tmp/mysql.sock in /etc/my.cnf [mysqld] and [client] sections). Or at least set /var/lib/mysql to 755Wilt
Thank you so much for this. sudo chmod -R 777 /var/lib/mysql/ worked for me and i was able to save a ton of time and effort.Disquietude
K
24

The MySQL server is not running, or that is not the location of its socket file (check my.cnf).

Kaceykachina answered 15/12, 2010 at 9:34 Comment(2)
I had the problem described above and this was the problem. Thanks.Vitascope
Where would that be for Homebrew install?Mathewmathews
T
24

If you are on a recent RHEL, you may need to start mariadb (an open source mysql db) instead of the mysql db:

yum remove mysql
yum -y install mariadb-server mariadb
service mariadb start

You should then be able to access mysql in the usual fashion:

mysql -u root -p
Tweak answered 26/5, 2016 at 23:8 Comment(1)
FYI this works for CentOS 8.1 as well. After install mariadb-server and mariadb, don't forget to run mysql_secure_installation to clean up some default unsafe settings.Keeping
R
23

Most likely mysql.sock does not exist in /var/lib/mysql/.

If you find the same file in another location then symlink it:

For ex: I have it in /data/mysql_datadir/mysql.sock

Switch user to mysql and execute as mentioned below:

su mysql

ln -s /data/mysql_datadir/mysql.sock /var/lib/mysql/mysql.sock

That solved my problem

Rabat answered 21/2, 2012 at 20:29 Comment(4)
renaming mysql.sock to something else, then restarting mysqld ("service mysqld restart"), then renaming something else back to mysql.sock worked for me. A slight variation on this answer, so thanks.Assimilation
This answer may help locating the mysql.sock fileCherisecherish
These literal commands did not solve the problem for my specific use case, but the concept of using a symlink to the .sock did indeed!Baudekin
symlink worked for me -- I too had mysql data directory in a non-standard dir pathFraktur
S
23

Just edit /etc/my.cnf Add following lines to my.cnf

[mysqld]

socket=/var/lib/mysql/mysql.sock 

[client]

socket=/var/lib/mysql/mysql.sock

Restart mysql and connect again

mysql -u user -p password database -h host;

Stucco answered 13/11, 2013 at 17:31 Comment(1)
The only correct answer. I wonder why it doesn't have enough up votes.Misbegotten
P
15

In my case I have moved socket file to another location inside /etc/my.cnf from /var/lib/mysql/mysql.sock to /tmp/mysql.sock

Even after restarting the mysqld service, I still see the error message when I try to connect. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

The problem is with the way that the client is configured. Running diagnostics will actually show the correct socket path. eg ps aux | grep mysqld

Works:

mysql -uroot -p -h127.0.0.1
mysql -uroot -p --socket=/tmp/mysql.sock

Does not Work:

mysql -uroot -p
mysql -uroot -p -hlocalhost

You can fix this problem by adding the same socket line under [client] section inside mysql config.

Paleoclimatology answered 21/10, 2013 at 17:23 Comment(0)
A
14

Check if your mysqld service is running or not, if not run, start the service.

If your problem isn't solved, look for /etc/my.cnf and modify as following, where you see a line starting with socket. Take a backup of that file before doing this update.

socket=/var/lib/mysql/mysql.sock  

Change to

socket=/opt/lampp/var/mysql/mysql.sock -u root
Announce answered 13/6, 2011 at 8:21 Comment(1)
Hi Sekar, I entered "find / -name my.cnf", and i got two locations. 1)/opt/lampp/etc/my.cnf 2)/etc/mysql/my.cnf. So, which one to use.. I asked question here.. #32438296Benito
K
10

MariaDB, a community developed fork of MySQL, has become the default implementation of MySQL in many distributions.

So first you should start,

$ sudo systemctl start mariadb

If this fails rather try,

$ sudo systemctl start mysqld

Then to start mysql,

$ mysql -u root -p

As of today, in Fedora the package is named mariadb And in Ubuntu it is called mariadb-server.

So you may have to install it if its not already installed in your system.

Kermitkermy answered 23/10, 2016 at 12:18 Comment(0)
W
7

Make sure you have enough space left in /var. If Mysql demon is not able to write additional info to the drive the mysql server won't start and it leads to the error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Consider using

expire_logs_days = 10
max_binlog_size = 100M

This will help you keep disk usage down.

Windjammer answered 14/5, 2013 at 15:37 Comment(4)
Thanks, I am running mysql on oracle virtualbox and when I made a dump of my bd, i run out of space, and couldn't get mysql server to start, when i deleted the dump file i was able to restart mysql server.Containment
This solved it for me. I had innodb_buffer_pool_size set to a very high value for production environments and it made mysql crash on my virtual machine.Chappy
I had this problem when using phabricator and the solution was to change innodb_buffer_pool_size in my.cnf. Setting it to a small value like innodb_buffer_pool_size = 50M is a good test to discard this hypothesis ;)Cicero
That was the freaking problem, checked everything but disk space. Freed space from /var, it came back!Monopolist
I
6

Make sure you started the server:

mysql.server start

Then connect with root user:

mysql -uroot
Insnare answered 31/3, 2014 at 22:7 Comment(0)
R
5

Please check whether another mysql service is running.

Rubinstein answered 15/6, 2013 at 17:45 Comment(0)
B
5

Here's what worked for me:

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
service mysqld restart
Budgie answered 25/2, 2015 at 0:20 Comment(0)
B
4

One way to reproduce this error: If you meant to connect to a foreign server but instead connect to the non existent local one:

eric@dev ~ $ mysql -u dev -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through 
socket '/var/lib/mysql/mysql.sock' (2)
eric@dev ~ $

So you have to specify the host like this:

eric@dev ~ $ mysql --host=yourdb.yourserver.com -u dev -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 235
Server version: 5.6.19 MySQL Community Server (GPL)

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

mysql> show databases;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| mysql                   |
| performance_schema      |
+-------------------------+
3 rows in set (0.00 sec)

mysql> exit
Bye
eric@dev ~ $
Betty answered 28/7, 2014 at 14:12 Comment(0)
P
4

if you change files in /var/lib/mysql [ like copy or replace that ], you must set owner of files to mysql this is so important if mariadb.service restart has been faild

chown -R mysql:mysql /var/lib/mysql/*

chmod -R 700 /var/lib/mysql/*

Piper answered 12/11, 2015 at 22:11 Comment(0)
I
3

try

echo 0 > /selinux/enforce
Imide answered 16/9, 2013 at 16:20 Comment(1)
My problem is SELinux related but this doesnt solve it.Bennet
H
2

If your mysql was previously working and has stopped suddenly just "reboot" the server.

Was facing this issue on my CentOS VPS.->

Was constantly getting

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

Tried all techniques, finally restarting the server fixed the issues ->

shutdown -r now

Hope this helps !!

Hoarfrost answered 12/11, 2012 at 5:15 Comment(0)
G
2

First enter "service mysqld start" and login

Ghislainegholston answered 7/5, 2013 at 3:30 Comment(2)
what do you want to say? please read the faq before asking question hereWimberly
This question was successfully answered three years ago. This exact answer was given more completely last year.Klepac
S
2

It worked for me with the following changes

Whatever path for socket is mentioned in [mysqld] and same in [client] in my.cnf and restart mysql

[mysqld] socket=/var/lib/mysql/mysql.sock

[client] socket=/var/lib/mysql/mysql.sock

Stucco answered 13/2, 2014 at 4:13 Comment(0)
S
2

Please ensure you have installed MySQL server correctly, I met this error many times and I think it's complicated to debug from the socket, I mean it might be easier to reinstall it.

If you are using CentOS 7, here is the correct way to install it:

First of all, add the mysql community source
yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

Then you can install it by yum install mysql-community-server

Start it with systemctl: systemctl start mysqld

Solitary answered 13/8, 2015 at 9:32 Comment(0)
M
1

Note that while mysql reads the info of the location of the socketfile from the my.cnf file, the mysql_secure_installation program seems to not do that correctly at times.

So if you are like me and shuffle things around at installationtime you might get into the situation where you can connect to the database with mysql just fine, but the thing can not be secured (not using that script anyway).

To fix this the suggestion from sreddy works well: make a softlink from where the script would expect the socket to where it actually is. Example:

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

(I use /tmp/ as a default location for sockets)

Marthena answered 14/10, 2013 at 21:50 Comment(0)
B
1

This might be a stupid suggestion but make 100% sure your DB is still hosted at localhost. For example, if a Network Admin chose (or changed to) Amazon DB hosting, you will need that hostname instead!

Bangle answered 15/4, 2016 at 15:52 Comment(1)
I am using the hosting provider 1and1 and got this error after ssh-ing to the host. The fix was simply to provide the host name "mysql -u dbo70123521 -p --host db70313321.db.1and1.com db703133520"Vickyvico
L
1

In my case, I was importing a new database, and I wasnt able to connect again after that. Finally I realized that was a space problem.

So you can delete the last database and expand you hard drive or what I did, restored a snapshot of my virtual machine.

Just in case someone thinks that is useful

Lafreniere answered 21/1, 2019 at 9:20 Comment(0)
C
1

I came to this issue when i reinstall mariadb with yum, which rename my /etc/my.cnf.d/client.cnf to /etc/my.cnf.d/client.cnf.rpmsave but leave /etc/my.cnf unchanged.

For I has configed mysqld's socket in /etc/my.cnf, and mysql's socket in /etc/my.cnf.d/client.cnf with customized path.

So after the installation, mysql client cannot find the mysql's socket conf, so it try to use the default socket path to connect the msyqld, which will cause this issue.

Here are some steps to locate this isue.

  1. check if mysqld is running with ps -aef | grep mysqld
$ps -aef | grep mysqld | grep -v grep
mysql    19946     1  0 09:54 ?        00:00:03 /usr/sbin/mysqld
  1. if mysqld is running, show what socket it use with netstat -ln | grep mysql
$netstat -ln | grep mysql
unix  2      [ ACC ]     STREAM     LISTENING     560340807 /data/mysql/mysql.sock
  1. check if the socket is mysql client trying to connect. if not, edit /etc/my.conf.d/client.cnf or my.conf to make the socket same with it in mysqld
[client]
socket=/data/mysql/mysql.sock

You also can edit the mysqld's socket, but you need to restart or reload mysqld.

Clairvoyance answered 4/12, 2020 at 2:58 Comment(0)
D
0

Just rain into the same problem -- and here's how I addressed it.

Assuming mysqld is running, then the problem might just be the mysql client not knowing where to look for the socket file.

The most straightforward way to address this consists in adding the following line to your user's profile .my.cnf file (on linux that's usually under /home/myusername):

socket=<path to the mysql socket file>

If you don't have a .my.cnf file there, then create one containing the following:

[mysql]
socket=<path to the mysql socket file>

In my case, since I moved the mysql default data folder (/var/lib/mysql) in a different location (/data/mysql), I added to .my.cnf the following:

[mysql]
socket=/data/mysql/mysql.sock

Hope this helps.

Date answered 26/8, 2013 at 6:27 Comment(0)
D
0

My problem was that I installed mysql successfully and it worked fine.

But one day, the same error occurred.

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

And no mysql.sock file existed.

This sollution solved my problem and mysql was up and running again:

Log in as root:

sudo su -

Run:

systemctl stop mysqld.service
systemctl start mysqld.service
systemctl enable mysqld.service

Test as root:

mysql -u root -p

mysql should now be up and running.

I hope this can help someone else as well.

Dunkle answered 3/10, 2013 at 9:30 Comment(0)
A
0

ran into this issue while trying to connect mysql in SSH client, found adding the socket path to the command helpful when switching between sockets is necessary.

> mysql -u user -p --socket=/path/to/mysql5143.sock
Aglaia answered 24/1, 2014 at 14:47 Comment(0)
P
0

This is a problem if you are running out of disk space. Solution is to free some space from the HDD.

Please read more to have the explanation :

If you are running MySQL at LINUX check the free space of HDD with the command disk free :

 df 

if you are getting something like that :

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2              5162828   4902260         0 100% /
udev                    156676        84    156592   1% /dev
/dev/sda3              3107124     70844   2878444   3% /home

Then this is the problem and now you have the solution!

Since mysql.sock wants to be created at the mysql folder which is almost always under the root folder could not achieve it because lack of space.

If you are periodicaly give the ls command under the mysql directory (at openSUSE 11.1 is at /var/lib/mysql) you will get something like :

hostname:/var/lib/mysql #
.protected  IT     files        ibdata1             mysqld.log  systemtemp
.tmp        NEWS   greekDB      mysql               mysqld.pid  test
ARXEIO      TEMP1  ib_logfile0  mysql.sock          polis
DATING      deisi  ib_logfile1  mysql_upgrade_info  restore

The mysql.sock file appearing and disappearing often (you must to try allot with the ls to hit a instance with the mysql.sock file on folder).

This caused by not enough disk space.

I hope that i will help some people!!!! Thanks!

Po answered 13/2, 2014 at 19:49 Comment(0)
Q
0

I had to disable explicit_defaults_for_timestamp from my.cnf.

Quadrumanous answered 1/6, 2014 at 10:13 Comment(0)
P
0

Try first 2, 3 solutions. Error is stil popup & If you can not find /var/lib/mysql/mysql.sock

find /var/ -name mysql.sock

Check the space available in /var/

df

If the directory is full remove some unusefull files/directories

rm /var/cache/*

Probably your issue will sorted now.

Principate answered 13/2, 2015 at 15:56 Comment(0)
J
0

If you are in the shell of sf.net, try:

mysql --host=mysql-{LETTER} --user={LETTER}{GROUP ID}admin -p

Change {LETTER} and {GROUP ID} as shown in your MySQL Database of project admin profile.

Jealous answered 3/1, 2016 at 18:49 Comment(0)
L
0

I don't find any solutions from all the answers above, I do have mariadb installation that was installed from source in Debian 11 (not from Debian apt package). So whenever I trigger mysql command in terminal, I will get this error:

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

and the mysql error log only shows the same error socket above which was hard for me to know the reason behind this

What I did was to remove mailutils package in my distro that has mariadb dependencies files in it.

So, I just need to trigger these 2 commands

apt -y remove mailutils
apt -y autoremove

and when I run mysql command in terminal, I don't have that socket error anymmore

Leathaleather answered 22/10, 2021 at 6:1 Comment(0)
I
0

I'm using MyXQL to access the database in Elixir. My actual error was using /tmp/mysql.sock, when the /etc/my.conf was using /var/lib/mysql/mysql.sock.

The solution for me was to set an environment variable in the Elixir setup:
export MYSQL_UNIX_PORT=/var/lib/mysql/mysql.sock so that MyXQL would not use its default location.

The principle of this solution is that you can redirect MyXQL to another location to avoid this type of problem.

Imprecision answered 18/7, 2022 at 15:46 Comment(0)
S
0

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' it can be solve by two way

  1. using short link
  2. mention socket file link in the client and server file.

Method 1: To do link you can use

ln -s /data/mysql_datadir/mysql.sock /var/lib/mysql/mysql.sock

Method 2: To mention socket file for client and server, you need to set socket file path like below for both client and server file. server file name could be server.cnf and client file name could be client.cnf. Inside that file you will find a section like [client] or [server] . You have to put update socket file path for both server and client to communicate..

client.cnf

[client]
socket=/mydata/db/mysql/mysql.sock

server.cnf

[server]
socket=/mydata/db/mysql/mysql.sock

My preference instead of method-1 use method-2. Because link is a dependent file if somehow it is deleted then you will get trouble. Since you know better option that link you should use it.

Solenoid answered 24/8, 2022 at 16:28 Comment(0)
A
-1

I have the same problem and when I digged into the log:

sudo tail -f /var/log/mysqld.log

InnoDB: mmap(137363456 bytes) failed; errno 12
200610  6:27:27 InnoDB: Completed initialization of buffer pool
200610  6:27:27 InnoDB: Fatal error: cannot allocate memory for the buffer pool
200610  6:27:27 [ERROR] Plugin 'InnoDB' init function returned error.
200610  6:27:27 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
200610  6:27:27 [ERROR] Unknown/unsupported storage engine: InnoDB
200610  6:27:27 [ERROR] Aborting

Clearly there's not enough memory and stopping InnoDB to start. I realized I never set up the swap file...

I followed this to set up the swap file and it works.

Abwatt answered 10/6, 2020 at 6:45 Comment(0)
I
-2

so it tells you that they cannot find the socket file at '/var/lib/mysql/mysql.sock'

what you'll do is you need to connect the mysql.sock file to it most probably it is at /tmp/mysql.sock if the file doesn' exist create it using

touch /tmp/mysql.sock

and then execute

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

note if there is no mysql directory inside /var/lib create it

mkdir mysql

refresh your app

Insphere answered 7/4, 2016 at 7:20 Comment(0)
A
-2

Navicat15: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

Ubuntu16.04, if mysql server is running and the configure in /etc/mysql like this:

[mysqld] socket = /var/run/mysqld/mysqld.sock

Then Navicat15 localhost default setting is: "/var/lib/mysql/mysql.sock"

You can edit in advance like this:

enter image description here

Antibiosis answered 5/6, 2020 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.