Can't connect to local MySQL server through so

2018-12-31 17:25发布

问题:

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?

回答1:

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.



回答2:

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


回答3:

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



回答4:

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



回答5:

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



回答6:

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.



回答7:

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


回答8:

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


回答9:

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;



回答10:

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.



回答11:

Here\'s what worked for me:

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
service mysqld restart


回答12:

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.



回答13:

Please check whether another mysql service is running.



回答14:

Make sure you started the server:

mysql.server start

Then connect with root user:

mysql -uroot


回答15:

try

echo 0 > /selinux/enforce


回答16:

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/*



回答17:

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



回答18:

First enter \"service mysqld start\" and login



回答19:

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



回答20:

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.



回答21:

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)



回答22:

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 ~ $


回答23:

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!



回答24:

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


回答25:

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



回答26:

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!



回答27:

I had to disable explicit_defaults_for_timestamp from my.cnf.



回答28:

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.



回答29:

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.



回答30:

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.