mysql關於mysqld_safe的總結

lusklusklusk發表於2018-11-23

總結

1、mysqld_safe是服務端工具,用於啟動mysqld,並且是mysqld的守護程式,mysqld_safe加&在後臺執行$BASEDIR/bin/mysqld_safe &

2、因為mysqld_safe是mysqld的守護程式,所以mysqld_safe指令碼會在啟動MySQL伺服器後繼續監控其執行情況,並在其當機時重新啟動它。

3、直接使用mysqld_safe啟動mysqld時,mysqld_safe可以使用引數選項見mysqld_safe --help,此時可以使用其他配置檔案,相當於mysqld_safe把引數傳遞給mysqld

4、mysql.server指令碼其實也是呼叫mysqld_safe指令碼去啟動MySQL伺服器的,但此時mysqld_safe不能使用引數選項即不能mysqld_safe --defaults-file這樣的模式,此時只能使用預設的/etc/my.cnf配置檔案,就算是ps -ef|grep mysql顯式看到的資訊也只是parse_server_arguments函式指定的引數,也是來自my.cnf,相當於mysql.server把my.cnf中的引數傳遞給mysqld_safe,mysqld_safe再傳遞給mysqld,如下看到的--datadir也是來自my.cnf

   [root@mydb]# ps -ef|grep mysql

   root      6687     1  0 19:38 pts/1    00:00:00 /bin/sh /mysql/mysql57/bin/mysqld_safe --datadir=/mysql/mysql57/data --pid-file=/mysql/mysql57/data/mydb.pid

5、mysqld_safe指定的--defaults-file會覆蓋my.cnf中的配置

   ./bin/mysqld_safe --defaults-file=/etc/my.cnf2 

6、mysqld_safe指定的--datadir引數會覆蓋my.cnf中的配置

   ./bin/mysqld_safe --datadir=/mysql/mysql57/data2 &

   mysqld_safe中這條語句they are added to mysqld command line to override settings from my.cnf

   它們被新增到mysqld命令列以覆蓋my.cnf中的設定  

7、mysqld直接啟動使用--datadi引數,也會覆蓋my.cnf中的配置

   &BASEDIR/bin/mysqld --datadir=/mysql/mysql57/data2 --user=root &

8、mysqld_safe多長時間檢測一次mysqld呢,即多長時間去把mysqld拉起

這是linux的機制,不是mysql的機制,因為mysqld_safe是父程式,mysqld是子程式,一旦子程式奔潰,linux訊號機制下父程式馬上就知道自己名下的子程式出問題了,會立即重新fork出一個新的子程式

9、mysqld的埠預設3306,mysqld_safe沒有埠



https://dev.mysql.com/doc/refman/5.7/en/mysqld-safe.html

mysqld_safe is the recommended way to start a mysqld server on Unix. mysqld_safe adds some safety features such as restarting the server when an error occurs and logging runtime information to an error log. A description of error logging is given later in this section.

mysqld_safe是在Unix上啟動mysqld伺服器的推薦方法。 mysqld_safe新增了一些安全功能,例如在發生錯誤時重新啟動伺服器並將執行時資訊記錄到錯誤日誌中。 本節後面將給出錯誤日誌記錄的說明。


mysqld_safe tries to start an executable named mysqld. To override the default behavior and specify explicitly the name of the server you want to run, specify a --mysqld or --mysqld-version option to mysqld_safe. You can also use --ledir to indicate the directory where mysqld_safe should look for the server.

Many of the options to mysqld_safe are the same as the options to mysqld. See Section 5.1.6, “Server Command Options”.

Options unknown to mysqld_safe are passed to mysqld if they are specified on the command line, but ignored if they are specified in the [mysqld_safe] group of an option file. See Section 4.2.6, “Using Option Files”.

mysqld_safe reads all options from the [mysqld], [server], and [mysqld_safe] sections in option files. For example, if you specify a [mysqld] section like this, mysqld_safe will find and use the --log-error option:

[mysqld]

log-error=error.log

mysqld_safe嘗試啟動名為mysqld的可執行檔案。 要覆蓋預設行為並明確指定要執行的伺服器的名稱,請為mysqld_safe指定--mysqld或--mysqld-version選項。 您還可以使用--ledir指示mysqld_safe應該查詢伺服器的目錄。

mysqld_safe的許多選項與mysqld的選項相同。 請參見第5.1.6節“伺服器命令選項”。

mysqld_safe未知的選項如果在命令列中指定則傳遞給mysqld,但如果在選項檔案的[mysqld_safe]組中指定它們則忽略。 請參見第4.2.6節“使用選項檔案”。

mysqld_safe從選項檔案中的[mysqld],[server]和[mysqld_safe]部分讀取所有選項。 例如,如果您像如下一樣指定[mysqld]部分,mysqld_safe將找到並使用--log-error選項:

[mysqld]

log-error=error.log




mysqld_safe是守護程式,會自動拉起mysqld

