mysql_safe和mysql_multi

本文深入剖析了MySQL启动脚本mysqld_safe的内部工作机制,包括其基本原理、执行流程、关键参数及其在单机安装多个数据库场景下的应用。此外,还详细介绍了如何安全地启动、关闭以及管理多个MySQL实例,确保系统稳定性和高效运行。

1 mysql_safe

原理

mysqld_safe其实为一个shell脚本(封装mysqld),启动时需要调用serverdatabase(/bin/data目录),因此需要满足下述条件之一:

1 /bin/datamysql_safe脚本位于同一目录;

2 如果本地目录找不到找到/bin/datamysqld_safe试图通过绝对路径定位(/usr/local)

shell> cd mysql_installation_directory

shell> bin/mysqld_safe &

如果从MySQL安装目录调用仍然失败,需要--ledir--datadir选项来指示服务器和数据库的安装目录。

注:service mysql start  , /etc/init.d/mysql  最终调的也是mysqld_safe

 

参数

路径

--basedir=path

The path to the MySQL installation directory.

--ledir=path

If mysqld_safe cannot find the server, use this option to indicate the path name to the directory where the server is located.

--datadir=path

The path to the data directory. 

选项文件

--defaults-extra-file=path

The name of an option file to be read in addition to the usual option files. This must be the first option on the command line if it is used. If the file does not exist or is otherwise inaccessible, the server will exit with an error.

--defaults-file=file_name

The name of an option file to be read instead of the usual option files. This must be the first option on the command line if it is used

输出日志

--log-error=file_name

Write the error log to the given file

--syslog, --skip-syslog

syslog causes error messages to be sent to syslog on systems that support the logger program. --skip-syslog suppresses the use of syslog; messages are written to an error log file.

注:如果上述3个选项都不指定,默认为—skip-syslog;如果同时指定log-errorsyslog则以前者为准;

其他

--malloc-lib=[lib_name]

The name of the library to use for memory allocation instead of the systemmalloc()library.

The --malloc-lib option works by modifying the LD_PRELOAD environment value to affect dynamic linking to enable the loader to find the memory-allocation library when mysqld runs:

--mysqld=prog_name

The name of the server program (in the ledir directory) that you want to start. This option is needed if you use the MySQL binary distribution but have the data directory outside of the binary distribution. If mysqld_safe cannot find the server, use the --ledir option to indicate the path name to the directory where the server is located.

 

执行流程

mysqld_safe脚本执行的基本流程:

1、查找basedirledir

2、查找datadirmy.cnf

3、解析my.cnf中的组[mysqld][mysqld_safe]并和终端里输入的命令合并。

4、对系统日志和错误日志的判断和相应处理,及选项--err-log参数的赋值。

5、对选项--user--pid-file--socket--port进行处理及赋值,保证启动时如果不给出这些参数它也会有值。

6、启动mysqld.

a)启动时会判断一个进程号是否存在,如果存在那么就在错误日志中记录"A mysqld process already exists"并且退出。

b)如不存在就删除进程文件,如果删除不了,那么就在错误日志中记录"Fatal error: Can't remove the pid file"并退出。

注:

1mysqld_safe增加了一些安全特性,例如当出现错误时重启服务器并向错误日志文件写入运行时间信息。

2、如果有的选项是mysqld_safe 启动时特有的,那么可以终端指定,如果在配置文件中指定需要放在[mysqld_safe]组里面,放在其他组不能被正确解析。

3mysqld_safe启动能够指定内核文件大小 ulimit -c $core_file_size以及打开的文件的数量ulimit -n $size

4MySQL程序首先检查环境变量,然后检查配置文件,最后检查终端的选项,说明终端指定选项优先级最高。

 

代码

在一个死循环里调用mysqld启动数据库,接下来检查pid-file,如果不存在说明mysqld被正常关闭则退出循环;接下来判断进程是否hang,如果是则kill -9

注:mysql_safe会反复尝试启动数据库,如果mysqld无法启动则会消耗大量CPU,为此5.6加入一个判断条件,若一秒内启动了5次则sleep 1

while true

do

  rm -f $safe_mysql_unix_port "$pid_file"     # Some extra safety

  if test -z "$args"

  then

    $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file="$pid_file"  >> "$err_log" 2>&1

  else

    eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file="$pid_file"  $args >> "$err_log" 2>&1"

  fi

  if test ! -f "$pid_file"            # This is removed if normal shutdown

  then

    echo "STOPPING server from pid file $pid_file"

    break

  fi

 

  if false && test $KILL_MYSQLD -eq 1

