mysqld與mysqld_safe

神諭丶發表於2015-08-25
在一些場景下,通常使用 mysqld_safe &來啟動mysql,mysqld是什麼程式呢?

啟動例項之後,可以透過ps來檢視一下mysql的程式,發現其中不僅僅有mysqld_safe,還有一個mysqld程式。

op        4546     1  0 13:13 ?        00:00:00 /bin/sh ../bin/mysqld_safe
op       10168  4546  0 13:42 ?        00:00:00 /home/op/software/mysql/bin/mysqld --basedir=/home/op/software/mysql --datadir=/home/op/software/mysql/data --plugin-dir=/home/op/software/mysql/lib/plugin --log-error=/home/op/software/mysql/data/localhost.localdomain.err --pid-file=/home/op/software/mysql/data/localhost.localdomain.pid
op       13814  8439  0 14:01 pts/1    00:00:00 grep mysql

mysqld程式就是最基本的mysql的伺服器了
man mysqld的描述如下:

       mysqld, also known as MySQL Server, is the main program that does most

       of the work in a MySQL installation. MySQL Server manages access to the

       MySQL data directory that contains databases and tables. The data

       directory is also the default location for other information such as

       log files and status files.

實質上,用rpm或者yum這類包管理的工具,在linux下安裝時,所執行的 service mysqld start也就是啟動的這個程式。

而mysqld_safe呢?我man mysqld_safe了一下:


       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

       file. A description of error logging is given later in this section.

       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.


按mysqld_safe的manual資訊來說,它是一個當mysqld崩掉以後,能夠再次將mysqld啟動起來的程式。

測試:


[op@localhost ~]$ ps -ef | grep mysql
op        4546     1  0 13:13 ?        00:00:00 /bin/sh ../bin/mysqld_safe
op       10168  4546  0 13:42 ?        00:00:00 /home/op/software/mysql/bin/mysqld --basedir=/home/op/software/mysql --datadir=/home/op/software/mysql/data --plugin-dir=/home/op/software/mysql/lib/plugin --log-error=/home/op/software/mysql/data/localhost.localdomain.err --pid-file=/home/op/software/mysql/data/localhost.localdomain.pid
op       19643  8439  0 14:31 pts/1    00:00:00 grep mysql
[op@localhost ~]$ kill -9 10168
[op@localhost ~]$ ps -ef | grep mysql
op        4546     1  0 13:13 ?        00:00:00 /bin/sh ../bin/mysqld_safe
op       19702  4546  4 14:31 ?        00:00:00 /home/op/software/mysql/bin/mysqld --basedir=/home/op/software/mysql --datadir=/home/op/software/mysql/data --plugin-dir=/home/op/software/mysql/lib/plugin --log-error=/home/op/software/mysql/data/localhost.localdomain.err --pid-file=/home/op/software/mysql/data/localhost.localdomain.pid
op       19736  8439  0 14:31 pts/1    00:00:00 grep mysql

此時,mysqld的程式號被修改為19702(之前是10168)
其父程式仍然為4546。

mysqld在mysqld_safe的“守衛”下,再一次的被啟動起來。

正常關閉mysql:
[op@localhost bin]$ ./mysqladmin shutdown -uroot -p
Enter password: 
[op@localhost bin]$ ps -ef | grep mysql
op       21877 21454  0 14:42 pts/0    00:00:00 grep mysql

重新啟動之後:
[op@localhost bin]$ ps -ef |grep -v  ‘grep’ | grep mysql
op       22482    1  0 14:45 pts/0    00:00:00 /bin/sh ./mysqld_safe
op       22568 22482  1 14:45 pts/0    00:00:00 /home/op/software/mysql/bin/mysqld --basedir=/home/op/software/mysql --datadir=/home/op/software/mysql/data --plugin-dir=/home/op/software/mysql/lib/plugin --log-error=/home/op/software/mysql/data/localhost.localdomain.err --pid-file=/home/op/software/mysql/data/localhost.localdomain.pid
[op@localhost bin]$ kill -9 22482
[op@localhost bin]$ ps -ef |grep -v 'grep'| grep mysql
op       22568     1  0 14:45 pts/0    00:00:00 /home/op/software/mysql/bin/mysqld --basedir=/home/op/software/mysql --datadir=/home/op/software/mysql/data --plugin-dir=/home/op/software/mysql/lib/plugin --log-error=/home/op/software/mysql/data/localhost.localdomain.err --pid-file=/home/op/software/mysql/data/localhost.localdomain.pid

殺掉父程式也就是mysqld_safe之後,檢查一下,確實殺掉了。

再次殺掉mysqld程式:
[op@localhost bin]$ kill -9 22568
[op@localhost bin]$ ps -ef |grep -v 'grep'| grep mysql
[op@localhost bin]$

發現已經沒有mysql相關程式了。

說明在mysqld_safe的情況下,mysqld如果“意外”退出,mysqld_safe會守衛mysqld,並保證其一直啟動。


作者公眾號(持續更新)


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

相關文章