Preface:

If you haven’t noticed, you need to configure pid and socket file path when MySQL starts. Occasionally also appear because pid file can not find the phenomenon of startup failure, so PID and socket file exactly what is used? Let’s take a look at the next article.

1. The pid – file is introduced

The PID file in MySQL records the PID of the current mysqld Process. Pid is also the Process ID. The pid file path and file name can be configured by using the pid-file parameter. If this parameter is not specified, the pid file name is host_name. Pid by default, the pid file is stored in the MySQL data directory by default.

You are advised to specify the PID file name and path. The PID directory permission is restricted to mysql system users. For details, see the following:

CNF configuration file [mysqld] pid-file  = /data/mysql/tmp/Pid # check mysqld process [root]@localhost ~]# ps -ef|grep mysqld
root       8670      1  0 Jun09 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/tmp/mysqld.pid
mysql      9353   8670  0 Jun09 ?        00:01:23 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/logs/error.log --pid-file=/data/mysql/tmp/mysqld.pid --socket=/data/mysql/tmp/mysql.sock# check pid file contents [root@localhost ~]# cat /data/mysql/tmp/mysqld.pid
9353
Copy the code

You can see that the PID file contains only one line, which records the ID of the mysqld process. When mysqld starts, create_pid_file is used to create a PID file. Getpid () is used to get the current process number and write the process ID to the PID file. After the process runs, a file lock will be added to the PID file. Only the process with pid file writing permission can start normally and write its OWN PID into the file, and other redundant processes of the same program will exit automatically. The pid file is therefore used to prevent multiple process copies from being started.

Pid file error: Pid file error: Pid file error: PID file error

  • Can‘t start server: can‘t create PID file: No such file or directory
  • ERROR! MySQL server PID file could not be found
  • ERROR! The server quit without updating PID file

First, check the error log to find the specific error, and then check the configuration file to ensure that the PID file directory path is correct and has permission and space, and then check whether the mysqld process exists. If so, you can manually kill it. If there are residual PID files can also be deleted, all the troubleshooting is ready, restart again, generally successful.

2. Introduction to socket files

Socket is a Unix socket file. On a UNIx-like platform, a client can connect to the MySQL server in two ways: TCP/IP and socket socket file. Unix socket file connections are faster than TCP/IP, but can only be used to connect to servers on the same computer.

The socket variable is used to configure the socket file path and name. The default value is/TMP /mysql.sock (directories may vary for some distribution formats). The reference configuration is as follows:

# my.cnf config file [mysqld] socket= /data/mysql/tmp/mysql.sock
[client]
socket = /data/mysql/tmp/Mysql. Sock # check the socket file root in the corresponding directory@localhost tmp]# ls -lh
total 8.0K
srwxrwxrwx 1 mysql mysql 0 Jun 10 15:19 mysql.sock
-rw------- 1 mysql mysql 6 Jun 10 15:19 mysql.sock.lock# by-The S command specifies the socket for login to [root]@localhost ~]# mysql -uroot -pxxxx -S /data/mysql/tmp/mysql.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.022. MySQL Community Server - GPL

Copyright (c) 2000.2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help; ' or '\h' for help. Type '\c' to clear the current input statement.

mysql> status
--------------
mysql  Ver 8.022. for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          12
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ' '
Using delimiter:        ;
Server version:         8.022. MySQL Community Server - GPL
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
UNIX socket:            /data/mysql/tmp/mysql.sock
Binary data as:         Hexadecimal
Uptime:                 1 hour 27 min 31 sec

Threads: 3  Questions: 27  Slow queries: 0  Opens: 135  Flush tables: 3  Open tables: 56  Queries per second avg: 0.005
Copy the code

The connection status shows that MySQL can be connected to the local server through socket. / TMP /mysql.sock will be used by mysql by default to search for/TMP /mysql.sock if the [client] part of the my.cnf configuration file is not specified. ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/ TMP /mysql.sock’) Actually [mysqld] and [client] part configuration specific path can avoid this problem, can also be soft in the TMP path connection, such as: ln -s/data/mysql/TMP/mysql. The sock/TMP/mysql. The sock. Also, the socket file directory permission should be granted to mysql system users.

Conclusion:

This article introduces the specific configuration and function of PID and socket file in MySQL. In fact, these two parameters are relatively easy to maintain, at the beginning of the configuration do not move it, if you encounter a restart error, according to the error log slowly to troubleshoot, careful operation, will always find the problem.