欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解MySQL中的pid與socket

 更新時間:2021年06月15日 09:58:46   作者:MySQL技術  
不知道你有沒有注意過,MySQL 啟動時需要配置 pid 及 socket 文件路徑。偶爾還會出現(xiàn)因 pid 文件找不到而啟動失敗的現(xiàn)象,那么 pid 與 socket 文件究竟是干什么用的呢?我們一起來看下本篇文章。
  • socket文件:當用Unix域套接字方式進行連接時需要的文件。
  • pid文件:MySQL實例的進程ID文件。

1.pid-file介紹

MySQL 中的 pid 文件記錄的是當前 mysqld 進程的 pid ,pid 亦即 Process ID ??梢酝ㄟ^ pid-file 參數(shù)來配置 pid 文件路徑及文件名,如果未指定此變量,則 pid 文件默認名為 host_name.pid ,存放的路徑默認放在 MySQL 的數(shù)據(jù)目錄。

建議指定 pid 文件名及路徑,pid 目錄權限要對 mysql 系統(tǒng)用戶放開,具體配置可參考如下:

# 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 函數(shù)新建 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 (對于某些發(fā)行格式,目錄可能有所不同)。參考配置如下:

# 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

查看上述連接狀態(tài)可知,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 系統(tǒng)用戶放開。

總結:

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

以上就是詳解MySQL中的pid與socket的詳細內容,更多關于MySQL pid與socket的資料請關注腳本之家其它相關文章!

相關文章

最新評論