mysql关于mysqld_safe的总结

总结

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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30126024/viewspace-2221483/

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值