…..

done

 

非正常关闭数据库时应先杀死mysqld_safe,而后是mysqld,否则mysqld会被再次启动

http://blog.csdn.net/thinke365/article/details/5016411

http://bugs.mysql.com/bug.php?id=54035

 

 

2 单机安装多个数据库

单机可以安装多个版本的mysql binary

非共享参数

每个mysql binary必须拥有独自的数据目录,日志文件和pid文件,以及socketport

如果mysql安装在不同路径,则可为每个安装路径指定—basedir,这样每个安装路径都自动使用各自的数据目录,日志文件和pid文件;

此时只需为每个mysql单独指定—socket—port即可;

指定非默认端口和socket文件

shell> cmake . -DMYSQL_TCP_PORT=port_number  -DMYSQL_UNIX_ADDR=file_name -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.23

查看已安装数据库使用的参数,比如base directoryunix socket,避免新安装的数据库使用同样参数从而产生冲突;

shell> mysqladmin --host=host_name --port=port_number variables

注:如果hostlocalhost,则mysqladmin默认使用unix socket,可通--protocol=tcp显示声明

 

创建data directory

有两种创建方式:

1 新建数据目录  mysql_install_db

2 复制已有的数据目录,关闭现有mysqld;复制数据目录;修改my.cof;启动mysqld

 

如何启动特定的mysql

1

shell> mysqld_safe --defaults-file=/usr/local/mysql/my.cnf

shell> mysqld_safe --defaults-file=/usr/local/mysql/my.cnf2

2

shell> MYSQL_UNIX_PORT=/tmp/mysqld-new.sock

shell> MYSQL_TCP_PORT=3307

shell> export MYSQL_UNIX_PORT MYSQL_TCP_PORT

shell> mysql_install_db --user=mysql

shell> mysqld_safe --datadir=/path/to/datadir &

3

shell> mysqld_multi [options] {start|stop|reload|report} [GNR[,GNR] ...]

读取my.cnf中对应的[mysqldN]

# This file should probably be in your home dir (~/.my.cnf)

# or /etc/my.cnf

# Version 2.1 by Jani Tolonen

[mysqld_multi]

mysqld     = /usr/local/bin/mysqld_safe

mysqladmin = /usr/local/bin/mysqladmin

user       = multi_admin

password   = multipass

 

[mysqld6]

socket     = /tmp/mysql.sock6

port       = 3311

pid-file   = /usr/local/mysql/var6/hostname.pid6

datadir    = /usr/local/mysql/var6

language   = /usr/local/share/mysql/japanese

user       = jani

注:每个数据库用于关闭mysqld的帐号和密码最好保持一致

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15480802/viewspace-1412269/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15480802/viewspace-1412269/

源码直接下载地址: https://pan.quark.cn/s/d280357b18e5 在网页构建领域中,HTML5被视为当代网页工程的基础规范,其问世显著增强了页面的视觉表现力与用户互动性。本工程致力于运用HTML5技术开发一个电视剧信息展示页面,目的是呈现诸如剧名、演员构成、故事梗概等电视剧关键资料。接下来将深入阐释如何借助HTML5的结构化组件样式管理功能达成此项目目标。 我们必须掌握HTML5的核心框架。一个规范的HTML5文档一般包含`<!DOCTYPE html>`声明、`<html>`根标记、`<head>`头部标记`<body>`主体标记。在头部区域,可以配置网页的基本元数据,例如字符集设定、页面标题等。在主体部分,将具体构建电视剧信息列表的内容。 电视剧展示页面通常包含多个条目,每个条目对应一部电视剧。HTML5中的`<section>`标记用于内容模块化,适合表示单个电视剧的详细信息区域。每个`<section>`内部,可使用`<h2>`标题标记显示剧名,`<img>`图像标记插入宣传剧照,`<p>`段落标记呈现剧情介绍,而`<ul>`无序列表与`<li>`列表项标记则用于罗列演员阵容。 为了优化页面布局,需要借助CSS(层叠样式表)进行样式管理。HTML5引入了创新的CSS选择器与布局模型,例如FlexboxGrid,使页面布局更加灵活多变。在此场景下,可以利用Flexbox为电视剧信息列表实现自适应布局,保障在不同设备尺寸下均能呈现理想视觉效果。具体操作时,可将`<section>`标记设定为Flex容器,通过`display: flex;`属性,并运用`justify-content``align-items`属性调整子元素的对...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值