Diagnosis and Solutions for socket.error: [Errno 111] Connection refused When Connecting to MySQL with PyMySQL

Dec 04, 2025 · Programming · 11 views · 7.8

Keywords: PyMySQL | MySQL Connection Error | Unix Socket

Abstract: This article provides an in-depth analysis of the socket.error: [Errno 111] Connection refused error encountered when using PyMySQL to connect to a local MySQL database. By comparing the connection mechanisms of MySQLdb and PyMySQL, it reveals that this error typically stems from mismatched Unix socket paths or port configurations. Two core solutions are presented: explicitly specifying the correct Unix socket path obtained via mysqladmin commands, and verifying and manually setting the correct MySQL port number. The article also explores best practices for connection parameter configuration, including behavioral differences in host parameters and connection parameter precedence, offering comprehensive troubleshooting guidance for Python developers.

Problem Background and Error Manifestation

In Python database programming, PyMySQL, as a pure-Python MySQL client library, is widely adopted for its cross-platform compatibility and full support for Python 3. However, developers may encounter specific connection errors when connecting to MySQL databases from local environments. A typical scenario is as follows: developers confirm that the MySQL service is running (connectable via the mysql command-line tool or phpMyAdmin), but using PyMySQL throws the following exceptions:

socket.error: [Errno 111] Connection refused
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (111)")

Notably, the same connection parameters succeed when using the MySQLdb library, suggesting that the issue may arise from differences in the underlying connection mechanisms between PyMySQL and MySQLdb.

Root Cause Analysis

Although PyMySQL and MySQLdb offer similar API interfaces, they exhibit key differences in handling local connections. When the host parameter is set to 'localhost', the default behaviors of the two libraries diverge:

This discrepancy can cause PyMySQL to fail to locate the correct communication endpoint even when the MySQL service is operational, triggering the "Connection refused" error. Error code 111 (ECONNREFUSED) explicitly indicates that the connection request was rejected by the target server, often due to incorrect socket paths or port mismatches.

Solution 1: Explicitly Specify the Unix Socket Path

The first solution involves determining the actual Unix socket file path used by the MySQL server and explicitly specifying it in the PyMySQL connection. The specific steps are as follows:

  1. Obtain the Current MySQL Socket Path: Execute the system command mysqladmin variables | grep socket to retrieve the MySQL server's configuration information. This command outputs results similar to:
  2. | socket | /tmp/mysql.sock |

    Here, /tmp/mysql.sock is the Unix socket file path on which the MySQL server is listening. Different systems or installation methods may result in varying paths, with common alternatives including /var/run/mysqld/mysqld.sock or /var/lib/mysql/mysql.sock.

  3. Modify the PyMySQL Connection Code: Add the unix_socket parameter to the connection arguments, pointing to the correct socket file:
  4. import pymysql
    conn = pymysql.connect(
        db='base',
        user='root',
        passwd='pwd',
        unix_socket="/tmp/mysql.sock"  # Use the actual path obtained
    )

    By explicitly specifying the unix_socket parameter, PyMySQL will directly use the Unix domain socket for connection, bypassing the default TCP/IP connection attempt that may cause issues.

Solution 2: Verify and Set the Correct Port Number

If the Unix socket method is not applicable or the developer prefers TCP/IP connections, the second solution is to verify and correctly configure the port number:

  1. Confirm the MySQL Listening Port: Execute the command mysqladmin variables | grep port, with example output as follows:
  2. | port | 3306 |

    The standard MySQL default port is 3306, but some installations may be configured to use other ports (e.g., 3307, 3308).

  3. Specify the Port in the Connection: If the port is not the default 3306, it must be explicitly specified in the PyMySQL connection:
  4. import pymysql
    conn = pymysql.connect(
        db='base',
        user='root',
        passwd='pwd',
        host='localhost',
        port=3307  # Use the actual port number obtained
    )

    Even if the port is the default 3306, explicit specification can prevent issues arising from differences in the library's default behavior.

Best Practices for Connection Parameter Configuration

Based on the above analysis, we summarize the following best practices for PyMySQL connection configuration:

In-Depth Technical Details

Understanding the essence of PyMySQL connection errors requires delving into network programming and database client implementation:

Conclusion and Recommendations

The "Connection refused" error when connecting to MySQL with PyMySQL is typically not due to MySQL service issues but rather mismatches between client configuration and the server's actual setup. Through the methods introduced in this article, developers can:

  1. Use the mysqladmin variables command to accurately obtain the MySQL server's socket path and port configuration.
  2. Explicitly specify the correct unix_socket or port parameters in PyMySQL connection code.
  3. Understand behavioral differences among various MySQL Python client libraries and write more robust connection code.

For persistent connection issues, additional factors such as MySQL server configuration (e.g., bind-address settings), firewall rules, and file permissions (readability of socket files) should be examined. Through systematic troubleshooting, PyMySQL can be ensured to connect reliably to MySQL databases across diverse environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.