自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL中的Pid與Socket是什么?

數(shù)據(jù)庫 MySQL
不知道你有沒有注意過,MySQL 啟動時需要配置 pid 及 socket 文件路徑。偶爾還會出現(xiàn)因 pid 文件找不到而啟動失敗的現(xiàn)象,那么 pid 與 socket 文件究竟是干什么用的呢?我們一起來看下本篇文章。

[[405222]]

本文轉(zhuǎn)載自微信公眾號「MySQL技術(shù)」,作者MySQL技術(shù)。轉(zhuǎn)載本文請聯(lián)系MySQL技術(shù)公眾號。

前言:

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

1.pid-file介紹

MySQL 中的 pid 文件記錄的是當(dāng)前 mysqld 進程的 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)用戶放開,具體配置可參考如下:

  1. # my.cnf 配置文件 
  2. [mysqld] 
  3. pid-file  = /data/mysql/tmp/mysqld.pid 
  4.  
  5. # 查看mysqld進程 
  6. [root@localhost ~]# ps -ef|grep mysqld 
  7. 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 
  8. 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 
  9.  
  10. # 查看pid文件內(nèi)容  
  11. [root@localhost ~]# cat /data/mysql/tmp/mysqld.pid 
  12. 9353 

可以看到 pid 文件內(nèi)容只有一行,記錄了 mysqld 進程的 ID 。mysqld 進程啟動后會通過 create_pid_file 函數(shù)新建 pid 文件,通過 getpid() 獲取當(dāng)前進程號并將進程 ID 寫入 pid 文件。進程運行后會給 pid 文件加一個文件鎖,只有獲得 pid 文件寫入權(quán)限的進程才能正常啟動并把自身的 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 相關(guān)報錯解決方法其實都是類似的,首先要看下 error log 找到具體報錯,然后查看配置文件,確保 pid 文件目錄路徑正確且有權(quán)限有空間,之后可以看下 mysqld 進程是否存在,若存在可手動 kill 掉,若有殘留的 pid 文件也可以先刪掉,一切排查就緒后,再次重新啟動,一般即可成功。

2.socket文件介紹

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

通過設(shè)置 socket 變量可配置套接字文件路徑及名稱,默認(rèn)值為 /tmp/mysql.sock (對于某些發(fā)行格式,目錄可能有所不同)。參考配置如下:

  1. # my.cnf 配置文件 
  2. [mysqld] 
  3. socket = /data/mysql/tmp/mysql.sock 
  4. [client] 
  5. socket = /data/mysql/tmp/mysql.sock 
  6.  
  7. # 查看對應(yīng)目錄下的socket文件 
  8. root@localhost tmp]# ls -lh 
  9. total 8.0K 
  10. srwxrwxrwx 1 mysql mysql 0 Jun 10 15:19 mysql.sock 
  11. -rw------- 1 mysql mysql 6 Jun 10 15:19 mysql.sock.lock 
  12.  
  13. # 通過 -S 命令指定socket登錄 
  14. [root@localhost ~]# mysql -uroot -pxxxx -S /data/mysql/tmp/mysql.sock 
  15. mysql: [Warning] Using a password on the command line interface can be insecure. 
  16. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  17. Your MySQL connection id is 12 
  18. Server version: 8.0.22 MySQL Community Server - GPL 
  19.  
  20. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 
  21.  
  22. Oracle is a registered trademark of Oracle Corporation and/or its 
  23. affiliates. Other names may be trademarks of their respective 
  24. owners. 
  25.  
  26. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  27.  
  28. mysql> status 
  29. -------------- 
  30. mysql  Ver 8.0.22 for Linux on x86_64 (MySQL Community Server - GPL) 
  31.  
  32. Connection id:          12 
  33. Current database
  34. Current user:           root@localhost 
  35. SSL:                    Not in use 
  36. Current pager:          stdout 
  37. Using outfile:          '' 
  38. Using delimiter:        ; 
  39. Server version:         8.0.22 MySQL Community Server - GPL 
  40. Protocol version:       10 
  41. Connection:             Localhost via UNIX socket 
  42. Server characterset:    utf8mb4 
  43. Db     characterset:    utf8mb4 
  44. Client characterset:    utf8mb4 
  45. Conn.  characterset:    utf8mb4 
  46. UNIX socket:            /data/mysql/tmp/mysql.sock 
  47. Binary data as:         Hexadecimal 
  48. Uptime:                 1 hour 27 min 31 sec 
  49.  
  50. 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 方式連接。在本地登錄時,如果 my.cnf 配置文件中的 [client] 部分沒有指定 socket 文件路徑,mysql 默認(rèn)會去尋找 /tmp/mysql.sock ,所以如果 mysqld 服務(wù)啟動的時候,生成的 socket 文件不是默認(rèn)路徑的話,登陸可能會報錯(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 文件目錄權(quán)限要對 mysql 系統(tǒng)用戶放開。

總結(jié):

 

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

 

責(zé)任編輯:武曉燕 來源: MySQL技術(shù)
相關(guān)推薦

2021-09-14 09:49:12

Linuxsocket fd網(wǎng)絡(luò)

2024-12-24 14:11:57

2025-02-06 08:44:11

MySQLEXISTSIN

2025-02-20 10:04:35

2025-02-27 09:30:00

MySQLLog Buffer數(shù)據(jù)庫

2022-09-15 09:54:34

nullPython字符

2023-03-02 08:48:43

Linuxsubshell

2010-06-29 13:58:17

SNMPMIB

2021-04-26 07:51:00

JavaScript方法函數(shù)

2024-12-23 13:00:00

MySQLMVCC數(shù)據(jù)庫

2024-12-25 16:04:53

2022-12-21 08:04:19

socket圖解網(wǎng)絡(luò)

2018-04-09 14:25:06

數(shù)據(jù)庫MySQL索引

2021-06-28 17:21:49

MySQL性能Java

2020-03-05 10:28:19

MySQLMRR磁盤讀

2023-03-28 07:03:15

gRPCMetadata

2022-05-17 07:36:38

CSSBFC前端

2023-03-01 09:49:23

2022-07-28 08:34:59

事件委托JS

2022-06-29 08:37:03

事件循環(huán)JS 語言
點贊
收藏

51CTO技術(shù)棧公眾號