Home >Database >Mysql Tutorial >Why Can\'t PyMySQL Connect to MySQL on Localhost?
pymysql Cannot Connect to MySQL on Localhost
When attempting to establish a connection to MySQL on localhost using PyMySQL, an error typically arises:
socket.error: [Errno 111] Connection refused pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (111)")
Despite the confirmation that MySQL is running, the connection fails using PyMySQL, while MySQLdb allows for a successful connection on Python 2.
Potential Solutions:
pymysql.connect(db='base', user='root', passwd='pwd', unix_socket="/tmp/mysql.sock")
pymysql.connect(db='base', user='root', passwd='pwd', host='localhost', port=XXXX)
One of these approaches should resolve the connection issue between PyMySQL and MySQL on localhost.
The above is the detailed content of Why Can\'t PyMySQL Connect to MySQL on Localhost?. For more information, please follow other related articles on the PHP Chinese website!