MySQL主从同步Shell实现

前言

mysql主从复制是mysql高可用性,高性能(负载均衡)的基础,Mysql主从复制起到容灾、读写分离、备份等作用.

用一个图片来说明这个原理:

MySQL主从同步Shell实现_第1张图片

实现思路

Master 服务器需要处理my.cnf 的配置,需要满足主机掉电以后重启主从服务。

Slave 服务器需要将原来的主数据同步,配置参数,配置和启动同步。

Shell 实现

Master

#启动MySQL
start_mysql()
{
	#启动并检查MySQL,每5秒检查一次,超时时间1分钟
	toInfoLog "begin start mysql" | tee -a $MasterTOSlavelog
	mysqlcounts=0
	MysqlStatue=0
	mysqld_multi start ${DB_PORT}
	sleep 5
	while (($mysqlcounts<=60));do
		MysqlStatue=$(mysqld_multi report | grep "mysqld${DB_PORT} is running" | wc -l)
		if [ $MysqlStatue -eq 1 ]  
		then
			break
		fi
		sleep 5
		mysqlcounts=$[mysqlcounts+5]
	done
	
	if [ $MysqlStatue -eq 0 ]
	then
		toErrorLog "start mysql failed" | tee -a $MasterTOSlavelog
		exit 1
	fi
	toInfoLog "start mysql success" | tee -a $MasterTOSlavelog
}

#停止MySQL
stop_mysql()
{
	#停止并检查MySQL,每5秒检查一次,超时时间1分钟
	toInfoLog "begin stop mysql" | tee -a $MasterTOSlavelog
	mysqlcounts=0
	MysqlStatue=0
	mysqld_multi stop ${DB_PORT}
	sleep 5
	while (($mysqlcounts<=60));do
		MysqlStatue=$(mysqld_multi report | grep "mysqld${DB_PORT} is not running" | wc -l)
		if [ $MysqlStatue -eq 1 ]  
		then
			break
		fi
		sleep 5
		mysqlcounts=$[mysqlcounts+5]
	done
	
	if [ $MysqlStatue -eq 0 ]
	then
		toInfoLog "stop mysql failed" | tee -a $MasterTOSlavelog
		exit 1
	fi
	toInfoLog "stop mysql success" | tee -a $MasterTOSlavelog
}

#添加master mysql配置参数
addParameters(){
	toInfoLog "begin set my.cnf" | tee -a $MasterTOSlavelog
	if [ `cat /etc/my.cnf | grep "log-bin=mysql-bin" | wc -l` -eq 0 ]
	then		
		sed -i  's/server-id=3308/server-id=3308\nlog-bin=mysql-bin\nexpire_logs_days=3\nsync_binlog=1\nslave-skip-errors=all\nbinlog-do-db=epmsgroup/g' /etc/my.cnf
	fi
	toInfoLog "set my.cnf success" | tee -a $MasterTOSlavelog
}

Slave

#设置my.cnf
addParameters()
{
	toInfoLog "begin set my.cnf" | tee -a $slavelog
	if [ `cat /etc/my.cnf | grep "relay-log=mysqld-relay-bin" | wc -l` -eq 0 ]
	then	
		sed -i  's/server-id=3308/server-id=2\nrelay-log=mysqld-relay-bin\nexpire_logs_days=3\nsync_binlog=1\nslave-skip-errors=all\nreplicate-do-table=epmsgroup.pms_power_station_t/g' /etc/my.cnf 
	fi
	toInfoLog "set my.cnf success" | tee -a $slavelog
}

#配置和启动同步
doMasterTOSlave()
{
	toInfoLog "begin set slave" | tee -a $slavelog
	mysql -h${HOST_B} -u${dbUserName} -P${DBPort} -p${dbPassword} -e "stop slave;"
	#添加B服务器主节点操作,写入正确的主节点IP、PORT、USER、PASSWD、及在主节点使用show master status;查询出来的文件名及偏移值:
	mysql -u${dbUserName} -p${dbPassword} -P${DBPort} -h${HOST_B} --protocol=TCP -e "change master to master_host='${HOST_A}', master_user='${dbUserName}', master_password='${dbPassword}',master_port=3308, master_log_file='${master_log_file_A}', master_log_pos=${master_log_pos_A};"
	mysql -h${HOST_B} -u${dbUserName} -P${DBPort} -p${dbPassword} -e "start slave;"
	toInfoLog "set slave success" | tee -a $slavelog
}

#检查同步状态
check_slaveStatus()
{
	toInfoLog "begin check slaveStatus" | tee -a $slavelog
	flag_BIO=`mysql -u${dbUserName} -P${DBPort} -p${dbPassword} -h${HOST_B} -e "show slave status\G;" |grep -i "Slave_IO_Running: Yes"|wc -l`
	flag_BSQL=`mysql -u${dbUserName} -P${DBPort} -p${dbPassword} -h${HOST_B} -e "show slave status\G;" |grep -i "Slave_SQL_Running: Yes"|wc -l`
	masterToslaveNum=$((${flag_BIO}+${flag_BSQL}))
	if [ ${masterToslaveNum} -eq 2 ]
	then
		toInfoLog "${HOST_A} to ${HOST_B}:master to slave success! masterToslaveNum:${masterToslaveNum};flag_BIO:${flag_BIO};flag_BSQL:${flag_BSQL}" | tee -a $slavelog
	else
		toInfoLog "${HOST_A} to ${HOST_B}:master to slave false! masterToslaveNum:${masterToslaveNum};flag_BIO:${flag_BIO};flag_BSQL:${flag_BSQL}" | tee -a $slavelog
		exit 1
	fi
	toInfoLog "check slaveStatus success" | tee -a $slavelog
}

结语

实际的代码根据业务需要先进行一次同步,会用到mysqldump,需要考虑到创建一个只有查询权限的用户。

你可能感兴趣的:(Shell)