The fundamental distinction lies in the communication mechanism. MySQL sockets (Unix domain sockets) use filesystem communication, while TCP/IP connections (host:port) utilize network protocols. This creates several technical implications:
# Socket connection example (PHP)
$link = mysqli_connect('localhost:/var/run/mysqld/mysqld.sock', 'user', 'password');
# TCP/IP connection example
$link = mysqli_connect('127.0.0.1:3306', 'user', 'password');
Unix domain sockets generally show 10-20% better throughput for local connections. Here's why:
- No TCP overhead (handshakes, congestion control)
- No network stack processing
- Direct kernel-to-kernel communication
Consider sockets mandatory when:
- MySQL is configured with
skip-networking
- Running in containerized environments with shared volumes
- High-security environments restricting network access
Host:port connections become necessary when:
# Remote connection must use TCP
$link = mysqli_connect('db.example.com:3306', 'user', 'password');
Additional benefits include:
- Standardized port configuration (3306)
- Easier container linking
- Compatible with all MySQL clients
MySQL's my.cnf
settings for socket optimization:
[mysqld]
socket = /var/run/mysqld/mysqld.sock
skip-networking # Optional for security
Connection pooling comparison:
# Socket-based pool (Python)
pool = QueuePool(
lambda: create_engine('mysql+pymysql://user:pass@/dbname?unix_socket=/tmp/mysql.sock')
)
# TCP pool
pool = QueuePool(
lambda: create_engine('mysql+pymysql://user:pass@localhost:3306/dbname')
)
Common socket issues and fixes:
# Check socket permissions
ls -la /var/run/mysqld/mysqld.sock
# Verify socket location
mysqladmin variables | grep socket
# Alternative connection test
mysql --socket=/path/to/mysql.sock -u user -p
Remember that some cloud providers modify default socket behaviors, so always check your specific environment.
When connecting to MySQL, you essentially have two pathways:
- Unix domain socket (e.g.,
/var/run/mysqld/mysqld.sock
) - TCP/IP connection (e.g.,
localhost:3306
or127.0.0.1:3306
)
Unix sockets are inter-process communication (IPC) mechanisms that only work when:
# Typical socket connection in PHP
$db = new mysqli('localhost', 'user', 'password', 'database', null, '/var/run/mysqld/mysqld.sock');
Key advantages:
- No network stack overhead - Data never leaves the machine
- Filesystem permissions - Additional security layer via socket file permissions
- No port conflicts - Doesn't consume a TCP port
# Standard TCP connection in Python
import mysql.connector
db = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="user",
password="password",
database="db"
)
When you must use TCP:
- Remote database servers
- Containerized environments (Docker-to-Docker communication)
- When your app and DB are on separate machines
For local connections, benchmarks typically show:
Metric | Unix Socket | TCP Localhost |
---|---|---|
Latency | 15-20% lower | Higher |
Throughput | 10-15% better | Lower |
Sockets provide:
- Filesystem-based access control
- No exposure to network sniffing
- Cannot be reached from external machines
TCP connections require:
- Proper firewall rules
- Secure authentication
- Potential SSL/TLS overhead
MySQL my.cnf settings:
[mysqld]
socket=/var/run/mysqld/mysqld.sock
port=3306
Connecting via socket in different languages:
// Node.js
const mysql = require('mysql');
const connection = mysql.createConnection({
socketPath: '/var/run/mysqld/mysqld.sock',
user: 'db_user',
password: 'db_password',
database: 'db_name'
});
Use sockets when:
- Application and DB are on same physical host
- Maximum local performance is critical
- Network security is a concern
Use TCP when:
- Connecting to remote databases
- In containerized environments (even if on same host)
- When socket file location is non-standard or inaccessible
Common socket errors:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
Solutions:
- Verify socket path in my.cnf
- Check file permissions (
ls -l /var/run/mysqld/
) - Confirm mysqld is running
TCP connection issues:
ERROR 2003 (HY000): Can't connect to MySQL server on 'host' (111)
Solutions:
- Check if port 3306 is open (
telnet host 3306
) - Verify MySQL is listening (
netstat -tulpn | grep 3306
) - Check firewall rules