MySQL
Error 2003
Database Connection
Troubleshooting
Linux

ERROR 2003 HY000 Can't connect to MySQL server on '127.0.0.1' 111

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Connecting to a MySQL server is a routine task for many developers and system administrators. However, encountering connection errors like ERROR 2003 (HY000) can be particularly frustrating. This specific error code indicates that a connection attempt to the MySQL server at 127.0.0.1 has failed, with the error code (111), meaning "Connection refused."

Understanding the Error

The error can be broken down into its components:

  • ERROR 2003 (HY000): The MySQL client error code that suggests a problem connecting to the MySQL server.
  • Can't connect to MySQL server on '127.0.0.1': Indicates the attempt was made to connect to the MySQL server running on the local machine (localhost).
  • (111): A typical UNIX/Linux error code meaning "Connection refused," which often suggests that the server is not listening on the specified port or is not running.

Common Causes

  1. MySQL Server Not Running:
    • The MySQL service might be stopped. Confirm by running service mysql status or systemctl status mysql to check the service status.
  2. Incorrect Configuration:
    • MySQL configuration (my.cnf or my.ini) might not be set up to listen on 127.0.0.1. Check the [mysqld] section for bind-address=127.0.0.1.
  3. Firewall Restrictions:
    • Firewall settings may block inbound connections to MySQL's default port (3306). Use iptables, firewalld, or equivalent tools to verify and adjust firewall settings.
  4. Port Issues:
    • MySQL might not be set to use the default port 3306. Verify the port setting in your configuration file and connect using --port= if different.
  5. Network Configuration:
    • Network issues could also prevent connection. Confirm network interfaces are up and properly configured.

Diagnostic Steps

  1. Check MySQL Service Status:
    • Open the configuration file typically found at /etc/mysql/my.cnf or /etc/my.cnf.
    • Make sure the relevant section has:
    • View MySQL logs often located in /var/log/mysql/ or /var/log/.
    • On systems with SELinux, ensure it's not preventing access.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.