简体   繁体   中英

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

$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 )

Before running the above lines, I have firstly activated XAMPP and I have enabled port localhost:8080 . phpMyAdmin is located at 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:

import pymysql

conn = pymysql.connect(host='localhost', user='root', password='', db='Work')

(The directory of this script is /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:

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?

Also, keep in mind that even if I replace localhost with 127.0.0.1 I am getting exactly the same error. 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 ).

PyMySQL accepts port as an independent parameter. Try this out:

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='Work')

Refer this link : PyMySQL

Just I followed the steps given in below URL, they give sample code to connect from MySQLdb plugin and PyMySQL plugin.

For me MySQLdb gives output and PyMySQL gives the connection error as like you get.

https://www.a2hosting.in/kb/developer-corner/mysql/connecting-to-mysql-using-python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM