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

背景:

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

原因:

mysql.sock文件是服务器与本地客户端进行通信的Unix套接字文件,默认位置是/tmp/MySQL.sock。可能此文件不存在。

解决:

四种方案

  1. 在使用mysql 连接的本地数据库时, 命令行添加参数指定,sock 文件的地址
  2. 配置my.cnf 的 [client] 指定 sock 文件的地址
  3. 修改MYSQL_UNIX_PORT环境变量, 将其设置为 unix sock 文件的路径
  4. 重新编译指定 --with-unix-socket-path 选项

方法一

查看当前机器已启动的mysqld使用的socket文件的位置如下:

[user_00@localhost ~]$ ps -ef | grep mysqld
root 5905 1 0 21:08 pts/17 00:00:00 /bin/sh /usr/local/services/mysql-5.6.24/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysqld.pid
mysql 6144 5905 0 21:08 pts/17 00:00:02 /usr/local/services/mysql-5.6.24/bin/mysqld --basedir=/usr/local/services/mysql-5.6.24 --datadir=/data/mysql --plugin-dir=/usr/local/services/mysql-5.6.24/lib/plugin --user=mysql --log-error=/data/mysql/mysql.err --pid-file=/data/mysql/mysqld.pid --socket=/data/mysql/mysql.sock --port=3306
user_00 6430 5575 0 21:14 pts/19 00:00:00 grep --color=auto mysqld

当前机器监听的本地socket 文件是:

--socket=/data/mysql/mysql.sock

或者,可以通过如下查询:

[user_00@localhost ~]$ netstat -xl | grep mysql
unix 2 [ ACC ] STREAM LISTENING 115130474 /data/mysql/mysql.sock

故可以使用如下方式进行连接:

[user_00@localhost ~]$ mysql -uroot -p123456 -S /data/mysql/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.24 Source distribution Copyright (c) 2000, 2015, 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. mysql>

方法二

在mysql的配置文件 /etc/my.cnf 中指定链接的socket 的地址

[client]
socket=/data/mysql/mysql.sock

再连接,成功

[user_00@localhost ~]$ mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.24 Source distribution Copyright (c) 2000, 2015, 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. mysql>

方法三

将MYSQL_UNIX_PORT环境变量设置为Unix套接字文件的路径

[user_00@localhost ~]$ echo $MYSQL_UNIX_PORT

[user_00@localhost ~]$ export MYSQL_UNIX_PORT=/data/mysql/mysql.sock
[user_00@localhost ~]$ mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.24 Source distribution Copyright (c) 2000, 2015, 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. mysql>

方法四

源码编译MySQL,不用默认Unix套接字文件位置。使用 --with-unix-socket-path 选项指定路径。

检查

用 mysqladmin 连接服务器,测试套接字位置是否正确

[user_00@localhost ~]$ mysqladmin -uroot -p123456 --socket=/data/mysql/mysql.sock version
mysqladmin Ver 9.0 Distrib 5.5.41-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others. Server version 5.6.24
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /data/mysql/mysql.sock
Uptime: 29 min 35 sec Threads: 1 Questions: 6 Slow queries: 0 Opens: 70 Flush tables: 1 Open tables: 63 Queries per second avg: 0.003

用错误的 socket 文件,提示如下

[user_00@localhost ~]$ mysqladmin -uroot -p123456 --socket=/data/mysql/mysqle.sock version
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/data/mysql/mysqle.sock' (2)'
Check that mysqld is running and that the socket: '/data/mysql/mysqle.sock' exists!

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的更多相关文章

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

    有时候,当我们使用"mysql"."mysqladmin"."mysqldump"等命令管理数据库时,服务器抛出类似如下错误: 一.错误现场 ...

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

    从供应商手中接手一个云平台(Windwos Azure)上的MySQL数据库,登录数据库时遇到错误: $mysql -uroot -p Enter password: ERROR 2002 (HY00 ...

  3. ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了(转载)

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var mysql 启动不了   ps -A | gr ...

  4. Mac mySql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决办法

    我的环境:Mac 10.11.6 ,mysql  5.7.14  . mac mySql 报错ERROR 2002 (HY000): Can't connect to local MySQL serv ...

  5. linux云服务器mysql ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’

    一早上过来发现网站打开报错,数据库连接不上.. 有人改密码? putty进去,mysql -uroot -p 输入密码后,报错 ERROR 2002 (HY000): Can't connect to ...

  6. 更换mysql数据目录后出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 的解决办法

    服务器上的mysql默认数据目录为/var/lib/mysql/,同时服务器的/空间不是很大,而近期又有大量的日志需要导入进行分析,时常搞得/的空间捉襟见肘,晚上一狠心就想把mysql的数据目录转移到 ...

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

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

  8. 安装mysql后ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了

    ps -A | grep -i mysql kill 列出来的进程 service mysql start 我的问题就解决了 ------------------------------------- ...

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

    好久不用mysql,今天突然想用的时候, mysql -uroot -p 直接报了下面的错误 ERROR 2002 (HY000): Can't connect to local MySQL serv ...

随机推荐

  1. Hadoop学习12-配置集群环境

    由于之前虚拟机都是用的桥接方式,有时候没有网络可用,想学习的时候,就狠不方便. 于是研究了一下,希望搭建一个多台虚机组成一个局域网的集群,即host-only方式 1.安装VM,网络选择“host-o ...

  2. Corel Painter 15在Surface Pro 4下开启笔触压力感应

    之前一直是用Wacom的板子,所以只需要下载Wacom板子相应的驱动安装即可就能在PS和Corel Painter中开启压力感应来调节笔触出线的粗细.Surface Pro 4的笔是支持压力感应的,但 ...

  3. java byte转无符号int

    import java.io.ByteArrayInputStream; public class Test{ public static void main(String[] args) { byt ...

  4. java连接mysql :No Suitable Driver Found For Jdbc 解决方法

    今天出现编码出现了No suitable driver found for jdbc,又是找遍了网上的资料,基本上都说是以下个问题:    一是:连接URL格式出现了问题(Connection con ...

  5. requirejs笔记

    1.RequireJS是一个工具库,主要用于客户端的模块管理.它可以让客户端的代码分成一个个模块.实现异步或动态加载,从而提高代码的性能和可维护性.它的模块管理遵循AMD规范(Asynchronous ...

  6. 如何修改SharePoint列表条数等阈值

    若要修改SharePoint中对列表最大条数等设定的阈值,可按如下步骤操作: 1. 打开页面:管理中心 > 应用程序管理 > 管理Web应用程序.2. 选择要修改阈值的Web应用程序,并在 ...

  7. Linux下的shell编程(一)BY 四喜三顺

    Ctrl + Alt + Tvim文本编辑器:i(插入模式),v(可视模式),Esc(普通模式),:w(保存),:r(打开),:q(退出)gvim 文本编辑器vim的升级版gedit 更常用的文本编辑 ...

  8. 云存储性能测试工具--COSBench安装

    COSBench安装 Cosbench是Intel的开源云存储性能测试软件,COSBench目前已经广泛使用与云存储测试,并作为云存储的基准测试工具使用 1 环境 1.1 操作系统 COSBench可 ...

  9. C语言初始化——bss段初始化、跃入C、C与汇编

    1.bss段初始化 变量 存放位置 初始化的全局变量 数据段 局部变量 栈 malloc函数分配的 堆 未初始的全局变量 bss段 说明:全局变量在未赋初值时,会被保留到bss段. 测试: #incl ...

  10. HTTP 权威指南

    第一章 http概述 1.3.1 媒体类型 - http为每种web传输的数据格式加上MIME类型数据标签(multipurpose internet mail 1.4 事务 一个请求 + 一个响应 ...