"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Am I Getting \"Connection Refused\" Errors When Connecting to MySQL with PyMySQL?

Why Am I Getting \"Connection Refused\" Errors When Connecting to MySQL with PyMySQL?

Published on 2024-11-08
Browse:275

Why Am I Getting \

Troubleshooting "Connection Refused" Error when Connecting to MySQL with PyMySQL

When attempting to establish a connection to MySQL on localhost using PyMySQL, some users encounter the error "socket: [Errno 111] Connection refused." This can be frustrating, especially when MySQL is known to be running.

To understand the cause of this issue, let's examine two potential reasons:

  1. Incorrect socket path: PyMySQL may be attempting to connect to a different socket location than where MySQL is listening. To rectify this, run the command mysqladmin variables | grep socket to determine the socket's location. Then, set the unix_socket parameter when connecting, as follows:
pymysql.connect(db='base', user='root', passwd='pwd', unix_socket="/tmp/mysql.sock")
  1. Mismatched port: MySQL may be listening on a port other than the default 3306. Verify this by running mysqladmin variables | grep port. If necessary, specify the correct port when connecting:
pymysql.connect(db='base', user='root', passwd='pwd', host='localhost', port=XXXX)

By addressing one of these potential causes, you should be able to successfully establish a connection to MySQL on localhost using PyMySQL.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3