当前位置: 首页>>技术教程>>正文


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

,

问题描述

我收到这个错误

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysql.sock' (2)

即使我设法通过ubuntu中的命令行启动mysql

mysql stop/waiting
mysql start/running, process 17691

但是,当尝试访问站点时,尝试通过mysql -u root -p访问mysql时,出现数据库连接错误以及上述错误

我检查了错误日志,看到了

    131029 12:53:34 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be remo$
    131029 12:53:34 [Note] Plugin 'FEDERATED' is disabled.
    131029 12:53:34 InnoDB: The InnoDB memory heap is disabled
    131029 12:53:34 InnoDB: Mutexes and rw_locks use GCC atomic builtins
    131029 12:53:34 InnoDB: Compressed tables use zlib 1.2.3.4
    131029 12:53:34 InnoDB: Initializing buffer pool, size = 26.0G
    131029 12:53:36 InnoDB: Completed initialization of buffer pool
    131029 12:53:36 InnoDB: highest supported file format is Barracuda.
    131029 12:53:38  InnoDB: Waiting for the background threads to start
    131029 12:53:39 InnoDB: 5.5.34 started; log sequence number 5146431500
    131029 12:53:39 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
    131029 12:53:39 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
    131029 12:53:39 [Note] Server socket created on IP: '0.0.0.0'.
    131029 12:53:39 [Note] Event Scheduler: Loaded 0 events
    131029 12:53:39 [Note] /usr/sbin/mysqld: ready for connections.
    Version: '5.5.34-0ubuntu0.12.04.1-log'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)

这是我第一次看到此错误,并且不确定如何解决此问题,请在此处帮助我。

谢谢

更新

好的,我尝试了glglgl的解决方案,重新启动后,我在错误日志中得到了以下内容:

    131029 13:17:36 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be remo$
    131029 13:17:36 [Note] Plugin 'FEDERATED' is disabled.
    131029 13:17:36 InnoDB: The InnoDB memory heap is disabled
    131029 13:17:36 InnoDB: Mutexes and rw_locks use GCC atomic builtins
    131029 13:17:36 InnoDB: Compressed tables use zlib 1.2.3.4
    131029 13:17:36 InnoDB: Initializing buffer pool, size = 26.0G
    131029 13:17:38 InnoDB: Completed initialization of buffer pool
    131029 13:17:38 InnoDB: highest supported file format is Barracuda.
    131029 13:17:40  InnoDB: Waiting for the background threads to start
    131029 13:17:41 InnoDB: 5.5.34 started; log sequence number 5146431500
    131029 13:17:41 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
    131029 13:17:41 [Note]   - '127.0.0.1' resolves to '127.0.0.1';
    131029 13:17:41 [Note] Server socket created on IP: '127.0.0.1'.  
    131029 13:17:41 [Note] Event Scheduler: Loaded 0 events
    131029 13:17:41 [Note] /usr/sbin/mysqld: ready for connections.
    Version: '5.5.34-0ubuntu0.12.04.1-log'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)

最佳回答

解决了我的问题,检查进程是否在Ubuntu 12.04上运行

ps ax | grep mysql

然后答案是它没有运行,所以我做了

sudo service mysql start

或尝试

sudo /etc/init.d/mysql start

次佳回答

首先,请确认已安装mysql-server。安装mysql-server时出现相同的错误,但是由于某种原因损坏了。我通过完全卸载mysql并重新安装来实现此目的。

sudo apt-get remove --purge mysql*
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get install mysql-server mysql-client

第三种回答

当我在系统上安装xampp时,我遇到了同样的问题。 mysql服务器寻找/var/run/mysqld/mysqld.sock,但是mysql.sock文件在xampp文件夹中,所以我用

 find / -name '*.sock'

查找mysql.sock文件,然后使用

ln -s <the file location> /var/run/mysqld/mysqld.sock

获取* .sock文件的链接,然后尝试mysql,它运行无误。希望这可以解决您的问题。

如果目录不存在,请记住要创建该目录。

第四种回答

客户端的设置应与服务器设置一致。

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

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306

第五种回答

这花了我很长时间才能弄清楚,但您能尝试一下吗?

1-更新和升级

sudo apt-get update
sudo apt-get upgrade

2-清除MySQL服务器和客户端(如果已安装)。

sudo apt-get purge mysql-server mysql-client

3-重新安装MySQL Server和客户端

sudo apt-get install mysql-server mysql-client

4-测试MySQL

mysql -u root -p
> enter root password

***应该在/var/run/mysqld/mysql.sock中找不到套接字

4-使用vi的nano配置mysqld.cnf

须藤nano /etc/mysql/mysql.conf.d/mysqld.cnf

将bind-address从127.0.0.1更改为localhost

bind-address            = localhost

**写入并退出

5-重新启动MySQL

sudo /etc/init.d/mysql restart

6-检查mysql

mysql -u root -p
> enter root password

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.13-0ubuntu0.16.04.2 (Ubuntu)

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>

您现在应该进入mysql cli。

希望这可以帮助

代码

第六种回答

最近我在我的mysql ERROR 2002 (HY000)中遇到了这个错误:

Can’t connect to local MySQL server through socket

‘/var/lib/mysql/mysql.sock’ (111)

因为我忘了开始我的mariadb。只需在您的终端上键入此命令即可。

systemctl start mariadb.service

第七种回答

您可能缺少mysql-server。使用安装

sudo apt-get install mysql-server

第八种回答

这是一个旧的脚步,提出了不同的解决方案,但是没有一个对我有用。我添加此答案是希望它能对像我一样挣扎了一段时间的人有所帮助。

我已经检查了所有现有答案和所有现有解决方案,但是对我来说,问题是对var/run/mysql文件夹的用户权限不正确。

我已经检查了该文件夹的用户权限,并将其设置为root。一旦我更改为mysql:mysql,问题就消失了。

因此,输入/var/run/mysqld并将用户权限更改为:

chown -R mysql:mysql .

第九种回答

我删除了与mysql连接的所有内容

sudo apt-get remove –purge –auto-remove mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server-5.7

sudo rm -r /etc/mysql* /var/lib/mysql* /var/log/mysql*

然后用重新安装

sudo apt-get update

sudo apt-get install mysql-server

然后开始

mysql -u root -p

跟密码

第十种回答

您不需要重新安装MySQL服务器,我遇到了同样的问题,但是我已经通过运行这些命令解决了该问题。

  1  ps -A|grep mysql
  2  sudo pkill mysql
  3  ps -A|grep mysqld
  4  sudo pkill mysqld
  5  sudo service mysql restart
  6  mysql -u root -p

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/8903.html,未经允许,请勿转载。