Unix sockets and TCP/IP sockets serve similar purposes but operate at different layers with distinct characteristics:
// TCP/IP socket connection example in Python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # IPv4 TCP socket
s.connect(('127.0.0.1', 3306))
# Unix domain socket example
us = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
us.connect('/var/run/mysqld/mysqld.sock')
Unix sockets typically offer better performance for local communication:
- No network protocol overhead (TCP handshake, congestion control, etc.)
- Bypasses the entire network stack
- Lower latency (typically 30-50% faster for local connections)
When connecting to MySQL with "localhost" as the host:
// PHP PDO using Unix socket (implicit)
$dbh = new PDO('mysql:dbname=testdb;host=localhost', $user, $pass);
// Explicit TCP/IP connection
$dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1;port=3306', $user, $pass);
Use Unix sockets when:
- Client and server are on the same machine
- Maximum performance is needed
- No network security concerns (since it's local)
Use TCP/IP sockets when:
- Communicating across machines
- Need standard port-based access control
- Require network-level encryption (SSL/TLS)
Sample benchmark results for 10,000 simple MySQL queries:
TCP/IP localhost: 1.82 seconds Unix socket: 1.12 seconds Network TCP (remote): 3.45 seconds
For MySQL specifically, the socket path can be configured in my.cnf:
[mysqld]
socket=/var/run/mysqld/mysqld.sock
[client]
socket=/var/run/mysqld/mysqld.sock
Remember that some MySQL client libraries may behave differently across programming languages when handling "localhost" versus "127.0.0.1".
When working with database connections in Unix-like systems, you'll encounter two primary socket types:
// TCP/IP socket connection example (MySQL)
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=test';
$user = 'username';
$password = 'password';
// Unix domain socket connection example (MySQL)
$dsn = 'mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test';
Unix domain sockets (UDS) typically offer better performance for local communication:
- No network protocol overhead
- No TCP handshakes or connection teardowns
- Kernel handles data copying between processes directly
Unix sockets provide filesystem-based permissions:
$ ls -l /var/run/mysqld/mysqld.sock
srwxrwxrwx 1 mysql mysql 0 Aug 10 10:00 /var/run/mysqld/mysqld.sock
While TCP/IP connections require additional security measures like firewalls and encrypted tunnels.
Use Unix sockets when:
- Client and server are on the same machine
- You need maximum performance
- Filesystem permissions provide sufficient security
Use TCP/IP sockets when:
- Communicating across machines
- Need network-level features (load balancing, failover)
- Working with containers where socket files might not persist
For PostgreSQL:
# TCP/IP connection
host all all 127.0.0.1/32 md5
# Unix socket connection
local all all peer
For Redis:
# TCP configuration
bind 127.0.0.1
port 6379
# Unix socket configuration
unixsocket /tmp/redis.sock
unixsocketperm 770
Common issues with Unix sockets:
# Check if socket exists
test -S /path/to/socket.sock || echo "Socket missing"
# Verify permissions
stat -c "%A %U %G" /path/to/socket.sock
For TCP connections, standard network tools apply:
telnet 127.0.0.1 3306
netstat -tulnp | grep mysql