Can't connect to local MySQL server through so

2018-12-31 17:49发布

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?

30条回答
无色无味的生活
2楼-- · 2018-12-31 18:07

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.

查看更多
零度萤火
3楼-- · 2018-12-31 18:07

try

echo 0 > /selinux/enforce
查看更多
深知你不懂我心
4楼-- · 2018-12-31 18:08

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.

查看更多
低头抚发
5楼-- · 2018-12-31 18:09

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

查看更多
余生请多指教
6楼-- · 2018-12-31 18:11

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)

查看更多
孤独寂梦人
7楼-- · 2018-12-31 18:11

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!

查看更多
登录 后发表回答