Keywords: MySQL | UNIX Socket | mysqld_safe | Directory Permissions | Troubleshooting
Abstract: This technical paper provides an in-depth analysis of the "Directory '/var/run/mysqld' for UNIX socket file don't exists" error during MySQL server startup. It explores the fundamental role of UNIX sockets in MySQL communication architecture, presents detailed manual directory creation solutions, and ensures stable MySQL operation through proper permission configuration and system service management. The article combines specific error logs and practical examples to help readers thoroughly understand the problem root causes and master effective troubleshooting methodologies.
Problem Background and Error Analysis
In the MySQL database management system, UNIX Domain Sockets serve as a crucial mechanism for local inter-process communication. When starting the MySQL server using mysqld_safe, the system requires a designated directory to create socket files for establishing connection channels between clients and the server. From the provided error log:
2017-02-10T17:05:44.870970Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-02-10T17:05:44.872874Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-02-10T17:05:44.874547Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
The error clearly indicates the system's inability to locate the /var/run/mysqld directory, which is MySQL's default location for storing socket files. This situation typically occurs in the following scenarios:
- Directory creation failure during MySQL installation
- Accidental deletion of the directory during system temporary file cleanup
- Manual modification of socket path in MySQL configuration files without creating corresponding directories
- Temporary filesystem clearance after system reboot
Core Role of UNIX Sockets in MySQL Architecture
UNIX Domain Sockets provide efficient data transmission mechanisms for local inter-process communication. Within MySQL architecture, socket files play a pivotal role:
// Example: Basic workflow of MySQL client connecting through sockets
#include <sys/socket.h>
#include <sys/un.h>
int connect_to_mysql_socket(const char *socket_path) {
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket creation failed");
return -1;
}
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);
if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("connect failed");
close(sockfd);
return -1;
}
return sockfd;
}
When the socket directory is missing, the MySQL server cannot create the mysqld.sock file, resulting in failure of all local connection requests. This is validated in the reference article: users attempting to reset MySQL root passwords encountered identical errors, unable to establish connections through sockets.
Solution Implementation Steps
For directory absence issues, the most direct solution involves manual directory creation with proper permission configuration. Below is the detailed operational procedure:
# Step 1: Create socket directory
sudo mkdir -p /var/run/mysqld
# Step 2: Set directory ownership to MySQL user and group
sudo chown mysql:mysql /var/run/mysqld
# Step 3: Verify directory permissions
ls -ld /var/run/mysqld
# Expected output: drwxr-xr-x 2 mysql mysql 4096 Feb 10 17:10 /var/run/mysqld
# Step 4: Restart MySQL service
sudo systemctl restart mysql
# Or using traditional service management
sudo service mysql restart
The -p parameter in the mkdir -p command ensures creation of parent directories if nonexistent, preventing issues with missing multi-level directory structures. chown mysql:mysql transfers directory ownership to the user and group required for MySQL service operation, which is crucial for ensuring MySQL processes can create and access socket files within this directory.
In-Depth Understanding of Permission Configuration
The correctness of permission configuration directly impacts MySQL service normal operation. Let's understand the permission verification mechanism through code examples:
// Simulating MySQL service directory permission checking process
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
int verify_mysql_directory(const char *path) {
struct stat st;
if (stat(path, &st) != 0) {
return -1; // Directory doesn't exist
}
if (!S_ISDIR(st.st_mode)) {
return -2; // Not a directory
}
// Get MySQL user information
struct passwd *pwd = getpwnam("mysql");
if (pwd == NULL) {
return -3; // MySQL user doesn't exist
}
// Verify directory ownership
if (st.st_uid != pwd->pw_uid) {
return -4; // User ownership mismatch
}
// Verify write permissions
if (access(path, W_OK) != 0) {
return -5; // No write permission
}
return 0; // Verification passed
}
This verification process explains why simple directory creation is insufficient to resolve the issue and must be accompanied by correct ownership configuration. In the reference article scenario, users attempted multiple password reset methods but failed due to fundamental filesystem problems, highlighting the importance of proper socket directory configuration.
System Service Integration and Persistence
In most Linux distributions, the /var/run directory belongs to the temporary filesystem (tmpfs), with contents lost after system reboot. To ensure MySQL service continues functioning properly after system restarts, system services must be configured to automatically create required directories during startup:
# Create system service startup script fragment
# /etc/systemd/system/mysql.service.d/create-socket-dir.conf
[Service]
ExecStartPre=/bin/mkdir -p /var/run/mysqld
ExecStartPre=/bin/chown mysql:mysql /var/run/mysqld
ExecStartPre=/bin/chmod 755 /var/run/mysqld
For systems using SysV init, directory creation commands can be added to appropriate locations in the /etc/init.d/mysql startup script:
# Add at the beginning of start() function
start() {
# Ensure socket directory exists
if [ ! -d "/var/run/mysqld" ]; then
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
chmod 755 /var/run/mysqld
fi
# Original startup logic...
}
Troubleshooting and Verification
After implementing solutions, comprehensive verification is necessary to ensure complete problem resolution:
# Verification 1: Check directory status
ls -la /var/run/mysqld/
# Should display empty directory or contain mysqld.sock file
# Verification 2: Check MySQL service status
sudo systemctl status mysql
# Or
sudo service mysql status
# Verification 3: Test local connection
mysql -u root -p
# If password configured, should connect successfully after entering password
# Verification 4: Check error logs
tail -f /var/log/mysql/error.log
# Confirm no new socket-related errors appear
If problems persist, examination of socket path configuration in MySQL configuration files /etc/mysql/my.cnf or /etc/my.cnf may be necessary:
# Check socket configuration
sudo grep "socket" /etc/mysql/my.cnf
# Common configuration line: [mysqld] socket = /var/run/mysqld/mysqld.sock
Preventive Measures and Best Practices
To prevent recurrence of similar issues, the following preventive measures are recommended:
- Include directory creation and permission configuration steps in system deployment scripts
- Regularly monitor MySQL error logs for early detection of potential issues
- Utilize configuration management tools (e.g., Ansible, Puppet) for automated MySQL environment configuration
- Ensure proper persistent volume mounting for socket directories in containerized deployments
By understanding the core role of UNIX sockets in MySQL architecture and mastering proper directory management and permission configuration methods, database administrators can effectively prevent and resolve such startup failures, ensuring stable and reliable MySQL service operation.