1 mysql_safe

原理

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

1 /bin和/data和mysql_safe脚本位于同一目录;

2 如果本地目录找不到找到/bin和/data,mysqld_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-error和syslog则以前者为准; 
其他 
--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、查找basedir和ledir。 
2、查找datadir和my.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"并退出。 
注: 
1、mysqld_safe增加了一些安全特性,例如当出现错误时重启服务器并向错误日志文件写入运行时间信息。 
2、如果有的选项是mysqld_safe 启动时特有的,那么可以终端指定,如果在配置文件中指定需要放在[mysqld_safe]组里面,放在其他组不能被正确解析。 
3、mysqld_safe启动能够指定内核文件大小以及打开的文件的数量ulimit -n $size。 
4、MySQL程序首先检查环境变量,然后检查配置文件,最后检查终端的选项,说明终端指定选项优先级最高。 
  
代码
在一个死循环里调用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://bugs.mysql.com/bug.php?id=54035

 

2 单机安装多个数据库

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

非共享参数

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

如果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 directory和unix socket,避免新安装的数据库使用同样参数从而产生冲突;

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

注:如果host为localhost,则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的帐号和密码最好保持一致

 

 http://blog.itpub.net/15480802/viewspace-1412269/

 

my3319.cnf 配置:

 



[mysqld]
  socket =/usr/local/mysql/dbdata_3309/mysql3309.sock
  port = 3319
  pid-file = /usr/local/mysql/dbdata_3309/mysql3309.pid
  datadir = /usr/local/mysql/dbdata_3309
  basedir = /usr/local/mysql
  server-id=3309



 

启动MySQL:

mysqld_safe --defaults-file=/usr/local/mysql/dbdata_3309/my3319.cnf &