如何解决登录mysql出现 Can't connect to local MySQL server through socket的问题

导语:一位客户的服务器需要登录到mysql里面去清理日志,但叶子使用mysql命令登录的时候出现问题,提示Can’t connect to local MySQL server through socket。经过努力使用了指定sock文件的方法成功登入mysql。

登录mysql提示错误

在服务器上执行命令,提示sock文件有问题。

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

查看my.cnf

我们来看一下my.cnf的配置,看它指定的sock文件是不是在那个地方。

[mysqld]
default-storage-engine = MyISAM
default-tmp-storage-engine = MYISAM
#loose-skip-innodb
innodb_file_per_table=1
innodb_file_format=barracuda
innodb_strict_mode=1

port = 3306
socket = /tmp/mysql-5.6.sock

叶子发现它指定的sock文件为/tmp/mysql-5.6.sock。

查找sock文件

叶子用下面的命令查看了mysql是运行状态的。

ps aux| grep mysqld

然后用find命令来查找sock是否在tmp目录存在

[root]# find / -name mysql-5.6.sock
/tmp/mysql-5.6.sock

登录mysql

我们在登录的时候指定sock的位置。


[root]# mysql56 -u root -p -S /tmp/mysql-5.6.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21701
Server version: 5.6.30 Source distribution

Copyright (c) 2000, 2016, 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>

为什么用了mysql56,因为客户的机器上安装了两个mysql的版本,一个是5.5,一个是5.6,叶子为了区分,就在/bin/位置创建了一个叫mysql56的软链接。

ln -s /usr/local/mysql-5.6/bin/mysql /bin/mysql56

正常的,使用下面的命令就可以登陆了。

mysql -u root -p -S /tmp/mysql-5.6.sock

结束

你学会了吗?

发表评论

邮箱地址不会被公开。 必填项已用*标注