This MySQL connection error occurs when a client application cannot establish a local socket connection to the MySQL server. The error typically indicates that the MySQL server is not running, the socket file path is incorrect, or there are permission issues preventing access to the socket file. Local socket connections are commonly used for connections from applications running on the same server as the database.
The CR_CONNECTION_ERROR (2002) is a MySQL client error that occurs when attempting to connect to a local MySQL server via a UNIX domain socket. Unlike TCP/IP connections that use network ports, socket connections use a special file on the filesystem for inter-process communication. Key aspects of this error: 1. **Socket Communication**: MySQL uses UNIX domain sockets for local connections (when connecting to "localhost" or "127.0.0.1" without specifying a port). This is more efficient than TCP/IP for local connections. 2. **Socket File Location**: The socket file (typically `/tmp/mysql.sock`, `/var/run/mysqld/mysqld.sock`, or `/var/lib/mysql/mysql.sock`) must exist and be accessible. 3. **Server Status**: The MySQL server process must be running and listening on the socket file. 4. **Permission Issues**: The client process must have appropriate filesystem permissions to read/write to the socket file. This error is most common in development environments, local installations, and when MySQL is configured to use non-standard socket paths.
First, verify that the MySQL server process is actually running:
# Check MySQL service status (systemd)
systemctl status mysql
# or
systemctl status mysqld
# Check for MySQL process
ps aux | grep mysql
ps aux | grep mysqld
# Check if MySQL is listening on socket
sudo netstat -lnp | grep mysql
sudo ss -lnp | grep mysql
# Try to start MySQL if not running
sudo systemctl start mysql
# or
sudo service mysql startIf MySQL is not running, start it and check logs for startup errors:
# Check MySQL error log
sudo tail -f /var/log/mysql/error.log
# or
sudo tail -f /var/log/mysqld.log
# Common locations for MySQL logs:
# /var/log/mysql/error.log
# /var/log/mysqld.log
# /var/lib/mysql/hostname.errCheck where MySQL is creating the socket file and ensure it's accessible:
# Find MySQL socket file location from server configuration
mysql --help | grep "socket"
# or check MySQL configuration
sudo grep -r "socket" /etc/mysql/
# Common socket file locations:
# /tmp/mysql.sock
# /var/run/mysqld/mysqld.sock
# /var/lib/mysql/mysql.sock
# /run/mysqld/mysqld.sock
# Check if socket file exists
ls -la /tmp/mysql.sock
ls -la /var/run/mysqld/mysqld.sock
# Check permissions on socket file and directory
ls -la /var/run/mysqld/
stat /var/run/mysqld/mysqld.sock
# Socket file should be accessible to your user
# Typical permissions: srwxrwxrwx (socket, world readable/writable)
# Fix permissions if needed
sudo chmod 777 /var/run/mysqld/mysqld.sock
sudo chown mysql:mysql /var/run/mysqld/mysqld.sockNote: The socket file is created by the MySQL server when it starts. If it doesn't exist, MySQL may not be running or configured to create it.
Ensure your application is using the correct socket path:
// PHP PDO example with socket
$dsn = "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=mydatabase";
$pdo = new PDO($dsn, $username, $password);
// PHP mysqli example
$mysqli = new mysqli("localhost", $user, $password, $database, null, "/var/run/mysqld/mysqld.sock");
// In php.ini
mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock# Python MySQL connector
import mysql.connector
config = {
'user': 'username',
'password': 'password',
'database': 'mydb',
'unix_socket': '/var/run/mysqld/mysqld.sock'
}
connection = mysql.connector.connect(**config)// Node.js with mysql2
const mysql = require('mysql2/promise');
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydb',
socketPath: '/var/run/mysqld/mysqld.sock'
});Check your framework or ORM documentation for socket configuration options.
If socket connections continue to fail, switch to TCP/IP connection:
# MySQL command line - use TCP/IP instead of socket
mysql -h 127.0.0.1 -P 3306 -u username -p
# Force TCP/IP with --protocol flag
mysql --protocol=tcp -u username -pIn application code, specify host as 127.0.0.1 with port 3306:
// PHP - TCP/IP connection
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=mydatabase";
$pdo = new PDO($dsn, $username, $password);# Python - TCP/IP
import mysql.connector
config = {
'host': '127.0.0.1',
'port': 3306,
'user': 'username',
'password': 'password',
'database': 'mydb'
}
connection = mysql.connector.connect(**config)Ensure MySQL is configured to listen on TCP/IP:
-- Check MySQL bind address
SHOW VARIABLES LIKE 'bind_address';
-- Should be '*' or '0.0.0.0' or '127.0.0.1' for local TCP/IP
-- In my.cnf:
[mysqld]
bind-address = 0.0.0.0 # or 127.0.0.1 for local only
port = 3306Verify and update MySQL server socket configuration:
# Check current socket configuration
mysql -e "SHOW VARIABLES LIKE 'socket';"
# Common configuration files:
# /etc/mysql/my.cnf
# /etc/mysql/mysql.conf.d/mysqld.cnf
# /etc/my.cnf
# ~/.my.cnf
# Example socket configuration in my.cnf:
[mysqld]
socket = /var/run/mysqld/mysqld.sock
[client]
socket = /var/run/mysqld/mysqld.sock
[mysql]
socket = /var/run/mysqld/mysqld.sockIf you need to change the socket location:
# Stop MySQL
sudo systemctl stop mysql
# Edit configuration
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
# Add or modify socket line under [mysqld]
socket = /tmp/mysql.sock
# Also update [client] and [mysql] sections
# Restart MySQL
sudo systemctl start mysql
# Verify new socket file exists
ls -la /tmp/mysql.sockNote: All clients must use the same socket path as the server.
Check for permission problems and security restrictions:
# Check socket file ownership and permissions
ls -la /var/run/mysqld/mysqld.sock
# Should be owned by mysql user and accessible
# Check directory permissions
ls -ld /var/run/mysqld/
# Check SELinux status
getenforce
sestatus
# If SELinux is enforcing, check for denials
sudo ausearch -m avc -ts recent | grep mysql
sudo grep mysql /var/log/audit/audit.log
# Temporarily disable SELinux to test (not recommended for production)
sudo setenforce 0
# Check AppArmor on Ubuntu/Debian
sudo aa-status | grep mysql
# Check if user has access to socket
sudo -u www-data ls -la /var/run/mysqld/mysqld.sock
# Common fixes:
sudo chmod 755 /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld/mysqld.sock
sudo chmod 777 /var/run/mysqld/mysqld.sock # Less secure, but works
# Add user to mysql group
sudo usermod -a -G mysql $USER
# Log out and back in for group changes to take effectThe CR_CONNECTION_ERROR (2002) has several advanced considerations:
1. Multiple MySQL Instances: When running multiple MySQL instances on the same server, each must use a different socket file path. Common in development environments with Docker containers or multiple MySQL versions.
2. Docker and Containerization: In Docker, MySQL containers typically expose TCP/IP port 3306 but may not have persistent socket files. Use TCP/IP connections (127.0.0.1:3306) or volume-mount socket directories.
3. MySQL 8.0 Socket Changes: MySQL 8.0 may use different default socket paths than older versions. Check version-specific documentation.
4. Connection Pooling: Connection pools may cache socket connections that become stale. Implement connection validation or use TCP/IP with proper timeout settings.
5. Filesystem Types: Some network filesystems or special filesystem types may not properly support UNIX domain sockets. Ensure MySQL socket is on a local filesystem.
6. Systemd and tmpfiles.d: Modern Linux distributions using systemd may manage MySQL socket through tmpfiles.d configuration. Check /usr/lib/tmpfiles.d/mysql.conf or similar.
7. MySQL Router and Proxy: When using MySQL Router or other proxies, connection behavior may differ from direct MySQL server connections.
For production systems, consider using TCP/IP connections (even locally) for better monitoring, connection pooling, and consistency across environments.
EE_WRITE (3): Error writing file
How to fix "EE_WRITE (3): Error writing file" in MySQL
CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters
How to fix "CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters" in MySQL
CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed
How to fix "CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed" in MySQL
ERROR 1146: Table 'database.table' doesn't exist
How to fix "ERROR 1146: Table doesn't exist" in MySQL
ERROR 1040: Too many connections
How to fix "ERROR 1040: Too many connections" in MySQL