詳解MySQL中的pid與socket
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)限要對 mysql 系統(tǒng)用戶放開,具體配置可參考如下:
# my.cnf 配置文件[mysqld]pid-file = /data/mysql/tmp/mysqld.pid# 查看mysqld進(jìn)程[root@localhost ~]# ps -ef|grep mysqldroot 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.pidmysql 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.pid9353
可以看到 pid 文件內(nèi)容只有一行,記錄了 mysqld 進(jìn)程的 ID 。mysqld 進(jìn)程啟動后會通過 create_pid_file 函數(shù)新建 pid 文件,通過 getpid() 獲取當(dāng)前進(jìn)程號并將進(jìn)程 ID 寫入 pid 文件。進(jìn)程運(yùn)行后會給 pid 文件加一個(gè)文件鎖,只有獲得 pid 文件寫入權(quán)限的進(jìn)程才能正常啟動并把自身的 PID 寫入該文件中,其它同一個(gè)程序的多余進(jìn)程則自動退出。因此 pid 文件的作用是防止啟動多個(gè)進(jìn)程副本。
有時(shí)候可能會遇到因 pid 文件問題而啟動失敗的情況,這幾類報(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)程是否存在,若存在可手動 kill 掉,若有殘留的 pid 文件也可以先刪掉,一切排查就緒后,再次重新啟動,一般即可成功。
2.socket文件介紹socket 即 Unix 套接字文件,在類 unix 平臺,客戶端連接 MySQL 服務(wù)端的方式有兩種,分別是 TCP/IP 方式與 socket 套接字文件方式。Unix 套接字文件連接的速度比 TCP/IP 快,但是只能連接到同一臺計(jì)算機(jī)上的服務(wù)器使用。
通過設(shè)置 socket 變量可配置套接字文件路徑及名稱,默認(rèn)值為 /tmp/mysql.sock (對于某些發(fā)行格式,目錄可能有所不同)。參考配置如下:
# my.cnf 配置文件[mysqld]socket = /data/mysql/tmp/mysql.sock[client]socket = /data/mysql/tmp/mysql.sock# 查看對應(yīng)目錄下的socket文件root@localhost tmp]# ls -lhtotal 8.0Ksrwxrwxrwx 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.sockmysql: [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 12Server version: 8.0.22 MySQL Community Server - GPLCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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: 12Current database:Current user: root@localhostSSL: Not in useCurrent pager: stdoutUsing outfile: ’’Using delimiter:;Server version: 8.0.22 MySQL Community Server - GPLProtocol version: 10Connection: Localhost via UNIX socketServer characterset: utf8mb4Db characterset: utf8mb4Client characterset: utf8mb4Conn. characterset: utf8mb4UNIX socket: /data/mysql/tmp/mysql.sockBinary data as: HexadecimalUptime: 1 hour 27 min 31 secThreads: 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)會去尋找 /tmp/mysql.sock ,所以如果 mysqld 服務(wù)啟動的時(shí)候,生成的 socket 文件不是默認(rèn)路徑的話,登陸可能會報(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)限要對 mysql 系統(tǒng)用戶放開。
總結(jié):本篇文章介紹了 MySQL 中的 pid 及 socket 文件的具體配置及作用。其實(shí)這兩個(gè)參數(shù)還是比較好維護(hù)的,一開始配置好不要去動它就好了,若遇到重啟報(bào)錯(cuò)的情況,根據(jù)錯(cuò)誤日志慢慢來排查,細(xì)心的操作,總會找到問題的。
以上就是詳解MySQL中的pid與socket的詳細(xì)內(nèi)容,更多關(guān)于MySQL pid與socket的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. MySQL表的CURD操作(數(shù)據(jù)的增刪改查)2. MySQL 鎖的相關(guān)知識總結(jié)3. 解決Mybatis中mapper.xml文件update,delete及insert返回值問題4. MySql關(guān)于null的函數(shù)使用分享5. mysql之如何查找配置文件my.ini的位置6. 詳解MySQL批量入庫的幾種方式7. mysql 數(shù)據(jù)庫優(yōu)化技巧8. 監(jiān)測你的SQL SERVER--讓瓶頸暴露9. MySQL系列之二 多實(shí)例配置10. DB2數(shù)據(jù)庫使用經(jīng)驗(yàn)漫談(1)