[root@mydb ~]# ps -ef|grep mysql|grep -v grep

root      3075     1  0 13:27 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/mydb.pid

mysql     4205  3075  0 13:55 ?        00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/mydb.err --pid-file=/var/lib/mysql/mydb.pid


[root@mydb ~]# kill -9 4205


[root@mydb ~]# ps -ef|grep mysql|grep -v grep

root      3075     1  0 13:27 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/mydb.pid

mysql     4267  3075  5 13:55 ?        00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/mydb.err --pid-file=/var/lib/mysql/mydb.pid





mysqld的埠預設3306,mysqld_safe沒有埠

[root@mydb ~]# netstat -anp |grep 4267

tcp        0      0 :::3306                     :::*                        LISTEN      4267/mysqld

unix  2      [ ACC ]     STREAM     LISTENING     13876  4267/mysqld         /var/lib/mysql/mysql.sock

[root@mydb ~]# netstat -anp |grep 3075



mysql.server啟動,預設使用/etc/my.cnf配置檔案資訊,--datadir=/mysql/mysql57/data,看到的--datadir、--pid-file都是parse_server_arguments函式指定的引數

[root@mydb]# service mysqld start

[root@mydb]# ps -ef|grep mysql

root      7747     1  0 21:13 pts/1    00:00:00 /bin/sh /mysql/mysql57/bin/mysqld_safe --datadir=/mysql/mysql57/data --pid-file=/mysql/mysql57/data/mydb.pid

mysql     7866  7747  8 21:13 pts/1    00:00:00 /mysql/mysql57/bin/mysqld --basedir=/mysql/mysql57 --datadir=/mysql/mysql57/data --plugin-dir=/mysql/mysql57/lib/plugin --user=mysql --log-error=/mysql/mysql57/data/mydb.err --pid-file=/mysql/mysql57/data/mydb.pid


mysqld_safe啟動,使用了其他配置檔案,/etc/my.cnf2中指定了--datadir=/mysql/mysql57/data2,覆蓋了/etc/my.cnf中指定的/mysql/mysql57/data

[root@mydb mysql57]# pwd

/mysql/mysql57

[root@mydb mysql57]# ./bin/mysqld_safe --defaults-file=/etc/my.cnf2 &

[root@mydb mysql57]# ps -ef|grep mysql

root      7394  4982  0 21:00 pts/1    00:00:00 /bin/sh ./bin/mysqld_safe --defaults-file=/etc/my.cnf2

mysql     7486  7394  8 21:00 pts/1    00:00:00 /mysql/mysql57/bin/mysqld --defaults-file=/etc/my.cnf2 --basedir=/mysql/mysql57 --datadir=/mysql/mysql57/data2 --plugin-dir=/mysql/mysql57/lib/plugin --user=mysql --log-error=/mysql/mysql57/data2/mydb.err --pid-file=/mysql/mysql57/data2/mydb.pid


mysqld_safe啟動,使用了--datadir=/mysql/mysql57/data3,會覆蓋/etc/my.cnf中指定的/mysql/mysql57/data,其他配置沿用my.cnf的

[root@mydb mysql57]# ./bin/mysqld_safe --datadir=/mysql/mysql57/data3 &

[root@mydb mysql57]# ps -ef|grep mysql

root      7996  4982  0 21:20 pts/1    00:00:00 /bin/sh ./bin/mysqld_safe --datadir=/mysql/mysql57/data3

mysql     8100  7996 14 21:20 pts/1    00:00:00 /mysql/mysql57/bin/mysqld --basedir=/mysql/mysql57 --datadir=/mysql/mysql57/data3 --plugin-dir=/mysql/mysql57/lib/plugin --user=mysql --log-error=/mysql/mysql57/data3/mydb.err --pid-file=/mysql/mysql57/data3/mydb.pid



mysqld_safe指令碼執行的基本流程:


1、檢查mysqld_safe命令列引數啟用了哪些選項,詳見Usage: $0 [OPTIONS]

2、查詢basedir和ledir

(basedir表示MySQL安裝目錄,ledir表示mysqld程式的目錄即bin目錄)

3、查詢datadir

4、解析my.cnf中的組[mysqld]和[mysqld_safe]並和終端裡輸入的命令合併。

5、呼叫parse_arguments函式解析使用者傳遞的所有引數,parse_arguments()函式只讀取--basedir、--datadir、--pid-file、--plugin-dir、--user、--log-error、--port、--socket這些個引數

6、對選項--user、--pid-file、--socket、--port、--log-error進行處理及賦值,保證啟動時如果不給出這些引數它也會有值

7、啟動mysqld,啟動時會判斷一個程式號是否存在,如不存在就刪除程式檔案

8、啟動時對錶進行檢查。

9、如果伺服器異常關閉,那麼會restart




mysqld_safe程式碼中的內容

mysql.server works by first doing a cd to the base directory and from there executing mysqld_safe

mysql.server的工作原理是先cd進入基目錄,然後執行mysqld_safe


