1.更改配置文件

############主##########
server_id = 1
binlog-do-db=tx
binlog-ignore-db = mysql
binlog_checksum = crc32
max_binlog_size = 500M
skip-slave-start

#######################
#########从############
server_id = 2
binlog-do-db=tx
binlog-ignore-db = mysql
binlog_checksum = crc32
max_binlog_size = 500M
skip-slave-start
##########################

2

主
grant replication slave on *.* to slave@'192.168.30.229' identified by "123456";
flush privileges; 
show master status;
从
change master to master_host='192.168.30.228',\
master_user='root',\
master_password='123456',\
master_port=3306,\
master_log_file='mysql-bin.000005',\
master_log_pos=409;
start slave; 
show slave status\G;

3 备份数据


#!/bin/bash
#数据库IP
dbserver='127.0.0.1'
#数据库用户名
dbuser='root'
#数据密码
dbpasswd='123456'
#数据库,如有多个库用空格分开
dbname='tongxiang'
#备份时间
backtime=`date +%Y%m%d`
#备份输出日志路径
logpath='/opt/mysql_bakup'

echo "################## ${backtime} #############################" 
echo "开始备份" 
#日志记录头部
echo "" >> ${logpath}/mysqlback.log
echo "-------------------------------------------------" >> ${logpath}/mysqlback.log
echo "备份时间为${backtime},备份数据库表 ${dbname} 开始" >> ${logpath}/mysqlback.log
#正式备份数据库
$mysqldump --single-transaction --master-data=2 -h ${dbserver}  -u${dbuser} -p${dbpasswd} --databases ${dbname} > ${logpath}/${backtime}_tongxiang.sql 2>> ${logpath}/mysqlback.log;
#备份成功以下操作
if [ "$?" == 0 ];then
#删除七天前备份,也就是只保存7天内的备份
find $datapath -name "*.sql" -type f -mtime +7 -exec rm -rf {} \; > /dev/null 2>&1
echo "数据库表 ${dbname} 备份成功!!" >> ${logpath}/mysqlback.log
else
#备份失败则进行以下操作
echo "数据库表 ${dbname} 备份失败!!" >> ${logpath}/mysqlback.log
fi
done
echo "完成备份"
echo "################## ${backtime} #############################"