CentOS 7.8 MySQL failed to start /usr/sbin/mysqld: Can't create/write to file'/var/run/mysqld/mysqld.pid'

1. Problem description:

MySQL failed to start after restarting the CentOS 7.8 machine
[ERROR] /usr/sbin/mysqld: Can't create/write to file'/var/run/mysqld/mysqld.pid' (Errcode: 2-No such file or directory)

Note : This scenario uses the MySQL RPM installation package, the version number is MySQL 5.7.12. The latest patch version of MySQL 5.7 is 5.7.31. It is recommended to download mysql-5.7.31-1.el7.x86_64.rpm-bundle directly. tar.

Second, cause analysis:

linux catalog detailed introduction

The /var/run directory points to the /run directory, and /run is a temporary file system that stores information since the system was started. When the system restarts, the files in this directory should be deleted or cleared.
It is conceivable that after CentOS restarts, the /var/run/mysqld directory will disappear, and the directory is not automatically created in the startup script of mysql5.7, so the error will be prompted!

3. Temporary solution:

cd /var/run
mkdir mysqld
chown mysql:mysql mysqld
systemctl start mysqld

4. Permanent solution:

  • Method 1: Modify the startup script. If the /var/run/mysqld directory cannot be found during startup, it will be created automatically. (recommend)
vim /etc/init.d/mysqld
# 以下截取修改部分内容
mypiddir="/var/run/mysqld"
get_mysql_option mysqld_safe pid-file "$mypiddir/mysqld.pid"
mypidfile="$result"

#############################省略#############################

start(){
    
    
    [ -x $exec ] || exit 5
    # check to see if it's already running
    RESPONSE=$(/usr/bin/mysqladmin --no-defaults --socket="$adminsocket" --user=UNKNOWN_MYSQL_USER ping 2>&1)
    if [ $? = 0 ]; then
        # already running, do nothing
        action $"Starting $prog: " /bin/true
        ret=0
    elif echo "$RESPONSE" | grep -q "Access denied for user"
    then
        # already running, do nothing
        action $"Starting $prog: " /bin/true
        ret=0
    else
    	# 不存在/var/run/mysqld目录,则自动创建
        if [ ! -e "$mypiddir" -a ! -h "$mypiddir" ]
        then
           mkdir -p "$mypiddir" || exit 1
        fi
        chown mysql:mysql "$mypiddir"

        # prepare for start
        touch "$errlogfile"
        chown mysql:mysql "$errlogfile"

#############################省略#############################

systemctl daemon-reload
systemctl start mysqld
  • Method 2: Modify the /etc/my.cnf configuration file, modify the location of the pid-file file under the mysqld_safe attribute group
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
skip-name-resolve

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

sql-mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

[mysqld_safe]
log-error=/var/log/mysqld.log
# 可改为/var/run/mysqld.pid
pid-file=/var/run/mysqld/mysqld.pid

systemctl start mysqld

Note : During the test, it was found that modifying the location of the pid-file can indeed start successfully, and the service can be used normally, but the mysqld_safe daemon cannot obtain the running status of mysql, and the startup script has been in a waiting state (the reason is that the running status of the mysqld_safe daemon is monitored The object is still the original /var/run/mysqld/mysqld.pid)!
It is speculated that the MySQL 5.7.12 running status monitoring script may have bugs. This method is not recommended for the time being.

If you have a better solution, you can also leave a message to explain, thank you very much!

Guess you like

Origin blog.csdn.net/ory001/article/details/109991632