mysql主从自动配置、诊断及测试脚本

#!/bin/sh

# ****************************************************************************

# Filename:

#                master_slave_configure.sh

# Function:

#                Automatic configuration of master to slave.

# Author:

#                [email protected]

#

# Usage Example:

#                ./master_slave_configure.sh mip:mport sip:sport

# ****************************************************************************

# Change History:

# ----------------------------------------------------------------------------

# Init Development              Peek.Ren                2014-09-16

# ****************************************************************************

#

function help()

{

cat << EOF

./master_slave_configure.sh IP1:PORT1 IP2:PORT2 [VARS]

VARS: inputting variables,pay attention to the input correct format.

EOF

}

function input_user()

{

read username

echo $username

}

function input_passwd()

{

stty -echo

read password

stty echo

echo $password

}

function get_master_info()

{

echo ""

echo ""

MYCMD="mysql -u$1 -p$2 -h$3 -P$4"

Master_Log_File=`${MYCMD} -e "show master status\G;" | sed ‘1d‘ | sed ‘3,$d‘ | awk -F ‘:‘ ‘{print $2}‘  | sed ‘2d‘ | sed ‘s/\ //g‘`

Master_Log_Pos=`${MYCMD} -e "show master status\G;" | sed ‘1d‘ | sed ‘3,$d‘ | awk -F ‘:‘ ‘{print $2}‘ | sed ‘1d‘ | sed ‘s/\ //g‘`

echo "[NOTICE]: Capture master data file and pos success!!"

}

function do_slave_config()

{

MYCMD="mysql -u$1 -p$2 -h$3 -P$4"

$MYCMD -e "stop slave;" &> /dev/null

if [ $? = 0 ];then

echo "[NOTICE]: stop slave success"

else

echo "[NOTICE]: stop slave error"

echo "[NOTICE]: Please check relation configure"

exit 1

fi

SLAVECONFIG="master_host=‘$db1_ip‘,master_port=$db1_port,master_user=‘$db1_user‘,master_password=‘$db1_passwd‘,master_log_file=‘$Master_Log_File‘,master_log_pos=$Master_Log_Pos"

CHANGE_MASTER="change master to ${SLAVECONFIG};"

$MYCMD -e "$CHANGE_MASTER" &> /dev/null

if [ $? = 0 ];then

echo "[NOTICE]: slave configure success"

else

echo "[NOTICE]: slave configure failure"

exit 1

fi

$MYCMD -e "start slave;" &> /dev/null

if [ $? = 0 ];then

echo "[NOTICE]: slave start success"

else

echo "[NOTICE]: slave start failure"

exit 1

fi

}

function get_ip()

{

echo ${1%%:*}

}

function get_port()

