MySQL 中的 pid 文件記錄的是當(dāng)前 mysqld 進(jìn)程的 pid ,pid 亦即 Process ID ??梢酝ㄟ^ pid-file 參數(shù)來配置 pid 文件路徑及文件名,如果未指定此變量,則 pid 文件默認(rèn)名為 host_name.pid ,存放的路徑默認(rèn)放在 MySQL 的數(shù)據(jù)目錄。
建議指定 pid 文件名及路徑,pid 目錄權(quán)限要對(duì) mysql 系統(tǒng)用戶放開,具體配置可參考如下:
# my.cnf 配置文件 [mysqld] pid-file = /data/mysql/tmp/mysqld.pid # 查看mysqld進(jìn)程 [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文件內(nèi)容 [root@localhost ~]# cat /data/mysql/tmp/mysqld.pid 9353
可以看到 pid 文件內(nèi)容只有一行,記錄了 mysqld 進(jìn)程的 ID 。mysqld 進(jìn)程啟動(dòng)后會(huì)通過 create_pid_file 函數(shù)新建 pid 文件,通過 getpid() 獲取當(dāng)前進(jìn)程號(hào)并將進(jìn)程 ID 寫入 pid 文件。進(jìn)程運(yùn)行后會(huì)給 pid 文件加一個(gè)文件鎖,只有獲得 pid 文件寫入權(quán)限的進(jìn)程才能正常啟動(dòng)并把自身的 PID 寫入該文件中,其它同一個(gè)程序的多余進(jìn)程則自動(dòng)退出。因此 pid 文件的作用是防止啟動(dòng)多個(gè)進(jìn)程副本。
有時(shí)候可能會(huì)遇到因 pid 文件問題而啟動(dòng)失敗的情況,這幾類報(bào)錯(cuò)你可能遇到過:
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 相關(guān)報(bào)錯(cuò)解決方法其實(shí)都是類似的,首先要看下 error log 找到具體報(bào)錯(cuò),然后查看配置文件,確保 pid 文件目錄路徑正確且有權(quán)限有空間,之后可以看下 mysqld 進(jìn)程是否存在,若存在可手動(dòng) kill 掉,若有殘留的 pid 文件也可以先刪掉,一切排查就緒后,再次重新啟動(dòng),一般即可成功。
socket 即 Unix 套接字文件,在類 unix 平臺(tái),客戶端連接 MySQL 服務(wù)端的方式有兩種,分別是 TCP/IP 方式與 socket 套接字文件方式。Unix 套接字文件連接的速度比 TCP/IP 快,但是只能連接到同一臺(tái)計(jì)算機(jī)上的服務(wù)器使用。
通過設(shè)置 socket 變量可配置套接字文件路徑及名稱,默認(rèn)值為 /tmp/mysql.sock (對(duì)于某些發(fā)行格式,目錄可能有所不同)。參考配置如下:
# my.cnf 配置文件 [mysqld] socket = /data/mysql/tmp/mysql.sock [client] socket = /data/mysql/tmp/mysql.sock # 查看對(duì)應(yīng)目錄下的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
查看上述連接狀態(tài)可知,MySQL 在本地可以通過 socket 方式連接。在本地登錄時(shí),如果 my.cnf 配置文件中的 [client] 部分沒有指定 socket 文件路徑,mysql 默認(rèn)會(huì)去尋找 /tmp/mysql.sock ,所以如果 mysqld 服務(wù)啟動(dòng)的時(shí)候,生成的 socket 文件不是默認(rèn)路徑的話,登陸可能會(huì)報(bào)錯(cuò)(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock')。其實(shí) [mysqld] 部分及 [client] 部分都配置具體路徑可避免此問題,也可以在 tmp 路徑下建立軟連接,如:ln -s /data/mysql/tmp/mysql.sock /tmp/mysql.sock 。同樣的,socket 文件目錄權(quán)限要對(duì) mysql 系統(tǒng)用戶放開。
本篇文章介紹了 MySQL 中的 pid 及 socket 文件的具體配置及作用。其實(shí)這兩個(gè)參數(shù)還是比較好維護(hù)的,一開始配置好不要去動(dòng)它就好了,若遇到重啟報(bào)錯(cuò)的情況,根據(jù)錯(cuò)誤日志慢慢來排查,細(xì)心的操作,總會(huì)找到問題的。
以上就是詳解MySQL中的pid與socket的詳細(xì)內(nèi)容,更多關(guān)于MySQL pid與socket的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:徐州 無錫 南充 麗水 迪慶 龍巖 西寧 自貢
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解MySQL中的pid與socket》,本文關(guān)鍵詞 詳解,MySQL,中的,pid,與,socket,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。