首页 > 数据库 >mysql_safe和mysql_multi

mysql_safe和mysql_multi

时间:2024-01-23 13:03:22浏览次数:23  
标签:multi -- safe pid mysqld file mysql

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 &

 



标签:multi,--,safe,pid,mysqld,file,mysql
From: https://blog.51cto.com/u_16532032/9377180

相关文章

  • mysqlslap压测
    mysqlslap是MySQL自带的压测工具:time./mysqlslap--no-defaults-usa-pcc.123-P18601--create-schema=test-S/tmp/mysql_sandbox18601.sock--number-of-queries=1000000--concurrency=10--query="select*fromtbwherea='1';" 上面: mysqlslap压测......
  • MySQL事件自动kill运行时间超时的SQL
    delimiter$createeventmy_long_running_trx_monitoronscheduleevery1minutestarts'2015-09-1511:00:00'oncompletionpreserveenabledobegindeclarev_sqlvarchar(500);declareno_more_long_running_trxintegerdefault0;declarec_......
  • MySQL数据库开发规范-EC
    最近一段时间一边在线上抓取SQL来优化,一边在整理这个开发规范,尽量减少新的问题SQL进入生产库。今天也是对公司的开发做了一次培训,PPT就不放上来了,里面有十来个生产SQL的案例。因为规范大部分还是具有通用性,所以也借鉴了像去哪儿和赶集的规范,但实际在撰写本文的过程中,每一条规范的背......
  • MySQL线程状态详解
    前言:我们常用showprocesslist或showfullprocesslist查看数据库连接状态,其中比较关注的是State列,此列表示该连接此刻所在的状态。那么你真的了解不同State值所表示的状态吗?下面我们参考官方文档来一探究竟。以MySQL5.7版本为例官方文档地址:https://dev.my......
  • MySQL学习总结 (InnoDB)
    主要内容:存储结构索引锁事务存储结构表索引组织表:表是根据主键顺序组织存放的。如果表中没有非空惟一索引,引擎会自动创建一个6字节大小的指针。主键的索引是定义索引的顺序,而不是建表时列的顺序。表空间:逻辑结构的最高层,所有的数据都存放在表空间中。段:表空间由各个段组成,常见的段......
  • [MySQL] 行级锁SELECT ... LOCK IN SHARE MODE 和 SELECT ... FOR UPDATE
    一、译文翻译来自官方文档:LockingReadsIfyouquerydataandtheninsertorupdaterelateddatawithinthesametransaction,theregularSELECTstatementdoesnotgiveenoughprotection.Othertransactionscanupdateordeletethesamerowsyoujustqueried.Inn......
  • 微博MySQL优化之路
    数据库是所有架构中不可缺少的一环,一旦数据库出现性能问题,那对整个系统都回来带灾难性的后果。并且数据库一旦出现问题,由于数据库天生有状态(分主从)带数据(一般还不小),所以出问题之后的恢复时间一般不太可控,所以,对数据库的优化是需要我们花费很多精力去做的。接下来就给大家介绍一下微......
  • mysql数据库tpch测试总结
    版本8.0.33语句1:--usingdefaultsubstitutionsselect l_returnflag, l_linestatus, sum(l_quantity)assum_qty, sum(l_extendedprice)assum_base_price, sum(l_extendedprice*(1-l_discount))assum_disc_price, sum(l_extendedprice*(1-l_discount)*(1......
  • MySQL5.7麒麟系统ARM架构下离线安装,搭建主从集群
    一、检查本机操作系统#一定要注意查看本机的操作系统,是amd(x86)还是arm(aarch)架构$uname-aLinuxServer-58aa6d9e-9412-4ab6-b496-2adc0af4e9c84.19.90-17.5.ky10.aarch64#1SMPFriAug713:35:33CST2020aarch64aarch64aarch64GNU/Linux$cat/etc/os-releaseN......
  • springboot+mybtais+mysql
    一、通过maven引入相应的包pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http......