无法通过套接字 '/var/run/mysqld/mysqld.sock' (2) 连接到本地 MySQL 服务器(错误 2002)

Posted

技术标签:

【中文标题】无法通过套接字 \'/var/run/mysqld/mysqld.sock\' (2) 连接到本地 MySQL 服务器(错误 2002)【英文标题】:Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) (error 2002)无法通过套接字 '/var/run/mysqld/mysqld.sock' (2) 连接到本地 MySQL 服务器(错误 2002) 【发布时间】:2015-08-11 14:59:58 【问题描述】:

我知道这是一个多次被问到的问题,但我似乎无法从其他主题中找到任何解决方案。

我尝试过的:

重新安装 mysql-server(所以不是客户端) chown mysql:mysql /var/lib/mysql 检查了 my.cnf,一切似乎都正确 将绑定地址更改为本地主机

在某个时候,我似乎没有套接字文件,并且

sudo find / -type s | grep mysqld.sock 没有给出任何结果。


编辑

我现在输入了mysql_config --socket,现在/var/run/mysqld/mysqld.sock似乎有一个套接字文件

还检查了apparmor_status,结果如下:

apparmor module is loaded.
14 profiles are loaded.
14 profiles are in enforce mode.
   /sbin/dhclient
   /usr/bin/evince
   /usr/bin/evince-previewer
   /usr/bin/evince-previewer//sanitized_helper
   /usr/bin/evince-thumbnailer
   /usr/bin/evince-thumbnailer//sanitized_helper
   /usr/bin/evince//sanitized_helper
   /usr/lib/NetworkManager/nm-dhcp-client.action
   /usr/lib/connman/scripts/dhclient-script
   /usr/lib/cups/backend/cups-pdf
   /usr/sbin/cups-browsed
   /usr/sbin/cupsd
   /usr/sbin/mysqld
   /usr/sbin/tcpdump
0 profiles are in complain mode.
3 processes have profiles defined.
0 processes are in enforce mode.
0 processes are in complain mode.
3 processes are unconfined but have a profile defined.
   /sbin/dhclient (1089) 
   /usr/sbin/cups-browsed (1415) 
   /usr/sbin/cupsd (16608) 

毕竟/etc/init.d/mysql start 仍然失败

my.cnf 中的设置

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

我检查了/var/log/error.log,当我尝试# /etc/init.d/mysql start 时,日志中显示以下报告:

150528 22:58:19 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
150528 22:58:19 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
150528 22:58:19 [Note] /usr/sbin/mysqld (mysqld 5.5.43-0ubuntu0.14.04.1) starting as process 14608 ...
150528 22:58:19 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
150528 22:58:19 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
150528 22:58:19 [Note] Plugin 'FEDERATED' is disabled.
/usr/sbin/mysqld: Can't find file: './mysql/plugin.frm' (errno: 13)
150528 22:58:19 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
150528 22:58:19 InnoDB: The InnoDB memory heap is disabled
150528 22:58:19 InnoDB: Mutexes and rw_locks use GCC atomic builtins
150528 22:58:19 InnoDB: Compressed tables use zlib 1.2.8
150528 22:58:19 InnoDB: Using Linux native AIO
150528 22:58:19 InnoDB: Initializing buffer pool, size = 128.0M
150528 22:58:19 InnoDB: Completed initialization of buffer pool
150528 22:58:19 InnoDB: highest supported file format is Barracuda.
150528 22:58:19  InnoDB: Waiting for the background threads to start
150528 22:58:20 InnoDB: 5.5.43 started; log sequence number 140421610
150528 22:58:20 [Note] Server hostname (bind-address): 'localhost'; port: 3306
150528 22:58:20 [Note]   - 'localhost' resolves to '127.0.0.1';
150528 22:58:20 [Note] Server socket created on IP: '127.0.0.1'.
150528 22:58:20 [ERROR] /usr/sbin/mysqld: Can't find file: './mysql/host.frm' (errno: 13)
150528 22:58:20 [ERROR] Fatal error: Can't open and lock privilege tables: Can't find file: './mysql/host.frm' (errno: 13)
150528 22:58:20 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

有人可以帮我解决如何在不丢失数据库的情况下修复它

【问题讨论】:

您是否使用目录/mysql/data/dir 作为数据目录?您是否也相应地更新了您的my.cnf 文件?默认情况下,MySQL 将查找/var/lib/mysql 我看到你使用的是 Ubuntu,所以还要检查 apparmor 是否处于 MySQL 的强制模式(命令是 apparmor_status)。 目录是一种格式,它的真实位置确实是/var/lib/mysql 您需要通过运行mysql_install_dbmysql_secure_installation 来初始化mysql 数据目录。然后就可以开始mysqld了。 当我运行 mysql_secure_installation 时,我需要输入 root 密码,然后它给出了同样的错误 --> 没有套接字文件。所以我运行mysql_install_db 并按照cmd 的建议进行操作,所以我能够输入mysql 并且show tables 也可以工作,但是从这些表中选择会出现以下错误ERROR 1017 (HY000): Can't find file: './mysql/user.frm' (errno: 13) 【参考方案1】:

mysql_install_db cmd 成功了。我又有了一个套接字,我现在可以再次登录到 mysql。谢谢

【讨论】:

【参考方案2】:

如果你还没有初始化数据目录,现在是时候在启动mysqld之前初始化它了。

您可以运行 mysql 提供的这两个脚本中的任何一个。在你的 shell 提示符下运行它们:

# mysql_secure_installation

# mysql_install_db --user=mysql \
         --basedir=<path to mysql install dir> \
         --datadir=/var/lib/mysql/data

mysql_install_db 将调用带有--bootstrap--skip-grant-tables 选项的mysqld

【讨论】:

现在看来我毕竟没有套接字文件。虽然当我运行mysql_config --socket 时它说它在那里,但是当我检查文件夹时,里面什么都没有。所以错误保持不变【参考方案3】:

您的 mysql 安装已完成:

150528 22:58:20 [ERROR] /usr/sbin/mysqld: Can't find file: './mysql/host.frm' (errno: 13)

它一开始就没有启动,因此没有套接字文件。您缺少内部/核心 mysql 数据库中的数据,这是一个非常糟糕的信号。

【讨论】:

我不相信他的安装是水管。我认为他没有初始化数据目录。

以上是关于无法通过套接字 '/var/run/mysqld/mysqld.sock' (2) 连接到本地 MySQL 服务器(错误 2002)的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu:无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)

Debian 10 MariaDB '无法通过套接字'/var/run/mysqld/mysqld.sock' (2) 连接到本地 MySQL 服务器'

无法通过套接字“/var/run/mysqld/mysqld.sock”连接到本地 MySQL 服务器。

ERROR 2002 (HY000): 无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)

ERROR 2002 (HY000): 无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)

ERROR 2002 (HY000): 无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)