关闭

ERROR 2002 (HY000): Can't connect to local MySQL server through socket

标签: DatabaseSQLdbamysqlsocket
9452人阅读 评论(0) 收藏 举报
分类:

    在安装好了MySQL之后,使用了新的配置文件后,MySQL服务器可以成功启动,但在登陆的时候出现了ERROR 2002 (HY000): Can't connect to local MySQL server through socket,即无法通过socket连接到mysql服务器,同时提供了socket文件的位置。下面是这个问题的描述与解决办法。

 

1、故障现象
[root@SZDB mysqldata]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/data/mysqldata/mysql.sock' (111)

#故障环境
[root@SZDB mysqldata]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m

 

2、故障分析
#查看mysql实例的状态
[root@SZDB mysqldata]# netstat -ntlp  | grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      13001/mysqld

#查看my.cnf关于socket的配置
[root@SZDB mysqldata]# more /etc/my.cnf |grep sock
socket = /tmp/mysql.sock

#由上可知my.cnf中定义的为/tmp目录下,而错误提示为/data/mysqldata/目录下
#也就是说mysqld已经声称了正确的sock文件,但客户端连接还是从初始目录去找sock文件
#下面查看后台日志,有个ERROR,是关于满查询日志的,是由于目录不存在而产生的错误,与当前故障无关
[root@SZDB mysqldata]# more SZDB.err
             ............
2014-10-11 13:17:21 13001 [Note] InnoDB: 5.6.12 started; log sequence number 1625997
/app/soft/mysql/bin/mysqld: File '/log/mysql_logs/slowquery.log' not found (Errcode: 2 - No such file or directory)
2014-10-11 13:17:21 13001 [ERROR] Could not use /log/mysql_logs/slowquery.log for logging (error 2). Turning logging off for the who
le duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.
2014-10-11 13:17:21 13001 [Note] Server hostname (bind-address): '*'; port: 3306
2014-10-11 13:17:21 13001 [Note] IPv6 is available.
2014-10-11 13:17:21 13001 [Note]   - '::' resolves to '::';
2014-10-11 13:17:21 13001 [Note] Server socket created on IP: '::'.
2014-10-11 13:17:21 13001 [Note] Event Scheduler: Loaded 0 events
2014-10-11 13:17:21 13001 [Note] /app/soft/mysql/bin/mysqld: ready for connections.
Version: '5.6.12-log'  socket: '/tmp/mysql.sock'  port: 3306  Source distribution
#Author :Leshami
#Blog   :
http://blog.csdn.net/leshami

 

3、解决故障
a、通过配置my.cnf mysql选项socket文件位置解决
#先停止mysql服务器
[root@SZDB mysqldata]# service mysqld stop
Shutting down MySQL.[  OK  ]

#修改my.cnf,如下
[root@SZDB mysqldata]# vi /etc/my.cnf
[mysql]
no-auto-rehash
socket = /tmp/mysql.sock  #添加该行

#重启mysql服务器
[root@SZDB mysqldata]# service mysqld start
Starting MySQL..[  OK  ]

#再次连接正常
[root@SZDB mysqldata]# mysql -uroot -p
Enter password:
mysql> show variables like 'version';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.6.12-log |
+---------------+------------+

 

b、为socket文件建立链接方式
[root@SZDB mysqldata]# ln -s /tmp/mysql.sock /data/mysqldata/mysql.sock
ln: creating symbolic link `/data/mysqldata/mysql.sock' to `/tmp/mysql.sock': File exists
[root@SZDB mysqldata]# rm mysql.sock    #上面提示文件存在,所以删除之前的mysql.sock文件
[root@SZDB mysqldata]# ln -s /tmp/mysql.sock /data/mysqldata/mysql.sock
[root@SZDB mysqldata]# ls -hltr mysql.sock
lrwxrwxrwx 1 root root 15 Oct 11 14:00 mysql.sock -> /tmp/mysql.sock
[root@SZDB mysqldata]# mysql -uroot -p
Enter password:
mysql> show variables like 'socket';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| socket        | /tmp/mysql.sock |
+---------------+-----------------+

 

鹏城DBA总群

2
0
查看评论

记一次 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock’(2) 排错流程

在linux与unix系统的socket文件是为了建立两个程序之间的连接使用的 , 而在mysql中,当mysql启动成功时,会根据配置文件中设定的路径(如:tmp/mysql.sock),在tmp中生成mysql.sock,用于连接数据库使用。而如果没有此文件,造成的结果可想而知
  • yun__yang
  • yun__yang
  • 2017-01-16 12:01
  • 26166

zabbix到安装界面报错" Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock"

mysql和zabbix正常安装后在,zabbix安装页面(连接数据库的界面)时,提示“”Error connecting to database: Can't connect to local MySQL server through socket '/var/lib/mysql/...
  • wzqzhq
  • wzqzhq
  • 2017-02-21 13:48
  • 4326

ERROR 2002 (HY000): Can't connect to local MySQL server through socket

在安装好了MySQL之后,使用了新的配置文件后,MySQL服务器可以成功启动,但在登陆的时候出现了ERROR 2002 (HY000): Can't connect to local MySQL server through socket,即无法通过socket连接到mysql服务器,同时提...
  • robinson_0612
  • robinson_0612
  • 2014-10-11 16:57
  • 9452

解决Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’错误

刚安装完mysql,进行运行测试,报Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’错误, [root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe ...
  • xiaoshunzi111
  • xiaoshunzi111
  • 2016-10-12 15:59
  • 21576

解决archlinux(manjaro)无法启动mysql(mariaDB):ERROR 2002 (HY000): Can't connect to local MySQL server throu

mysql无法启动
  • XingYuan8023
  • XingYuan8023
  • 2017-05-31 16:55
  • 1207

Linux下Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ mysql service 无法正常启动和关闭

Ubuntu下Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ mysql service 无法正常启动和关闭 这时候只能Kill进程,原因是找不到socket 有时候socket会在这里地方,需要在...
  • dongfangxiaozi_
  • dongfangxiaozi_
  • 2017-06-10 09:43
  • 1360

centos下mysql问题记录--ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/

一、关于云主机的mysql最近一次云主机的服务器由于被攻击便打算重装一边mysql,彻底删除相关文件后重新安装,没想到怎么装都会出问题。再启动的时候总是出现ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘...
  • qq_26446443
  • qq_26446443
  • 2016-12-06 19:40
  • 1897

亲测有效,解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";

最近租了一个阿里云云翼服务器,趁着自己还是一个学生,享受一下优惠。我租的是阿里云Ubuntu16.04版本的服务器,在搭建mysql的时候,一开始是可以运行的,由于某次手抽,把mysql的套接字文件不小心删除了,然后无论怎么卸载重装,都出现了这个问题: root@iZufkfljcZ:~# mysq...
  • hjf161105
  • hjf161105
  • 2017-12-20 10:50
  • 854

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

今天服务器遇到了一个很熟悉的问题 输入#mysql -u root -p ERROR 2002 (HY000):Cant connect to local MySQL server  随即上网找寻答案 根据大家提供的方法我逐一尝试 方案1. ...
  • lmss82
  • lmss82
  • 2009-08-05 22:42
  • 176839

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

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock’(2) 是一个比较常见的数据库报错,而昨天我们的服务器也报了这个错误,所有服务全挂,而且恰逢阿里云升级,以为...
  • qw_xingzhe
  • qw_xingzhe
  • 2017-12-02 23:15
  • 130
    个人资料
    • 访问:5642125次
    • 积分:54509
    • 等级:
    • 排名:第61名
    • 原创:641篇
    • 转载:0篇
    • 译文:7篇
    • 评论:732条
    关于我

    • 十年以上数据库运维管理,性能优化经验
    • 提供数据库相关技术支持及数据库培训
    •  全部文章,欢迎扩散,转载请注明出处


    QQ/Weixin:645746311

    DBA交流群:170233858

    打赏作者
      支付宝&微信支付二维码
      支付宝&微信支付
    文章存档
    最新评论