MySQL Socket File Missing After EC2 Instance Type Change: mysqld.sock Not Found in /var/run/mysqld


3 views

When you change an EC2 instance type in a VPC environment, several system-level configurations might reset. One particularly frustrating issue is the disappearance of the MySQL socket file (mysqld.sock). Here's what's happening under the hood:


$ ls -la /var/run/mysqld/
ls: cannot access /var/run/mysqld/: No such file or directory

Ubuntu 10.04 uses Upstart for service management. When you stop/start an EC2 instance (especially when changing instance types), the /var/run directory gets recreated. MySQL's init script might fail to recreate the socket directory due to:

  • Missing directory permissions
  • Changed tmpfs mount points
  • AppArmor/SELinux restrictions

First, verify MySQL is actually running:


$ sudo service mysql status
mysql start/running, process 1234

Then manually create the directory structure:


$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld

Edit your MySQL configuration (typically /etc/mysql/my.cnf):


[mysqld]
socket = /var/run/mysqld/mysqld.sock

[client]
socket = /var/run/mysqld/mysqld.sock

Then restart MySQL:


$ sudo service mysql restart

If you're having persistent issues, you can force TCP connections instead:


$ mysql --protocol=TCP -u root -p

Or update your applications to use:


jdbc:mysql://localhost:3306/database?useSSL=false

For Ubuntu 10.04 specifically, modify the Upstart script (/etc/init/mysql.conf) to include:


pre-start script
    mkdir -p /var/run/mysqld
    chown mysql:mysql /var/run/mysqld
end script

Remember that Ubuntu 10.04 is quite old - consider upgrading to a supported LTS release where systemd handles these runtime directories more reliably.


When you changed your EC2 instance type and restarted MySQL on Ubuntu 10.04, you encountered the classic "mysqld.sock doesn't exist" error. This happens because Ubuntu's Upstart or init scripts might not have properly recreated the socket file during the instance type transition.

First, let's verify where MySQL is trying to create the socket. Check your MySQL configuration:

grep "socket" /etc/mysql/my.cnf
# or for newer MySQL versions
grep "socket" /etc/mysql/mysql.conf.d/mysqld.cnf

Typically, you'll see something like:

socket = /var/run/mysqld/mysqld.sock

The /var/run directory is often tmpfs (in-memory filesystem) and gets cleared on reboot. Here's how to recreate it:

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

Now restart MySQL to recreate the socket file:

sudo service mysql restart

If this doesn't work, try the nuclear option:

sudo stop mysql
sudo start mysql

If you're still having issues, consider these approaches:

# 1. Check if MySQL is actually running
ps aux | grep mysqld

# 2. Try starting MySQL manually with socket specification
sudo mysqld_safe --socket=/var/run/mysqld/mysqld.sock &

# 3. Temporarily change socket location in my.cnf
#    (useful for testing)
socket = /tmp/mysqld.sock

For a more permanent solution on EC2:

  • Create an Upstart or systemd script that ensures the directory exists before MySQL starts
  • Consider mounting /var/run/mysqld as a persistent volume if you frequently change instance types
  • Upgrade from Ubuntu 10.04 (which reached EOL in 2015) to a supported version

If applications can't connect via socket, temporarily use TCP/IP:

mysql -h 127.0.0.1 -u root -p

In your application configuration, replace "localhost" with "127.0.0.1" to force TCP/IP connection.