my.cnf 问题小计--ERROR 2002

本文介绍了一种常见的MySQL客户端连接问题——无法通过指定的socket连接到服务器,并提供了解决方案。问题出现在尽管my.cnf中配置了正确的socket路径,但执行mysql命令时仍提示无法连接。解决方法在于调整/etc/my.cnf文件中的[client]部分,确保其包含正确的socket路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:
*.一直想不明白为啥my.cnf已经配置了如下配置:
[mysql@Nginx 3306]$ pwd
/opt/mysqldata/3306
[mysql@Nginx 3306]$ more my.cnf 
[client]
port = 3306
#socket = /opt/mysqldata/3306/mysql.sock
socket = /opt/mysqldata/3306/mysql.sock

但是执行如下命令:socket无法通过:
[mysql@Nginx ~]$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)


解决思路:
1.查看mysql的help命令有如下说明:
[mysql@Nginx 3306]$ mysql --help

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 

说明,虽然mysqld调用的是你制定的mysql.cnf,但是mysql命令却只能调用/etc/my.cnf.因此
配置/etc/my.cnf,可以调用[client]中的socket选项,进去mysql。

[root@Nginx etc]# cp /etc/my.cnf.bak /etc/my.cnf

[mysql@Nginx ~]$ mysql -uroot -p'redhat'
mysql: [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 4
Server version: 5.7.13-log MySQL Community Server (GPL)
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.

(root@localhost) [(none)]> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值