mysqld_safe之三言兩語

aaron8219發表於2018-06-03

 

    today,one buddy in IMG wechat group 2 asked “why i`ve installed the MySQL 5.7 on linux server,but there`s no mysqld_safe command at all?”so,here i`d like to post this article to say something about it.first of all,let`s see the command parameter and usage:

 

 1 #mysqld_safe --help
 2 Usage: /usr/local/mysql/bin/mysqld_safe [OPTIONS]
 3  The following options may be given as the first argument:
 4   --no-defaults              Don`t read the system defaults file
 5   --defaults-file=FILE       Use the specified defaults file
 6   --defaults-extra-file=FILE Also use defaults from the specified file
 7 
 8  Other options:
 9   --ledir=DIRECTORY          Look for mysqld in the specified directory
10   --open-files-limit=LIMIT   Limit the number of open files
11   --core-file-size=LIMIT     Limit core files to the specified size
12   --timezone=TZ              Set the system timezone
13   --malloc-lib=LIB           Preload shared library LIB if available
14   --mysqld=FILE              Use the specified file as mysqld
15   --mysqld-version=VERSION   Use "mysqld-VERSION" as mysqld
16   --nice=NICE                Set the scheduling priority of mysqld
17   --plugin-dir=DIR           Plugins are under DIR or DIR/VERSION, if
18                              VERSION is given
19   --skip-kill-mysqld         Don`t try to kill stray mysqld processes
20   --syslog                   Log messages to syslog with `logger`
21   --skip-syslog              Log messages to error log (default)
22   --syslog-tag=TAG           Pass -t "mysqld-TAG" to `logger`
23   --mysqld-safe-log-         TYPE must be one of UTC (ISO 8601 UTC),
24     timestamps=TYPE          system (ISO 8601 local time), hyphen
25                              (hyphenated date a la mysqld 5.6), legacy
26                              (legacy non-ISO 8601 mysqld_safe timestamps)
27 
28 All other options are passed to the mysqld program.
29 
30 [root@zlm3 07:43:01 /data/mysql/mysql3306]
31 #

 

    the most simplest usage of mysqld_safe way is to just use `–defaults-file` to specify which “my.cnf” you want to use,just like:

 

 1 [root@zlm3 07:54:10 /usr/local/mysql/bin]
 2 #pkill mysqld
 3 
 4 [root@zlm3 07:54:40 /usr/local/mysql/bin]
 5 #ps aux|grep mysqld
 6 root      6302  0.0  0.0 112640   960 pts/0    R+   07:54   0:00 grep --color=auto mysqld
 7 
 8 [root@zlm3 07:54:51 /usr/local/mysql/bin]
 9 #mysqld_safe --defaults-file=/data/mysql/mysql3306/my.cnf &
10 [1] 6307
11 
12 [root@zlm3 07:55:21 /usr/local/mysql/bin]
13 #2018-06-04T05:55:21.758814Z mysqld_safe Logging to `/data/mysql/mysql3306/data/error.log`.
14 2018-06-04T05:55:21.786306Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql3306/data
15 ^C
16 
17 [root@zlm3 07:55:35 /usr/local/mysql/bin]
18 #ps aux|grep mysqld
19 root      6307  0.1  0.1 113252  1628 pts/0    S    07:55   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql3306/my.cnf
20 mysql     7328  1.0 17.4 1069424 177592 pts/0  Sl   07:55   0:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --open-files-limit=65535 --pid-file=mysql.pid --socket=/tmp/mysql3306.sock --port=3306
21 root      7361  0.0  0.0 112640   960 pts/0    R+   07:55   0:00 grep --color=auto mysqld
22 
23 [root@zlm3 07:55:44 /usr/local/mysql/bin]
24 #

 

    here we can see,there`re two processes running,one is mysqld_safe,another one is the mysqld.even if you use “kill -9 7328” to stop the mysqld process,but subsequently you`ll find that the mysqld will startup again soon,unless you kill mysqld process by using “pkill mysqld” as below:

 

 1 [root@zlm3 07:55:44 /usr/local/mysql/bin]
 2 #kill -9 7328
 3 
 4 [root@zlm3 07:57:15 /usr/local/mysql/bin]
 5 #/usr/local/mysql/bin/mysqld_safe: line 198:  7328 Killed                  nohup /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --open-files-limit=65535 --pid-file=mysql.pid --socket=/tmp/mysql3306.sock --port=3306 < /dev/null > /dev/null 2>&1
 6 2018-06-04T05:57:15.076914Z mysqld_safe Number of processes running now: 0
 7 2018-06-04T05:57:15.083092Z mysqld_safe mysqld restarted
 8 ^C
 9 
