Can't connect to local MySQL server through socket

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

2013年9月6日aiezu發表評論閱讀評論php

mysql,Mysqldump,Mysqladmin,php鏈接mysql服務常會提示下面錯誤:html

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)mysql

1sql

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

這是因爲修改mysql服務的了socket文件mysql.sock位置,而致使沒法經過mysql socket文件鏈接到mysql服務引發的,具體解決辦法以下:
一、查看mysql服務的socket文件位置:
mysql socket文件的位置是在/etc/my.cnf中設置的,cat /etc/my.cnf內容以下:服務器

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

1spa

2server

3htm

4

[mysqld]

datadir=/storage/db/mysql

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

user=mysql

其中socket等於的路徑就是socket文件的位置,咱們只要修改my.cnf文件,告訴mysql,mysqldump,mysqladmin mysql服務的socket位置在哪裏就能夠。

二、修改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)

一、先查看 /etc/rc.d/init.d/mysqld status 看看m y s q l 是否已經啓動.
另外看看是否是權限問題.

二、肯定你的mysql.sock是否是在那個位置,
mysql -u 你的mysql用戶名 -p -S /var/lib/mysql/mysql.sock

三、試試:service mysqld start

四、若是是權限問題,則先改變權限 #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 &問題解決。

相關文章
相關標籤/搜索