[英]Python script (PyMySQL) does not connect to database on localhost
When I am using PHP I can connect to the phpMyAdmin at the localhost simply by writing the following lines in my php script: 当我使用PHP时,只需在php脚本中编写以下行即可连接到localhost上的phpMyAdmin:
<?php
$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'Work';
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>
(The directory of this script is /Users/User/.bitnami/stackman/machines/xampp/volumes/root/htdocs/index.php
) (此脚本的目录是
/Users/User/.bitnami/stackman/machines/xampp/volumes/root/htdocs/index.php
)
Before running the above lines, I have firstly activated XAMPP
and I have enabled port localhost:8080
. 在运行上述行之前,我首先激活了
XAMPP
并启用了端口localhost:8080
。 phpMyAdmin
is located at http://localhost:8080/phpmyadmin/
. phpMyAdmin
位于http://localhost:8080/phpmyadmin/
。 This connects perfectly fine to the database. 这样可以很好地连接到数据库。
Now, in Python I have downloaded PyMySQL
and I have written the following lines to do the same simple task: 现在,在Python中,我下载了
PyMySQL
,并编写了以下几行代码来完成相同的简单任务:
import pymysql
conn = pymysql.connect(host='localhost', user='root', password='', db='Work')
(The directory of this script is /Users/User/PycharmProjects/First/Main.py
) (此脚本的目录是
/Users/User/PycharmProjects/First/Main.py
)
However, when I try to run this (after I have firstly activated XAMPP
and I have enabled port localhost:8080
) I am getting the following error: 但是,当我尝试运行此命令(首先激活
XAMPP
并启用端口localhost:8080
)之后,出现以下错误:
Traceback (most recent call last):
File "/Users/User/Library/Python/3.6/lib/python/site-packages/pymysql/connections.py", line 920, in connect
**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/User/PycharmProjects/GCP-OCR/Main.py", line 14, in <module>
conn = pymysql.connect(host='localhost', user='root', password='', db='Work')
File "/Users/User/Library/Python/3.6/lib/python/site-packages/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/Users/User/Library/Python/3.6/lib/python/site-packages/pymysql/connections.py", line 699, in __init__
self.connect()
File "/Users/User/Library/Python/3.6/lib/python/site-packages/pymysql/connections.py", line 967, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connection refused)")
How can I fix this error and finally connect my Python script to the MySQL database? 如何解决此错误,最后将我的Python脚本连接到MySQL数据库?
Also, keep in mind that even if I replace localhost
with 127.0.0.1
I am getting exactly the same error. 此外,请记住,即使我将
localhost
替换为127.0.0.1
,也会遇到完全相同的错误。 Moreover, if I replace localhost
with localhost:3306
then I am an error too ( socket.gaierror: [Errno 8] nodename nor servname provided, or not known
). 而且,如果我用
localhost:3306
替换localhost
,那么我也是一个错误( socket.gaierror: [Errno 8] nodename nor servname provided, or not known
)。
Just I followed the steps given in below URL, they give sample code to connect from MySQLdb plugin and PyMySQL plugin. 只要我按照下面URL中给出的步骤进行操作,它们就会提供示例代码以连接MySQLdb插件和PyMySQL插件。
For me MySQLdb gives output and PyMySQL gives the connection error as like you get. 对我来说,MySQLdb提供输出,而PyMySQL提供连接错误,就像您得到的一样。
https://www.a2hosting.in/kb/developer-corner/mysql/connecting-to-mysql-using-python https://www.a2hosting.in/kb/developer-corner/mysql/connecting-to-mysql-using-python
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.