10 [root@zlm3 07:57:20 /usr/local/mysql/bin]
11 #ps aux|grep mysqld
12 root      6307  0.0  0.1 113256  1676 pts/0    S    07:55   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql3306/my.cnf
13 mysql     7385  2.4 17.7 1081288 180624 pts/0  Sl   07:57   0:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --open-files-limit=65535 --pid-file=mysql.pid --socket=/tmp/mysql3306.sock --port=3306
14 root      7419  0.0  0.0 112640   960 pts/0    R+   07:57   0:00 grep --color=auto mysqld
15 
16 [root@zlm3 07:57:24 /usr/local/mysql/bin]
17 #pkill mysqld
18 
19 [root@zlm3 07:57:37 /usr/local/mysql/bin]
20 #2018-06-04T05:57:38.957789Z mysqld_safe mysqld from pid file /data/mysql/mysql3306/data/mysql.pid ended
21 ^C
22 [1]+  Done                    mysqld_safe --defaults-file=/data/mysql/mysql3306/my.cnf
23 
24 [root@zlm3 07:57:48 /usr/local/mysql/bin]
25 #ps aux|grep mysqld
26 root      7439  0.0  0.0 112640   956 pts/0    R+   07:57   0:00 grep --color=auto mysqld
27 
28 [root@zlm3 07:57:55 /usr/local/mysql/bin]
29 #

 

    why will that happen?the consequence is trigged by mysqld_safe which can protect mysqld from killing by some command like “kill -9” accidentally.or in the other case,the mysqld process shuted down by the OS because of some unknowing issue or bug.that will help you to prevent the application from disconnecting the MySQL server when mysqld process down.but why the buddy`s MySQL server hasnot the mysqld_safe command?here`s the explaination:
 

Note

For some Linux platforms, MySQL installation from RPM or Debian packages includes systemd support for managing MySQL server startup and shutdown. On these platforms, mysqld_safe is not installed because it is unnecessary. For more information, see Section 2.5.9, “Managing MySQL Server with systemd”.


 

    therefore,if you want the mysqld_safe feature,i rather recommend you to install MySQL server with binary distribution instead of rpm distribution.

someone said that mysqld_safe will not be supported in the future release,but what i`ve seen is the version 8.0 official document is that it still be recommended:
 

 MySQL 8.0 Reference Manual  /    /  mysqld_safe — MySQL Server Startup Script

4.3.2 mysqld_safe — MySQL Server Startup Script

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.

 來源: https://dev.mysql.com/doc/refman/8.0/en/mysqld-safe.html 


 

summary:

    another buddy in zst techique wechat group said that it will be messy in troubleshooting while using mysqld_safe to startup mysqld,`cause in some case,the mysqld_safe may lead to the ceaseless restarting of mysqld.furthermore,it may destroy the evidences and logs which can be diagnosted by DBAs.anyhow,in my opinion it depends:

  • if the bussines continuity is the first thing you need to consider,i recommend to use mysqld_safe method.
  • if your monitor system is strong enough or the application on the MySQL server is not so important such as enterprise management system,BBS system,i recommend to use mysqld method.

相關文章