How to fix MySQL can’t connect to server on localhost (10061) error

MySQL is a widely-used open-source relational database management system, popular for its robustness and ease of use. However, users may sometimes encounter errors while working with MySQL, one of which is the “Can’t connect to server on localhost (10061)” error. This article will delve into the causes of this error and provide solutions to fix it.

Understanding the Error

The “Can’t connect to MySQL server on ‘localhost’ (10061)” error occurs when a MySQL client fails to establish a connection with the MySQL server running on the local machine. There are several possible causes for this error, including a misconfigured MySQL server, incorrect connection settings, or a stopped server.

Fixing the Error

To resolve the “Can’t connect to MySQL server on ‘localhost’ (10061)” error, follow these troubleshooting steps:

  1. Verify if the MySQL server is running:

First, ensure that the MySQL server is running on your local machine. To check the server status, open the command prompt or terminal and execute the following command:

For Windows:

net start

For macOS and Linux:

sudo service mysql status

If the MySQL server is not running, start it with the following command:

For Windows:

net start MySQL

For macOS and Linux:

sudo service mysql start
  1. Check the MySQL server configuration:

If the server is running, verify that the MySQL server configuration file (my.cnf or my.ini) is set up correctly. The file should include the correct bind address (typically “127.0.0.1” or “localhost”) and port number (default is 3306).

For Windows, the my.ini file is usually located in the MySQL installation directory (e.g., C:\Program Files\MySQL\MySQL Server x.x).

For macOS and Linux, the my.cnf file is typically found in /etc/mysql/ or /etc/.

Ensure that the configuration file contains the following lines:

bind-address = 127.0.0.1
port = 3306
  1. Check the connection settings in your application:

Review your application’s connection settings and ensure that they match the settings in the MySQL server configuration file. Check that the host, port number, username, and password are correct.

Example of a connection string in Python using the PyMySQL library:

import pymysql

connection = pymysql.connect(host='127.0.0.1',
                             port=3306,
                             user='your_username',
                             password='your_password',
                             database='your_database')
  1. Disable or configure your firewall:

Sometimes, a firewall may block connections to the MySQL server. To resolve this issue, either disable the firewall temporarily or create a rule to allow connections on the MySQL port (default is 3306).

For Windows, follow these steps to create a rule in the Windows Firewall:

  • Open Control Panel and navigate to System and Security > Windows Defender Firewall > Advanced settings.
  • Select Inbound Rules and click on New Rule.
  • Choose Port and click Next.
  • Enter 3306 as the port number and click Next.
  • Select Allow the connection and click Next.
  • Ensure all profiles are checked and click Next.
  • Provide a name and description for the rule, then click Finish.

For macOS and Linux, refer to your specific firewall’s documentation for instructions on creating a rule or temporarily disabling the firewall.

Conclusion

By following these troubleshooting steps, you can resolve the “Can’t connect to MySQL server on ‘localhost’ (10061)” error and establish a successful connection with the MySQL server. Ensuring the server is running, verifying the configuration file, checking your application’s connection settings, and configuring your firewall are essential steps to address this error. By overcoming this issue, you can continue to leverage the power and flexibility of MySQL in your development projects.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts