How to fix CR_IPSOCK_ERROR (2004): Can't create TCP/IP socket in MySQL
This MySQL client error occurs when the MySQL client library cannot create a TCP/IP socket for connecting to the database server. The error indicates system-level socket creation failures, often due to resource exhaustion, permission issues, or network configuration problems. TCP/IP sockets are essential for remote database connections and certain local connections.
Quick fix
Fixes CR_IPSOCK_ERROR (2004): Can't create TCP/IP socket
# Check current file descriptor limits for the process
cat /proc/$(pgrep -f "your-app-process")/limits | grep "open files"
# Check system-wide limits
ulimit -n
ulimit -Sn # Soft limit
ulimit -Hn # Hard limit
# Check kernel limits
cat /proc/sys/fs/file-max
cat /proc/sys/fs/file-nr
# Increase limits temporarily
ulimit -n 65536
# Permanent increase in /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
# For systemd services, add to service file:
[Service]
LimitNOFILE=65536
# Reload systemd and restart service
sudo systemctl daemon-reload
sudo systemctl restart your-service- Check system file descriptor limits
- Verify network connectivity and firewall rules
- Check kernel network parameters
- Investigate security restrictions (SELinux/AppArmor)
# Check current file descriptor limits for the processcat /proc/$(pgrep -f "your-app-process")/limits | grep "open files"# Check system-wide limitsulimit -nulimit -Sn # Soft limitulimit -Hn # Hard limit# Check kernel limitscat /proc/sys/fs/file-maxcat /proc/sys/fs/file-nr# Increase limits temporarilyulimit -n 65536# Permanent increase in /etc/security/limits.conf* soft nofile 65536* hard nofile 65536# For systemd services, add to service file:[Service]LimitNOFILE=65536# Reload systemd and restart servicesudo systemctl daemon-reloadsudo systemctl restart your-serviceCR_IPSOCK_ERROR (2004): Can't create TCP/IP socket
What this error means
The CR_IPSOCK_ERROR (2004) is a MySQL client error that occurs when the client library (libmysqlclient) fails to create a TCP/IP socket for network communication with the MySQL server. Unlike UNIX domain sockets used for local connections, TCP/IP sockets are required for remote connections and some local configurations. Key aspects of this error: 1. **Socket Creation Failure**: The operating system is refusing to create a new socket, which is a fundamental network communication endpoint. 2. **System Resource Issues**: Socket creation can fail due to system resource limits, including file descriptor exhaustion, memory constraints, or kernel parameter limits. 3. **Network Stack Problems**: Issues with the network stack, firewall rules, or network interface configuration can prevent socket creation. 4. **Permission Denied**: The client process may lack necessary permissions to create sockets, especially when running with restricted privileges or in containerized environments. This error typically appears when establishing new connections, during connection pooling initialization, or when the MySQL server is configured to use TCP/IP instead of UNIX sockets for local connections.
Typical symptoms
- MySQL client fails with "CR_IPSOCK_ERROR (2004): Can't create TCP/IP socket"
- Application connection pooling fails during initialization
- Database connections fail when using TCP/IP (remote or localhost:3306)
- Error occurs specifically with hostnames or IP addresses, not local socket connections
- Connection attempts fail immediately without timeout delays
- Error appears after system updates, security hardening, or configuration changes
- Multiple applications or users experience the same error simultaneously
- Error occurs in containerized environments (Docker, Kubernetes)
Common causes
- System file descriptor limit reached (ulimit -n too low)
- Maximum number of open files exceeded for the process or system
- Network namespace or container isolation preventing socket creation
- Firewall or security software blocking socket creation at kernel level
- Insufficient memory or kernel resources for new sockets
- Permission denied due to SELinux/AppArmor policies or capability restrictions
- Network interface configuration issues or missing network stack
- Kernel parameter limits (net.core.somaxconn, net.ipv4.tcp_max_syn_backlog)
- Running out of ephemeral ports (net.ipv4.ip_local_port_range)
- Client running in chroot jail or restricted environment without network access
How to fix "CR_IPSOCK_ERROR (2004): Can't create TCP/IP socket"
Verify and increase file descriptor limits for the MySQL client process:
# Check current file descriptor limits for the process
cat /proc/$(pgrep -f "your-app-process")/limits | grep "open files"
# Check system-wide limits
ulimit -n
ulimit -Sn # Soft limit
ulimit -Hn # Hard limit
# Check kernel limits
cat /proc/sys/fs/file-max
cat /proc/sys/fs/file-nr
# Increase limits temporarily
ulimit -n 65536
# Permanent increase in /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
# For systemd services, add to service file:
[Service]
LimitNOFILE=65536
# Reload systemd and restart service
sudo systemctl daemon-reload
sudo systemctl restart your-serviceFor MySQL client processes, ensure they have sufficient file descriptors for both connections and internal socket usage.
Check basic network connectivity and ensure firewalls aren't blocking socket creation:
# Test basic network connectivity to MySQL server
ping mysql-server-hostname
nc -zv mysql-server-hostname 3306
# Check local network stack
ip addr show
ip route show
# Check firewall rules (iptables)
sudo iptables -L -n -v
sudo iptables -L -n -v | grep 3306
# Check nftables
sudo nft list ruleset
# Check if port 3306 is blocked
sudo ss -tlnp | grep 3306
# Temporarily disable firewall to test (not for production)
sudo systemctl stop firewalld
# or
sudo ufw disable
# For Docker/container environments, check network mode:
docker inspect container-name | grep -A 10 "NetworkSettings"
# Ensure container has network access
docker run --network host ... # For host network modeNote: Some security policies may block socket creation entirely, not just port access.
Verify and adjust kernel network parameters that affect socket creation:
# Check current kernel parameters
cat /proc/sys/net/core/somaxconn
cat /proc/sys/net/ipv4/tcp_max_syn_backlog
cat /proc/sys/net/core/netdev_max_backlog
# Check ephemeral port range
cat /proc/sys/net/ipv4/ip_local_port_range
# Increase socket-related parameters temporarily
sudo sysctl -w net.core.somaxconn=1024
sudo sysctl -w net.ipv4.tcp_max_syn_backlog=2048
sudo sysctl -w net.core.netdev_max_backlog=2000
# Increase ephemeral port range
sudo sysctl -w net.ipv4.ip_local_port_range="1024 65535"
# Make permanent changes in /etc/sysctl.conf
net.core.somaxconn = 1024
net.ipv4.tcp_max_syn_backlog = 2048
net.core.netdev_max_backlog = 2000
net.ipv4.ip_local_port_range = 1024 65535
# Apply changes
sudo sysctl -p
# Check total available sockets
cat /proc/sys/net/ipv4/tcp_mem
cat /proc/sys/net/ipv4/tcp_rmem
cat /proc/sys/net/ipv4/tcp_wmemThese parameters control socket queue sizes, connection backlogs, and memory allocation for sockets.
Check if security modules are blocking socket creation:
# Check SELinux status
getenforce
sestatus
# Check for SELinux denials related to MySQL or sockets
sudo ausearch -m avc -ts recent | grep -i socket
sudo ausearch -m avc -ts recent | grep -i mysql
# Check audit logs
sudo grep -i "socket" /var/log/audit/audit.log
sudo grep -i "mysql" /var/log/audit/audit.log
# Temporarily disable SELinux to test (not for production)
sudo setenforce 0
# Check AppArmor status (Ubuntu/Debian)
sudo aa-status
sudo aa-status | grep mysql
# Check AppArmor profiles
sudo apparmor_status
# Temporarily disable AppArmor for MySQL
sudo aa-complain /usr/sbin/mysqld
sudo aa-complain /usr/bin/mysql
# Check capabilities
getcap $(which mysql)
getcap $(which mysqld)
# Add network capability if needed (advanced)
sudo setcap 'cap_net_bind_service,cap_net_admin+ep' /usr/bin/mysqlSecurity modules may prevent processes from creating sockets, especially in containerized or restricted environments.
If TCP/IP socket creation continues to fail, switch to UNIX domain socket for local connections:
# MySQL command line - use UNIX socket instead of TCP/IP
mysql -u username -p # Uses default socket
# Specify socket explicitly
mysql --socket=/var/run/mysqld/mysqld.sock -u username -p
# In application configuration, use socket instead of host:portApplication configuration examples:
// PHP - Use socket instead of TCP/IP
// Instead of: mysql:host=127.0.0.1;port=3306
// Use:
$dsn = "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=mydatabase";# Python - Use socket
import mysql.connector
config = {
'unix_socket': '/var/run/mysqld/mysqld.sock',
'user': 'username',
'password': 'password',
'database': 'mydb'
}// Node.js - Use socket
const mysql = require('mysql2/promise');
const connection = await mysql.createConnection({
socketPath: '/var/run/mysqld/mysqld.sock',
user: 'root',
password: 'password',
database: 'mydb'
});Ensure MySQL server is configured to create and listen on the UNIX socket.
Investigate system-wide resource exhaustion that could prevent socket creation:
# Check system memory usage
free -h
# Check swap usage
swapon --show
# Check process limits
cat /proc/$(pidof mysqld)/limits
cat /proc/$(pidof your-app)/limits
# Check number of open files system-wide
cat /proc/sys/fs/file-nr
# Output format: allocated unused maximum
# Check for out-of-memory conditions
dmesg | grep -i "out of memory"
dmesg | grep -i "oom"
# Check kernel messages for socket errors
dmesg | grep -i "socket"
dmesg | grep -i "tcp"
# Check network connection count
ss -s
# Check for TIME_WAIT sockets (can exhaust ports)
ss -tan state time-wait | wc -l
# Reduce TIME_WAIT timeout (temporary fix)
sudo sysctl -w net.ipv4.tcp_fin_timeout=30
sudo sysctl -w net.ipv4.tcp_tw_reuse=1
sudo sysctl -w net.ipv4.tcp_tw_recycle=1 # Careful with NAT
# Monitor socket creation with strace
strace -e socket,connect your-application-command 2>&1 | grep -i "socket\|failed"System resource exhaustion often manifests as socket creation failures before other symptoms appear.
Advanced notes
The CR_IPSOCK_ERROR (2004) has several advanced considerations:
1. Container Networking: In Docker/Kubernetes, each container has its own network namespace. Ensure containers have appropriate network capabilities and aren't using overly restrictive network policies.
2. Network Namespace Isolation: Processes in separate network namespaces cannot create sockets in other namespaces. This is common in containerized, chrooted, or sandboxed environments.
3. Capability-Based Security: Modern Linux uses capabilities instead of full root privileges. The CAP_NET_RAW and CAP_NET_BIND_SERVICE capabilities may be required for socket operations.
4. Systemd Socket Activation: Some services use systemd socket activation where systemd creates the socket and passes it to the service. This can bypass socket creation permissions.
5. IPv6 vs IPv4: Socket creation failures may differ between IPv4 and IPv6. Test with both protocols if applicable.
6. Kernel Version Differences: Older kernels may have different default limits or socket behavior. Check kernel documentation for version-specific issues.
7. Virtualization Overhead: In virtualized environments (VMware, KVM, AWS), additional virtualization layers can affect socket creation and network performance.
For high-connection applications, consider connection pooling, persistent connections, or alternative connection strategies to reduce socket creation frequency.
Frequently asked questions
- What does "CR_IPSOCK_ERROR (2004): Can't create TCP/IP socket" mean?
- The CR_IPSOCK_ERROR (2004) is a MySQL client error that occurs when the client library (libmysqlclient) fails to create a TCP/IP socket for network communication with the MySQL server. Unlike UNIX domain sockets used for local connections, TCP/IP sockets are required for remote connections and some local configurations. Key aspects of this error: 1.
- How do I fix "CR_IPSOCK_ERROR (2004): Can't create TCP/IP socket"?
- This MySQL client error occurs when the MySQL client library cannot create a TCP/IP socket for connecting to the database server. The error indicates system-level socket creation failures, often due to resource exhaustion, permission issues, or network configuration problems. TCP/IP sockets are essential for remote database connections and certain local connections.
- What causes "CR_IPSOCK_ERROR (2004): Can't create TCP/IP socket"?
- System file descriptor limit reached (ulimit -n too low) Maximum number of open files exceeded for the process or system Network namespace or container isolation preventing socket creation
- Is the "CR_IPSOCK_ERROR (2004): Can't create TCP/IP socket" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐MySQL Official Documentation
- ๐dev.mysql.com - client error reference(Official Docs)
- ๐dev.mysql.com - can not connect to server(Official Docs)
- ๐dev.mysql.com - mysql error(Official Docs)
- ๐ฌstackoverflow.com - mysql+socket(Stack Overflow)
- ๐github.com - mysqld error.h(GitHub)