mysql關於mysql.server的總結

lusklusklusk發表於2018-11-23

1、mysql.server服務端工具,主要作用就是為了方便啟動和關閉mysql服務,這個指令碼中呼叫mysqld_safe來啟動mysqld

2、RPM包安裝時,發現/etc/rc.d/init.d/mysql和/usr/share/mysql/mysql.server裡面的東西一模一樣

3、mysql.server指令碼其實也是呼叫mysqld_safe指令碼去啟動MySQL伺服器的,但此時mysqld_safe不能使用引數選項即不能mysqld_safe --defaults-file這樣的模式,此時只能使用預設的/etc/my.cnf配置檔案

   相當於mysql.server把引數傳遞給mysqld_safe,mysqld_safe再傳遞給mysqld

4、mysql.server傳遞給mysqld_safe的引數,可以顯式看到的都是parse_server_arguments函式指定的引數,如下只能看到--datadir、--pid-file,但是也都是來自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、my.cnf會覆蓋mysql.server裡的basedir和datadir配置

   These may get overwritten by settings in the MySQL configuration files



解壓檔案安裝時mysql.server存放於解壓目錄的support-files/mysql.server

cp /mysql/mysql-5.5.25-linux2.6-i686/support-files/mysql.server /etc/rc.d/init.d/mysqld

chmod 700 /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig --level 345 mysqld on

service mysqld start


RPM安裝方式下,mysql.server存放於/usr/share/mysql/mysql.server

[root@mydb mysql55rpm]# ll /etc/rc.d/init.d/mysql

-rwxr-xr-x 1 root root 10585 Mar 10  2011 /etc/rc.d/init.d/mysql

[root@mydb mysql55rpm]# ll /usr/share/mysql/mysql.server

-rwxr-xr-x 1 root root 10585 Mar  9  2011 /usr/share/mysql/mysql.server

[root@mydb mysql55rpm]# ll /usr/sbin/mysqld

-rwxr-xr-x 1 root root 45012185 Mar 10  2011 /usr/sbin/mysqld


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

root      4448     1  0 16:57 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/mydb.pid

mysql     4527  4448  1 16:57 pts/1    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 mysql55rpm]# diff /etc/rc.d/init.d/mysql /usr/share/mysql/mysql.server                     

[root@mydb mysql55rpm]#




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中的內容

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

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



/usr/share/mysql/mysql.server中的內容

If you install MySQL on some other places than /usr/local/mysql, then you have to do one of the following things for this script to work

Run this script from within the MySQL installation directory

Create a /etc/my.cnf file with the following information:

[mysqld]

basedir=<path-to-mysql-installation-directory>

and copy my_print_defaults to /usr/bin


在/etc/init.d/mysql裡面修改basedir和datadir,會被my.cnf覆蓋,即my.cnf中的生效而/etc/init.d/mysql中的不生效(已經實驗驗證過的)

If you change base dir, you must also change datadir. These may get overwritten by settings in the MySQL configuration files

basedir=

datadir=


The following variables are only set for letting mysql.server find things

if test -z "$basedir" then basedir=/usr/local/mysql bindir=/usr/local/mysql/bin else  bindir="$basedir/bin"

if test -z "$datadir" then datadir=/usr/local/mysql/data else datadir="$basedir/data"


parse_server_arguments() {

  for arg do

    case "$arg" in

      --basedir=*) 

      --datadir=*)  

      --pid-file=*) 

      --service-startup-timeout=*) 


Try to find basedir in /etc/my.cnf

conf=/etc/my.cnf


parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`


echo $echo_n "Starting MySQL"

if test -x $bindir/mysqld_safe

then

#Give extra arguments to mysqld with the my.cnf file

$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

else

log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"


mysql.server 指令碼的主要作用就是為了方便啟動和關閉mysql服務,這個指令碼中需要呼叫的是 mysqld_safe這個指令碼

在呼叫mysqld_safe的時候要把-datadir、-pid-file,$other_args這些引數值傳入到mysqld_safe 指令碼,那怎麼來確定這些引數呢?

首先得知道 my_print_defaults 這個命令,這個命令就是從配置檔案中讀取mysql的引數值,具體可以透過my_print_defaults  --help 檢視:

parse_server_arguments 這個函式,其實只需要讀取—basedir,--datadir,--pid-file,--service-startup-timeout這些引數


[root@mydb]# my_print_defaults --help

Prints all arguments that is give to some program using the default files

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

相關文章