【问题标题】:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) on Lubuntu 20.04 LTSERROR 2002 (HY000): Can't connect to local MySQL server through socket'/var/run/mysqld/mysqld.sock' (2) on Lubuntu 20.04 LTS
【发布时间】:2021-03-18 02:39:52
【问题描述】:

首先,我在我的Lubuntu 20.04 LTS系统上检查了mysql的正确安装:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo apt install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
mysql-server is already the newest version (8.0.22-0ubuntu0.20.04.3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

接下来,我尝试连接mysql: (scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo mysql -u root ERROR 2002 (HY000): 无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2) (scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

当尝试通过sudo systemctl stop mysql 停止mysql 时,终端半分钟以上没有响应,但最终停止了进程。 然后,我执行sudo systemctl restart mysql,同样的等待游戏超过一分钟左右。 最后显示如下错误信息:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo systemctl restart mysql
Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xe" for details.

马上查看,上面写着:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ systemctl status mysql.service
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: activating (start) since Sun 2020-12-06 17:46:48 CET; 30s ago
    Process: 18803 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 18811 (mysqld)
     Status: "Server startup in progress"
      Tasks: 13 (limit: 9257)
     Memory: 244.7M
     CGroup: /system.slice/mysql.service
             └─18811 /usr/sbin/mysqld

Dec 06 17:46:48 andylu-Lubuntu-PC systemd[1]: Starting MySQL Community Server...

另一个要检查的命令打印出以下内容:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ journalctl -xe
-- The job identifier is 7118 and the job result is done.
Dec 06 17:48:29 andylu-Lubuntu-PC systemd[1]: Starting MySQL Community Server...
-- Subject: A start job for unit mysql.service has begun execution
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- A start job for unit mysql.service has begun execution.
-- 
-- The job identifier is 7118

除了在终端中使用MySQL,我已经安装了python mysqlclient,如下所示:

sudo apt install default-libmysqlclient-dev
pip install mysqlclient

现在,在我的ipython-shell 中,我尝试像这样连接到我的根mySQL-database,结果与标准bash 终端中的错误相同:

Python 3.9.0 (default, Nov 22 2020, 23:12:14) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import MySQLdb

In [2]: mydb = MySQLdb.connect(host='localhost', user='root', passwd='astralux13')
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-2-8747acd85ff9> in <module>
----> 1 mydb = MySQLdb.connect(host='localhost', user='root', passwd='astralux13')

~/.virtualenvs/scrapy_course/lib/python3.9/site-packages/MySQLdb/__init__.py in Connect(*args, **kwargs)
    128     from MySQLdb.connections import Connection
    129 
--> 130     return Connection(*args, **kwargs)
    131 
    132 

~/.virtualenvs/scrapy_course/lib/python3.9/site-packages/MySQLdb/connections.py in __init__(self, *args, **kwargs)
    183         autocommit = kwargs2.pop("autocommit", False)
    184 
--> 185         super().__init__(*args, **kwargs2)
    186         self.cursorclass = cursorclass
    187         self.encoders = {k: v for k, v in conv.items() if type(k) is not int}

OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

似乎我无法通过此错误并且不知道该尝试其他什么了。 我打算实施以下讨论中发布的一些顶级解决方案,但无济于事:

【问题讨论】:

  • 如果指定 `host=127.0.0.1' 而不是 localhost 是否有效?
  • 在命令行中,是的,我试过这个主机,但它也不起作用。现在,一切正常,我终于可以更改密码了,感谢以下帖子:stackoverflow.com/a/58921619/12298276

标签: mysql sockets mysql-python


【解决方案1】:

this post 的启发,对我有用的是以下内容(杀死仍然打开的 mysql 进程):

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ ps -A|grep mysql
  13308 pts/2    00:00:31 mysqld
  19980 ?        00:00:00 mysqld
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo pkill mysql
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ ps -A|grep mysql
  19980 ?        00:00:00 mysqld
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo pkill mysql
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ ps -A|grep mysql
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ ps -A|grep mysqld
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo service mysql restart

根据上述帖子,我应该通过mysql -u root -p 连接,但在我的情况下导致错误:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

现在,使用 sudo 同时避免密码标志“-p”它终于起作用了:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.22-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2020, 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更改密码,我也尝试了一堆对我不起作用的解决方案,直到我幸运地找到了this solution。 这解决了OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)"),当我想在我的 ipython shell 中登录时发生:

In [4]: mydb = MySQLdb.connect(host='localhost', user='root', passwd='blablabalbala')
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-4-8747acd85ff9> in <module>
----> 1 mydb = MySQLdb.connect(host='localhost', user='root', passwd='astralux13')

~/.virtualenvs/scrapy_course/lib/python3.9/site-packages/MySQLdb/__init__.py in Connect(*args, **kwargs)
    128     from MySQLdb.connections import Connection
    129 
--> 130     return Connection(*args, **kwargs)
    131 
    132 

~/.virtualenvs/scrapy_course/lib/python3.9/site-packages/MySQLdb/connections.py in __init__(self, *args, **kwargs)
    183         autocommit = kwargs2.pop("autocommit", False)
    184 
--> 185         super().__init__(*args, **kwargs2)
    186         self.cursorclass = cursorclass
    187         self.encoders = {k: v for k, v in conv.items() if type(k) is not int}

OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

现在,一切正常,我什至可以更改密码。

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 2012-09-19
    • 2019-09-28
    • 2021-07-25
    • 2018-08-01
    • 2017-03-18
    • 2021-08-15
    • 1970-01-01
    • 2022-06-30
    相关资源
    最近更新 更多