{

echo ${1##*:}

}

function parse_param()

{

if [ $# -ne 2 ] && [ $# -ne 3 ]

then

help

exit 0

fi

db1_ip=`get_ip $1`

db1_port=`get_port $1`

db2_ip=`get_ip $2`

db2_port=`get_port $2`

}

function slave_check()

{

MYCMD="mysql -u$1 -p$2 -h$3 -P$4"

Seconds_Behind_Master=`$MYCMD -e "show slave status\G;" | grep "Seconds_Behind_Master:" | awk -F ‘:‘ ‘{print $2}‘`

Slave_IO_Running=`$MYCMD -e "show slave status\G;" | grep "Slave_IO_Running:" | awk -F ‘:‘ ‘{print $2}‘`

Slave_SQL_Running=`$MYCMD -e "show slave status\G;" | grep "Slave_SQL_Running:" | awk -F ‘:‘ ‘{print $2}‘`

if [ $Seconds_Behind_Master = "NULL" ];then

echo "[NOTICE]: Disruption of replication"

if [ $Slave_IO_Running = "Yes" -a $Slave_SQL_Running = "No" ];then

echo "[NOTICE]: Slave_IO_Running is OK and Slave_SQL_Running is failure"

elif [ $Slave_IO_Running = "Connecting" -a $Slave_SQL_Running = "Yes" ];then

echo "[NOTICE]: Slave_IO_Running is Connecting and Slave_SQL_Running is OK"

elif [ $Slave_IO_Running = "No" -a $Slave_SQL_Running = "Yes" ];then

echo "[NOTICE]: Slave_IO_Running is failure and Slave_SQL_Running is OK"

else

echo "[NOTICE]: Slave_IO_Running is failure and Slave_SQL_Running is failure"

fi

elif [ $Seconds_Behind_Master -eq 0 ];then

echo "[NOTICE]: slave status OK!"

elif [ $Seconds_Behind_Master -gt 0 ];then

echo "[NOTICE]: slave has beened delayed compared with master"

else

echo "[NOTICE]: slave Unknown fault!"

fi

}

function consistence_master_check()

{

MYCMD="mysql -u$1 -p$2 -h$3 -P$4"

$MYCMD -e "use test;drop table if exists tt;create table tt(id int,name varchar(10));insert into tt select 1,‘rgf‘;" &> /dev/null

if [ $? = 0 ];then

echo "[NOTICE]: Master create table success."

else

echo "[NOTICE]: create table failure,please check create synatx."

exit 1

fi

}

function consistence_slave_check()

{

MYCMD="mysql -u$1 -p$2 -h$3 -P$4"

$MYCMD -e "use test;select * from tt;" && echo ""

if [ $? = 0 ];then

echo "[NOTICE]: Congratulations!slave configuration OK."

else

echo "[NOTICE]: Please check configure."

exit 1

fi

}

function delete_master_test()

{

MYCMD="mysql -u$1 -p$2 -h$3 -P$4"

$MYCMD -e "use test;drop table if exists tt;" &> /dev/null

echo "[NOTICE]: Master test data have been deleted!"

echo "[NOTICE]: Congratulations!master-slave configuration complete success."

}

function main()

{

parse_param [email protected]

echo -n "Please Input the Master Username($db1_ip:$db1_port)[repl_user]:"

db1_user=`input_user`

echo -n "Please Input the master Password($db1_ip:$db1_port):"

db1_passwd=`input_passwd`

echo ""

echo ""

echo -n "Please Input the Slave Username($db2_ip:$db2_port)[repl_user]:"

db2_user=`input_user`

echo -n "Please Input the Slave Password($db2_ip:$db2_port):"

db2_passwd=`input_passwd`

get_master_info $db1_user $db1_passwd $db1_ip $db1_port

echo "Now,beginning to configure slave database..."

echo "..........................................."

do_slave_config $db2_user $db2_passwd $db2_ip $db2_port

slave_check $db2_user $db2_passwd $db2_ip $db2_port

consistence_master_check $db1_user $db1_passwd $db1_ip $db1_port

consistence_slave_check $db2_user $db2_passwd $db2_ip $db2_port

echo "Now,beginning to delete master test data..."

delete_master_test $db1_user $db1_passwd $db1_ip $db1_port

}

main [email protected]

时间: 2024-10-05 04:26:33

mysql主从自动配置、诊断及测试脚本的相关文章

MySQL主从自动配置脚本

通过yum安装mariadb,并配置MySQL主从服务器主服务器:192.168.10.11从服务器:192.168.10.12 #!/bin/bash #==================================================== # Author: Mr.Song # Create Date: 2019-02-21 # Description: auto config MySQL master&slave #============================

企业生产MySQL主从同步配置

MySQL主从同步配置 前言:测试环境 一台mysql多个实例 主机IP地址 10.0.0.52 Master   3306 Salve    3307 一.主库要开启binlog服务 1. 1修改配置文件3306/my.cnf [[email protected] ~]# egrep "log-bin|server-id" /data/3306/my.cnf   log-bin = /data/3306/mysql-bin server-id = 1 1. 2查看主库有没有开启bin

centos下MySQL主从同步配置

centos下MySQL主从同步配置一.环境 主机: master操作系统:centos 5.3 IP:192.168.1.222 MySQL版本:5.0.77 从机: slave操作系统:centos 5.3 IP:192.168.1.220 MySQL版本:5.0.77 二.创建数据库 分别登录master机和slave机的mysql:mysql –u root –p 创建数据库:create database repl; 三.master机和slave机的相关配置 1.修改master机器

CentOS之MYSQL主从同步配置

一.主机master与salver均关闭防火墙iptables,执行service iptables stop命令: 设置SELINUX文件的SELINUX=disabled,如图所示: 二.创建数据库 分别登录master机和slave机的mysql:mysql –u root –p 创建数据库:create database repl; 在/usr/local/mysql目录下建立MYSQL复制的日志配置文件/usr/local/mysql/mysql-bin.log并赋予权限以及改变文件的

mysql 主从及配置

MySQL从库上有一个IO线程负责从主库取binlog到写到本地.另外有一个SQL线程负责执行这些本地日志,实现命令重放:一.环境 主机: master操作系统:centos 5.3 IP:192.168.1.222 MySQL版本:5.0.77 从机: slave操作系统:centos 5.3 IP:192.168.1.220 MySQL版本:5.0.77 二.创建数据库分别登录master机和slave机的mysql:mysql –u root –p创建数据库:create database

mysql主从数据库配置

在这里吧昨天做的主从数据库配置记录下来,免得以后折腾 数据库主从配置心得: master : 192.168.16.247 slave1 : 192.168.16.248 1 修改配置文件 /etc/mysql/my.cnf(如果my.cnf已有该配置项,则相应的进行修改) 主数据库: server-id = 1 log-bin = mysql-bin log-bin-index = mysql-bin.index #log_bin = /var/log/mysql/mysql-bin.log

mysql主从简单配置

接上一篇源码安装mysql5.6. 主从服务器均安装了msyql5.6 主服务器和从服务器均开启了logbin日志. 主从服务器均开启了server-id,且serverid编号不一样. 主服务器配置同步的帐号 grant replication slave on *.* to 'repl'@'192.168.4.31' identified by 'repl'; flush privileges: show master status;看主服务器二进制文件信息. 可以从第一个二进制日志第一行开

mysql主从库配置ps:mysql5.6

1 Mysql cluster版本主从服务器搭建实践 主从的作用:MySQL的主从服务器可以满足同步数据库,同步表,同步表内容,也可以指定仅同步某个数据库或某个表,还可以排除不同步某个数据库某个表. 同步原理:主从数据库同步主要是通过二进制日志来实现同步的. 从mysql官网下载最新版本:dev.mysql.com 本文环境: 主mysql服务器:10.1.68.110 安装版本:mysql-cluster-gpl-7.0.35-linux-x86_64-glibc23.tar.gz 从mysq

Mysql主从同步配置方案(Centos7)

最近在做项目高可用时,需要使用数据同步.由于只有双节点,且采用主主同步可能存在循环同步的风险,故综合考虑采用Mysql主从同步(Master-Slave同步). 可能没有接触过Mysql数据同步时,可能会觉得数据同步很难,但你按照如下操作一遍之后,会发现原来同步如此简单,毕竟我们只需要会配置,会排查问题,底层具体实现Mysql团队早帮忙实现了.废话不多说,直入话题: (1)首先,保证有两台设备(主.从),分别在两台设备上安装Mysql数据库.安装完成之后, 使用mysql -u[数据库用户名]