mysql异常错误 ERROR 2002(转载)

#: mysql

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

解决方案三:

# vi /etc/selinux/config

打开以下界面:

  # This file controls the state of SELinux on the system.

 # SELINUX= can take one of these three values:

  #       enforcing - SELinux security policy is enforced.

 #       permissive - SELinux prints warnings instead of  enforcing.

  #       disabled - SELinux is fully disabled.

  SELINUX=disable

  # SELINUXTYPE= type of policy in use. Possible values are:

  #        targeted - Only targeted network daemons ard protected.

 #        strict - Full SELinux protection.

     SELINUXTYPE=strict

Version: '5.1.71'  socket: '/home/mysql_data/mysql/mysql.sock'  port: 3306  Source distribution

修改权限

解决方案一:

对mysql 使用以下命令

[root@localhost ~]#    service mysqld status; //查看mysql状态

[root@localhost ~]#  service mysqld  stop;  //停止mysql

[root@localhost ~]#  service mysqld start ; //启动mysql

这里虽然mysql 仍然出现mysql  ERROR(2002)错误但是程序可以远程连接成功;

查看mysql 日志是以下内容:

170412 17:56:53 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
170412 17:57:09 mysqld_safe Starting mysqld daemon with databases from /home/mysql_data/mysql
170412 17:57:09  InnoDB: Initializing buffer pool, size = 8.0M
170412 17:57:09  InnoDB: Completed initialization of buffer pool
170412 17:57:09  InnoDB: Started; log sequence number 0 44273
170412 17:57:09 [Note] Event Scheduler: Loaded 0 events
170412 17:57:09 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.1.71'  socket: '/home/mysql_data/mysql/mysql.sock'  port: 3306  Source distribution

表示正常运行

查看mysql.sock

[root@localhost ~]# find / -name mysql.sock
/tmp/mysql.sock
/home/mysql_data/mysql/mysql.sock
/home/.Trash-0/files/mysql.sock
/root/mysql.sock

前提我以做软连接  ln -s  /var/lib/mysql/mysql.sock    /home/mysql_data/mysql/mysql.sock

以上数据库可以正常操作(远程mysql连接数据库)

解决方案二:

(转载)http://blog.sina.com.cn/s/blog_5fb846310100lxsu.html

启动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 看看mysql是否已经启动.
另外看看是不是权限问题.

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 -u root -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 &

问题解决。

原文地址:https://www.cnblogs.com/miaoer/p/6700748.html