Common SQL Errors

MySQL Error 2002: CR_CONNECTION_ERROR - How to Fix Local Connection Failures

Galaxy Team
August 5, 2025

MySQL error 2002 (CR_CONNECTION_ERROR) means the client cannot connect to the local MySQL server, usually because the socket path, host, port, or server process is incorrect or unavailable.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error code 2002?

MySQL Error 2002: CR_CONNECTION_ERROR signals the client cannot reach the local MySQL server via the expected socket or TCP port. Confirm mysqld is running, verify the socket file or host and port, and update the client or my.cnf path to restore the connection.

Error Highlights

Typical Error Message

Can't connect to local MySQL server through socket '%s' (%d)

Error Type

Connection Error

Language

MySQL

Symbol

CR_CONNECTION_ERROR

Error Code

2002

SQL State

Explanation

Table of Contents

What is MySQL Error 2002: CR_CONNECTION_ERROR?

MySQL error 2002 appears when the client library tries to open a network or UNIX-socket connection but fails. The accompanying message, "Can't connect to local MySQL server through socket '%s' (%d)", tells you which socket file or TCP endpoint it attempted.

This is a client-side error raised before authentication. It indicates the server process is unreachable, the socket file path is wrong, or the TCP port is closed.

Fixing it quickly restores database availability for applications and scripts.

.

What Causes This Error?

The error usually arises when mysqld is not running, has crashed, or is listening on a different path or port. Misconfigured my.cnf files, wrong --socket parameters, and firewall rules are frequent culprits.

It can also occur after upgrades that change the default socket location, when Docker containers map ports incorrectly, or when file permissions block access to the .sock file.

How to Fix MySQL Error 2002: CR_CONNECTION_ERROR

First verify the server is running: systemctl status mysqld or docker ps. If it is down, start or restart it. Next, confirm the socket path with ps -ef | grep mysqld and match it to client settings in /etc/my.cnf or ~/.my.cnf.

If you use TCP, test with telnet 127.0.0.1 3306. Open the port in the firewall if the connection times out. Update client commands with --host=127.0.0.1 --port=3306 to bypass socket issues.

Common Scenarios and Solutions

After macOS Homebrew upgrades, mysqld may relocate its socket to /tmp/mysql.sock. Point the client there or create a symlink. Inside Docker, map 3306 correctly and connect to the container IP, not localhost.

For Galaxy users, set the correct socket or TCP parameters in the connection dialog. Galaxy saves these settings, preventing repeated errors and making collaboration seamless.

Best Practices to Avoid This Error

Always run mysqld under a process manager and enable automatic restarts. Keep a single authoritative my.cnf and document socket paths. Use TCP 127.0.0.1 connections in scripts for portability.

Monitor the MySQL port with a health probe and alert on downtime. Galaxy’s SQL editor surfaces connection issues immediately, allowing quick fixes before they impact shared queries.

Related Errors and Solutions

Error 2003: CR_CONN_HOST_ERROR arises when connecting over TCP to a remote host. It often shares the same root causes but involves network routing or firewalls.

Error 2006: MySQL server has gone away occurs after a successful connection drops. Tuning wait_timeout and net_write_timeout prevents it.

Common Causes

mysqld not running

The server process is stopped, crashed, or failed to start during boot, leaving no socket or listening port.

Incorrect socket path

The client looks for /var/run/mysqld/mysqld.sock while the server created /tmp/mysql.sock after an upgrade.

Port 3306 blocked

A local firewall or Docker network rule prevents TCP connections to 127.0.0.1:3306.

File permission issues

The client UID lacks read/write access to the .sock file, especially after running mysqld under a different user.

Configuration drift

Multiple my.cnf files override each other, causing the client and server to reference different sockets or ports.

.

Related Errors

FAQs

Why does MySQL create the socket in /tmp?

Certain installations default to /tmp for easier access. Update my.cnf or create a symlink if your client expects /var/run.

Can I force MySQL to use TCP instead of a socket?

Yes. Connect with --protocol=TCP or set host to 127.0.0.1. This bypasses socket lookup entirely.

Is restarting mysqld safe?

Restarting is generally safe during maintenance windows. Always flush logs and inform applications to minimize impact.

How does Galaxy handle connection errors?

Galaxy surfaces detailed error messages in the editor and lets you save multiple connection profiles, reducing misconfigurations across teams.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo