ITPub博客

首页 > 数据库 > MySQL > ERROR 2002(HY000):Can't connect to local MySQL socket '/tmp/mysql.sock'

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

MySQL 作者:urgel_babay 时间:2016-02-29 17:13:09 0 删除 编辑

公司要使用MySQL,我就开始玩MySQL,可是跟想象中的不一样,问题颇多,只能慢慢享受了。之前就通过编译的方式安装好MySQL5.7。
今天登陆的时候遇见一的问题,记录一下

[root@testmysql bin]# ./mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)


解决方法:

由于mysql 默认的mysql.sock 是在/usr/local/mysql/data/mysql.sock,但linux系统总是去/tmp/mysql.sock查找,所以会报错

1.直接指定mysql通道

据我知道的(ps:我是菜鸟,请纠正),老版本(5.4之前)的是:/var/lib/mysql/mysql.sock

 在新版本中默认是在/usr/local/mysql/data/mysql.sock,具体的可以去查看my.cnf文件里面

[root@testmysql bin]# ./mysql --socket=/usr/local/mysql/data/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.4-m7 Source distribution
Copyright (c) 2000, 2011, 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>


 

2. 创建符号连接:

 

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

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


eg:
root@localhost ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@testmysql bin]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

插曲:有可能你在建立连接的时候出现下面的情况,
[root@testmysql bin]# ln -vs /usr/local/mysql/data/mysql.sock /tmp/mysql.sock
ln: creating symbolic link `/tmp/mysql.sock': File exists
没关系,加上-f参数就可以了。-f的主要过程:先将同名的软链接dd删除,然后再创建硬链接dd

[root@testmysql bin]# ln -f /usr/local/mysql/data/mysql.sock /tmp/mysql.sock
[root@testmysql bin]# ./mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.4-m7 Source distribution
Copyright (c) 2000, 2011, 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>


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30936525/viewspace-2016620/,如需转载,请注明出处,否则将追究法律责任。

下一篇: Sysbench基准测试
请登录后发表评论 登录
全部评论

注册时间:2016-02-29

  • 博文量
    203
  • 访问量
    225812