ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql
Posted 月落无影
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql相关的知识,希望对你有一定的参考价值。
我在使用mysqll客户端连接我的mysql服务器的时候,出现了上述的问题。我的操作系统是ubuntu,安装版本是对应的64位服务器。我的服务器的启动方式是sudo service mysql start.
我对服务器的改动仅限于新增了一个mysql的配置文件,配置文件的位置在/etc/my.cnf。配置文件如下(不要在意设置的具体值):
[mysqld] # GENERAL datadir = /var/lib/mysql socket = /var/lib/mysql/mysql.sock pid_file = /var/lib/mysql/mysql.pid user = mysql port = p3306 default_storage_engine = InnoDB #INNODB innodb_buffer_pool_size = 2G innodb_log_file_size = 300M # LOGGING log_error = /var/lib/mysql/mysql-error.log slow_query_log_file = /var/lib/mysql/mysql-slow.log # OTHER tmp_table_size = 32M max_heap_table_size = 32M #query_cache_type = 0 #query_cache_size = 0 max_connections = 200 thread_cache_size = 50 table_open_cache = 300 open_files_limit = 65535 [client] socket = /var/lib/mysql/mysql.sock port = 3306
首先我要说明一点,使用mysql客户端不能连接到mysql服务端,是因为mysql连接的时候,使用了这个配置文件的client部分,也就是
[client]
socket = /var/lib/mysql/mysql.sock
而服务端的socket文件的位置不在这里,自然会出现问题。我查看/var/lib/mysql文件夹,都不存在mysql.sock文件。
那么服务端的socket文件保存在哪里呢?
答案是/var/run/mysqld文件夹,这里有一个mysqld.sock文件. 这个文件的位置是由/etc/my.cnf->/etc/mysql.cnf里面配置的文件/etc/mysql/mysql.conf.d/mysqld.cnf指定的。
查看mysqld.cnf文件夹,还可以如下配置:
[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql log-error = /var/log/mysql/error.log
可以查看对应文件夹,发现上述配置都生效了。我们查看mysql官网,可以看到如下警告语句:
Note On Unix platforms, MySQL ignores configuration files that are world-writable. This is intentional as a security measure.
你检查一下你的/etc/my.cnf文件,然后改成/etc/mysql/mysql.cnf相同的访问属性,发现,仍然出现相同的问题。
你又会惊奇地发现,官网上面有如下说明:
On Unix and Unix-like systems, MySQL programs read startup options from the files shown in the following table, in the specified order (files listed first are read first, files read later take precedence).
你会觉得这里是/etc/mysql/mysql.conf.d/mysqld.cnf配置文件替换了/etc/my.cnf中的值。然后你选择删除/etc/my.cnf中关于client的配置,你发现不报上述错误了。当你进去之后,使用show gloabal variables命令,发现根本没有生效。
那么这个又是为什么?
然后你可以进行如下操作:在/etc/mysql/mysql.cnf中添加一句port = 3306,修改后,如下:
port = 3306 !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/
然后你调用sudo service mysql start命令,会发现如下错误:
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
然后你调用systemctl status mysql.service命令,得到如下错误:
4月 14 00:11:35 sun-Lenovo-IdeaPad-Y480 mysql-systemd-start[25400]: my_print_defaults: [ERROR] Fatal error in defaults handling. Program abort 4月 14 00:11:35 sun-Lenovo-IdeaPad-Y480 mysqld[25439]: mysqld: [ERROR] Found option without preceding group in config file /etc/mysql/my.cnf a 4月 14 00:11:35 sun-Lenovo-IdeaPad-Y480 mysqld[25439]: mysqld: [ERROR] Fatal error in defaults handling. Program aborted! 4月 14 00:11:35 sun-Lenovo-IdeaPad-Y480 systemd[1]: mysql.service: Main process exited, code=exited, status=1/FAILURE 4月 14 00:11:35 sun-Lenovo-IdeaPad-Y480 systemd[1]: mysql.service: Failed with result ‘exit-code‘. 4月 14 00:11:35 sun-Lenovo-IdeaPad-Y480 systemd[1]: Failed to start MySQL Community Server.
然后你移除/etc/mysql/mysql.cnf中的port = 3306,改为在/etc/my.cnf中只添加这一行。
port = 3306
你发现调用sudo service mysql start没有报错。按照上述的表现来看,mysql应该根本就不会读取/etc/my.cnf,所以你之前添加的这个文件,只是对mysql客户端链接产生了影响。
如果你想在使用sudo service mysql start方式启动时的服务端参数,最简单的方法是将原先的/etc/mysql/mysql.conf.d/mysqld.cnf备份一下,然后直接修改这个文件的值。修改之后,可能会遇到其他问题,再想办法解决了。
以上是关于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 '/tmp/mysql.sock' 。安装后出错
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) on Mac
FreeBSD 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'/var/run/mysqld/mysqld.sock'
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when I
尝试了一切仍然得到 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (