1. 程式人生 > 其它 >MySQL中的pid與socket是什麼?

MySQL中的pid與socket是什麼?

前言:

不知道你有沒有注意過,MySQL 啟動時需要配置 pid 及 socket 檔案路徑。偶爾還會出現因 pid 檔案找不到而啟動失敗的現象,那麼 pid 與 socket 檔案究竟是幹什麼用的呢?我們一起來看下本篇文章。

1.pid-file介紹

MySQL 中的 pid 檔案記錄的是當前 mysqld 程序的 pid ,pid 亦即 Process ID 。可以通過 pid-file 引數來配置 pid 檔案路徑及檔名,如果未指定此變數,則 pid 檔案預設名為 host_name.pid ,存放的路徑預設放在 MySQL 的資料目錄。

建議指定 pid 檔名及路徑,pid 目錄許可權要對 mysql 系統使用者放開,具體配置可參考如下:

# my.cnf 配置檔案
[mysqld]
pid-file  = /data/mysql/tmp/mysqld.pid

# 檢視mysqld程序
[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

# 檢視pid檔案內容 
[root@localhost ~]# cat /data/mysql/tmp/mysqld.pid
9353

可以看到 pid 檔案內容只有一行,記錄了 mysqld 程序的 ID 。mysqld 程序啟動後會通過 create_pid_file 函式新建 pid 檔案,通過 getpid() 獲取當前程序號並將程序 ID 寫入 pid 檔案。 程序執行後會給 pid 檔案加一個檔案鎖,只有獲得 pid 檔案寫入許可權的程序才能正常啟動並把自身的 PID 寫入該檔案中,其它同一個程式的多餘程序則自動退出。因此 pid 檔案的作用是防止啟動多個程序副本。

有時候可能會遇到因 pid 檔案問題而啟動失敗的情況,這幾類報錯你可能遇到過:

  • 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

上面幾類 pid 相關報錯解決方法其實都是類似的,首先要看下 error log 找到具體報錯,然後檢視配置檔案,確保 pid 檔案目錄路徑正確且有許可權有空間,之後可以看下 mysqld 程序是否存在,若存在可手動 kill 掉,若有殘留的 pid 檔案也可以先刪掉,一切排查就緒後,再次重新啟動,一般即可成功。

2.socket檔案介紹

socket 即 Unix 套接字檔案,在類 unix 平臺,客戶端連線 MySQL 服務端的方式有兩種,分別是 TCP/IP 方式與 socket 套接字檔案方式。 Unix 套接字檔案連線的速度比 TCP/IP 快,但是隻能連線到同一臺計算機上的伺服器使用。

通過設定 socket 變數可配置套接字檔案路徑及名稱,預設值為 /tmp/mysql.sock (對於某些發行格式,目錄可能有所不同)。參考配置如下:

# my.cnf 配置檔案
[mysqld]
socket = /data/mysql/tmp/mysql.sock
[client]
socket = /data/mysql/tmp/mysql.sock

# 檢視對應目錄下的socket檔案
root@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

# 通過 -S 命令指定socket登入
[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.0.22 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.0.22 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.0.22 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

檢視上述連線狀態可知,MySQL 在本地可以通過 socket 方式連線。在本地登入時,如果 my.cnf 配置檔案中的 [client] 部分沒有指定 socket 檔案路徑,mysql 預設會去尋找 /tmp/mysql.sock ,所以如果 mysqld 服務啟動的時候,生成的 socket 檔案不是預設路徑的話,登陸可能會報錯(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock')。其實 [mysqld] 部分及 [client] 部分都配置具體路徑可避免此問題,也可以在 tmp 路徑下建立軟連線,如:ln -s /data/mysql/tmp/mysql.sock /tmp/mysql.sock 。同樣的,socket 檔案目錄許可權要對 mysql 系統使用者放開。

總結:

本篇文章介紹了 MySQL 中的 pid 及 socket 檔案的具體配置及作用。其實這兩個引數還是比較好維護的,一開始配置好不要去動它就好了,若遇到重啟報錯的情況,根據錯誤日誌慢慢來排查,細心的操作,總會找到問題的。