Usage: $0 [OPTIONS]

--no-defaults              Don't read the system defaults file

--defaults-file=FILE       Use the specified defaults file

--defaults-extra-file=FILE Also use defaults from the specified file

--ledir=DIRECTORY          Look for mysqld in the specified directory

...

All other options are passed to the mysqld program

所有其他選項都傳遞給mysqld程式


We only need to pass arguments through to the server if we don't handle them here.  So, we collect unrecognized options (passed on the command line) into the args variable

如果我們不在這裡處理它們,我們只需要將引數傳遞給伺服器。 因此,我們將無法識別的選項(在命令列上傳遞)收集到args變數中


parse_arguments() {

# these get passed explicitly to mysqld//這些明確地傳遞給mysqld

--basedir=*) MY_BASEDIR_VERSION="$val" ;;

--datadir=*) DATADIR="$val" ;;

--pid-file=*) pid_file="$val" ;;

--plugin-dir=*) PLUGIN_DIR="$val" ;;

--user=*) user="$val"; SET_USER=1 ;;

# these might have been set in a [mysqld_safe] section of my.cnf//這些可能是在my.cnf的[mysqld_safe]部分設定的

# they are added to mysqld command line to override settings from my.cnf//它們被新增到mysqld命令列以覆蓋my.cnf中的設定

--log-error=*) err_log="$val" ;;

--port=*) mysql_tcp_port="$val" ;;

--socket=*) mysql_unix_port="$val" ;;

# mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])//mysqld_safe特定的選項 - 必須在my.cnf中設定([mysqld_safe])

--core-file-size=*) core_file_size="$val" ;;


First, try to find BASEDIR and ledir (where mysqld is)

首先,嘗試找到BASEDIR和ledir(mysqld所在的地方)


Second, try to find the data directory

其次,嘗試查詢資料目錄


Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe] and then merge with the command line arguments

從my.cnf檔案,組[mysqld]和[mysqld_safe]獲取第一個引數,然後與命令列引數合併


A pid file is created for the mysqld_safe process. This file protects the server instance resources during race conditions

為mysqld_safe程式建立了一個pid檔案。 此檔案在競爭條件期間保護伺服器例項資源

safe_pid="$DATADIR/mysqld_safe.pid"

log_error "A mysqld_safe process already exists"

log_error "Fatal error: Can't remove the mysqld_safe pid file"


If the user doesn't specify a binary, we assume name "mysqld"

does not exist or is not executable. Please cd to the mysql installation directory and restart this script from there as follows:

./bin/mysqld_safe&

如果使用者沒有指定二進位制檔案,我們假設名稱為“mysqld”

不存在或不可執行。 請cd到mysql安裝目錄並從那裡重新啟動此指令碼,如下所示:

./bin/mysqld_safe&


If there exists an old pid file, check if the daemon is already running

如果存在舊的pid檔案,請檢查守護程式是否已在執行

log_error "A mysqld process already exists"

log_error Fatal error: Can't remove the pid file

mysqld daemon not started


Uncomment the following lines if you want all tables to be automatically checked and repaired during startup. 

You should add sensible key_buffer and sort_buffer values to my.cnf to improve check performance or require less disk space.

Alternatively, you can start mysqld with the "myisam-recover" option. 

如果要在啟動期間自動檢查和修復所有表,請取消註釋以下行。 

您應該向my.cnf新增合理的key_buffer和sort_buffer值,以提高檢查效能或減少磁碟空間。

或者,您可以使用“myisam-recover”選項啟動mysqld。


Avoid 'nohup: ignoring input' warning

log_notice "Starting $MYSQLD daemon with databases from $DATADIR"

log_notice "$pid_file.shutdown present. The server will not restart."

log_notice "mysqld restarted"



解壓檔案安裝時,如果使用絕對路徑執行mysqld_safe時,它預設去/usr/local/mysql/bin/mysqld路徑下找mysqld會報錯,到mysqld_safe上級bin目錄下,使用相對路徑執行mysql_safe時沒有問題./bin/mysqld_safe&

[root@mydb ~]# /mysql/mysql57/bin/mysqld_safe --port=3306 --default-file=/etc/my.cnf

181026 15:26:03 mysqld_safe Logging to '/mysql/mysql57/data/mydb.err'.

181026 15:26:03 mysqld_safe The file /usr/local/mysql/bin/mysqld

does not exist or is not executable. Please cd to the mysql installation

directory and restart this script from there as follows:

./bin/mysqld_safe&

See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information


mysqld_safe程式碼中裡面其實也給出了上面的答案

MY_PWD=`pwd`

ledir="$MY_PWD/bin"                   # Where mysqld is

# Since we didn't find anything, used the compiled-in defaults

else

MY_BASEDIR_VERSION='/usr/local/mysql'

ledir='/usr/local/mysql/bin'

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30126024/viewspace-2221483/,如需轉載,請註明出處,否則將追究法律責任。

相關文章