MySQL Error 2001 (CR_SOCKET_CREATE_ERROR) occurs when the MySQL client cannot create or access a UNIX socket file needed for local database connections. This is typically caused by file descriptor limits, permission issues on the socket directory, or missing socket directories. The error occurs at the OS level before reaching the MySQL server.
MySQL uses UNIX socket files for local connections between the client and server on Unix-like systems. Error 2001 (CR_SOCKET_CREATE_ERROR) means the MySQL client failed to create or open the socket file at the operating system level. Unlike Error 2002 (CR_CONNECTION_ERROR) which indicates the socket exists but the server is unreachable, Error 2001 fails during socket creation itself, indicating an operating system resource constraint or permission restriction. Key aspects of this error: 1. **Client-Side Error**: This is a client-side error that occurs before any communication with the MySQL server happens. The failure occurs in the underlying system call. 2. **Socket Path**: By default, MySQL expects the socket at /var/run/mysqld/mysqld.sock on most Linux systems. The socket file must be writable by the connecting user. 3. **Error Codes**: Error 2001 often includes additional system error codes like (2) for ENOENT (file not found), (13) for EACCES (permission denied), or (24) for EMFILE (too many open files). 4. **Local Connections Only**: This error only affects local connections using socket protocol. Remote TCP connections (e.g., -h 127.0.0.1) bypass socket creation entirely. 5. **Operating System Impact**: This is fundamentally an OS-level issue, not a MySQL server configuration issue. The MySQL daemon may be running fine; the problem is at the client level.
The most common cause is hitting the OS file descriptor limit. Check your limits:
# View current limits for current shell
ulimit -n
ulimit -a
# View limits for mysqld process
cat /proc/$(pgrep mysqld)/limits | grep "open files"
# View descriptor usage
lsof -p $(pgrep mysqld) | wc -l
ls /proc/$(pgrep mysqld)/fd | wc -lIf soft limit is 1024 or lower and file descriptor count is near that limit, this is your issue. Each MySQL connection uses several file descriptors.
For immediate testing, increase the limit in your shell before attempting MySQL connection:
# Increase for current shell
ulimit -n 4096
# Verify change
ulimit -n
# Now try connecting to MySQL
mysql -u root -pIf this fixes error 2001, the root cause is file descriptors. Proceed to permanent configuration.
To persist file descriptor limits across reboots, edit systemd service configuration:
# Edit MySQL systemd service
sudo systemctl edit mysql
# Add or modify the [Service] section:
[Service]
LimitNOFILE=4096
LimitNPROC=4096
# Save (Ctrl+X, Y, Enter)
# Or edit limits.conf for system-wide settings
sudo nano /etc/security/limits.conf
# Add at the end:
mysql soft nofile 4096
mysql hard nofile 10240
mysql soft nproc 4096
mysql hard nproc 10240
# Apply changes
sudo systemctl daemon-reload
sudo systemctl restart mysql
# Verify new limits
cat /proc/$(pgrep mysqld)/limits | grep "open files"
ulimit -nFor containers/Docker, set limits in docker-compose or Dockerfile.
Check if the socket directory exists and is properly configured:
# Check if directory exists
ls -la /var/run/mysqld/
# If directory doesn't exist, create it
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo chmod 755 /var/run/mysqld
# If socket file exists but has wrong permissions
sudo ls -la /var/run/mysqld/mysqld.sock
# Fix socket file permissions
sudo chown mysql:mysql /var/run/mysqld/mysqld.sock
sudo chmod 660 /var/run/mysqld/mysqld.sock
# Verify permissions (mysql should be owner and group)
ls -la /var/run/mysqld/
# Should show: drwxr-xr-x mysql:mysql mysqldIf permissions are wrong but you can't fix them, the socket directory may be on a temporary filesystem.
Verify /var/run has sufficient space and is writable:
# Check free space on /var/run
df /var/run
df -h /var/run
# Check if filesystem is mounted read-only
mount | grep "/var/run"
# Should show "rw" (read-write), not "ro" (read-only)
# If read-only, remount as read-write
sudo mount -o remount,rw /var/run
# Check for disk full globally
df -h /
du -sh /var/run
# List large files in /var/run
ls -lhS /var/run | headIf /var/run is on tmpfs (RAM filesystem), ensure system has sufficient RAM available.
While troubleshooting socket issues, use TCP connections instead:
# Force TCP protocol (bypass socket)
mysql -h 127.0.0.1 -u root -p --protocol=TCP
# In application connection strings
# Instead of: socket=/var/run/mysqld/mysqld.sock
# Use: host=127.0.0.1, port=3306
# PHP PDO
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=mydb";
# Python
connection = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="root",
password="password"
)
# Node.js
const connection = mysql.createConnection({
host: '127.0.0.1',
port: 3306,
user: 'root'
});TCP connections work when socket creation fails and help isolate the issue to socket/filesystem problems.
Review MySQL error log for more detailed error information:
# View MySQL error log
sudo tail -n 100 /var/log/mysql/error.log
# or for MariaDB
sudo tail -n 100 /var/log/mariadb/mariadb.log
# Search for socket-related errors
sudo grep -i socket /var/log/mysql/error.log
sudo grep -i "too many open files" /var/log/mysql/error.log
# Watch log in real-time
sudo tail -f /var/log/mysql/error.log
# Check systemd journal for startup errors
sudo journalctl -u mysql -n 50
sudo journalctl -u mysql -f
# Look for permission or resource errors
sudo journalctl -u mysql -p errSpecific error messages indicate: "Permission denied" = chmod issue, "No such file" = directory missing, "Too many open files" = descriptor limit.
Ensure MySQL server and client use the same socket path:
# Query MySQL server for configured socket path
mysql -e "SHOW VARIABLES LIKE 'socket';" 2>/dev/null || echo "Server not running"
# Check configuration files
grep -r "^socket" /etc/mysql/
grep -r "^socket" ~/.my.cnf
# Common socket locations
# /var/run/mysqld/mysqld.sock - Linux standard
# /var/lib/mysql/mysql.sock - Some distributions
# /tmp/mysql.sock - Fallback location
# /var/run/mysqld/mysqld.sock - Debian/Ubuntu
# Verify client config uses same path
mysql --help | grep socket
# If mismatch exists, update configuration
sudo nano /etc/mysql/my.cnf
# Ensure both server and client sections use same path:
[mysqld]
socket=/var/run/mysqld/mysqld.sock
[client]
socket=/var/run/mysqld/mysqld.sock
# Restart MySQL
sudo systemctl restart mysqlClient and server must agree on socket location.
Security policies may block socket creation:
# Check SELinux status (CentOS/RHEL)
getenforce
# Output: Enforcing, Permissive, or Disabled
# View recent SELinux denials
sudo ausearch -m avc -ts recent | grep mysql
sudo grep -i mysql /var/log/audit/audit.log | tail -20
# Check AppArmor status (Ubuntu/Debian)
sudo aa-status | grep mysql
# View AppArmor denials
sudo dmesg | grep -i apparmor | grep mysql
sudo journalctl | grep apparmor | grep mysql
# Temporarily disable SELinux to test (TESTING ONLY)
sudo setenforce 0
# Temporarily set MySQL to AppArmor complain mode
sudo aa-complain /usr/sbin/mysqld
# If issue disappears, configure proper policies rather than disabling
# Re-enable SELinux/AppArmor
sudo setenforce 1
sudo aa-enforce /usr/sbin/mysqldIf disabling security modules fixes the issue, configure proper policies. Don't run with security disabled.
Advanced considerations for Error 2001:
1. File Descriptor Exhaustion: Long-running applications with connection leaks, high concurrency without proper pooling, or cron jobs that don't close file handles commonly trigger this. Monitor with lsof -p $(pgrep mysqld) and identify which processes hold file descriptors.
2. Container/Docker Environments: In Docker, /var/run/mysqld may be on tmpfs (RAM filesystem). Ensure the container has sufficient memory allocated and the volume mount permissions are correct. The entrypoint script must create the directory with proper ownership before starting mysqld.
3. Systemd-specific Issues: Modern systemd installations may use PrivateTmp=yes, creating isolated /tmp and /var/run for the service. This can cause socket visibility issues between service and client. Review the service file with systemctl cat mysql and consider PrivateTmp=no if needed.
4. File Descriptor Limits per User vs Process: The limits.conf settings apply per-user login session. Systemd LimitNOFILE applies directly to the service process. For containers, set limits in docker-compose resource limits or Dockerfile ENV.
5. Error 2001 vs Error 2002: Error 2001 fails at socket creation (OS-level). Error 2002 succeeds at socket creation but fails to connect (server not running or listening). If toggling TCP fixes it but socket still fails, it's permission/resource issue (2001). If both fail, it's server connectivity (2002).
6. Monitoring File Descriptors: Use watch -n1 "lsof -p $(pgrep mysqld) | wc -l" to monitor file descriptor growth. Sudden jumps indicate connection leaks.
7. MySQL 8.0+ and Unix Socket Authentication: MySQL 8.0+ supports unix_socket authentication plugin. Ensure both plugin and UNIX_SOCKET_PLUGIN are properly configured if using socket-based authentication.
8. Testing with Different Users: Socket errors may be user-specific. Test with sudo -u mysql mysql -u root -p vs mysql -u root -p to isolate permission issues.
9. Kernel Parameters: On heavily loaded systems, increase net.core.somaxconn and other TCP parameters. For local sockets, ensure adequate memory in /var/run tempfs: mount -o remount,size=512M /var/run.
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