【发布时间】: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