Can't connect to local MySQL server through socket

连接Mysql提示Can’t connect to local MySQL server through socket的解决方法

2013年9月6日aiezu发表评论阅读评论

mysql,Mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/<span class='wp_keywordlink_affiliate'><a href="http://www.aiezu.com/tag/mysql-2" title="查看 mysql 中的全部文章" target="_blank">mysql</a></span>/mysql.sock' (2)

1

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

这是由于修改mysql服务的了socket文件mysql.sock位置,而导致无法通过mysql socket文件连接到mysql服务引起的,具体解决办法如下:
1、查看mysql服务的socket文件位置:
mysql socket文件的位置是在/etc/my.cnf中设置的,cat /etc/my.cnf内容如下:

[mysqld] datadir=/storage/db/mysql socket=/storage/db/mysql/mysql.sock user=mysql

1

2

3

4

[mysqld]

datadir=/storage/db/mysql

socket=/storage/db/mysql/mysql.sock

user=mysql

其中socket等于的路径就是socket文件的位置,我们只要修改my.cnf文件,告诉mysql,mysqldump,mysqladmin mysql服务的socket位置在哪里就可以。

2、修改my.cnf文件:
在/etc/my.cnf文件中添加如下内容,并重启mysqls服务,即可解决mysql,mysqldump,mysqladmin的“Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'”问题:

[mysqld] datadir=/storage/db/mysql socket=/storage/db/mysql/mysql.sock [mysql] socket=/storage/db/mysql/mysql.sock [mysqldump] socket=/storage/db/mysql/mysql.sock [mysqladmin] socket=/storage/db/mysql/mysql.sock

1

2

3

4

5

6

7

8

9

10

11

12

[mysqld]

datadir=/storage/db/mysql

socket=/storage/db/mysql/mysql.sock

 

[mysql]

socket=/storage/db/mysql/mysql.sock

 

[mysqldump]

socket=/storage/db/mysql/mysql.sock

 

[mysqladmin]

socket=/storage/db/mysql/mysql.sock




eoiioe

Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’解决方法

启动mysql 报错:

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

1、先查看 /etc/rc.d/init.d/mysqld status 看看m y s q l 是否已经启动.
另外看看是不是权限问题.

2、确定你的mysql.sock是不是在那个位置,
mysql -u 你的mysql用户名 -p -S /var/lib/mysql/mysql.sock

3、试试:service mysqld start

4、如果是权限问题,则先改变权限 #chown -R mysql:mysql /var/lib/mysql

[root@localhost ~]# /etc/init.d/mysqld start
启动 MySQL: [ 确定 ]
[root@localhost ~]# mysql -uroot -p

 

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
   原因是,/var/lib/mysql 的访问权限问题。

shell> chown -R mysql:mysql /var/lib/mysql

接着启动服务器

shell> /etc/init.d/mysql start

服务器正常启动后察看 /var/lib/mysql 自动生成mysql.sock文件。

但是我的问题仍然没有得到解决。

问题终于解决:
方法:    修改/etc/my.conf:
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/usr/local/mysql

If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:

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


发现依旧如此,运行/etc/init.d/mysql start报错:    Starting MySQLCouldn't find MySQL manager or server
是mysqld服务没启,运行/usr/local/mysql/bin/mysqld_safe &

问题解决。


你可能感兴趣的:(Can't connect to local MySQL server through socket)