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

问题描述: 

在linux下安装mysql,进行以下步骤

[root@localhost support-files]# service mysql start
Starting MySQL... SUCCESS! 
[root@localhost support-files]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
去/var/lib/目录下查看,发现没有mysql目录,更不会有mysql.sock文件了,估计这正是问题所在。

解决方法: 
大多数mysql都是rpm方式安装的,它会自动寻找/var/lib/mysql/mysql.sock这个文件。

检查mysql.sock的位置,通过/etc/my.cnf中的socket的字段。 
如果socket如下所示:

socket=/tmp/mysql.sock

如果socket没有值,则直接去/tmp目录下查看,会发现有mysql.sock这个文件。

2.为mysql.sock增加软连接(相当于windows中的快捷方式)。

先在/var/lib/下创建mysql目录,运行命令为:

mkdir /var/lib/mysql

为mysql.sock增加软链接,实际的mysql.sock在/tmp/下,运行如下命令:

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

再次运行mysql

[root@localhost usr]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.19 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

启动成功!

你可能感兴趣的:(Linux)