简介:
MySQL主从复制也称主从同步,他是构建数据库高可用集群架构的基础,将一台主机的数据复制到多台主机,并重新应用日志中的SQL语句实现复制功能
MySQL支持单向,双向,链式级联,异步复制
5.5版本新增半同步复制 5.6新增GTID复制 5.7新增多源复制/并行复制/loss-less复制,复制过程中,一台服务器充当主库,另一个充当从库
常见主从模式:
1) master------------->slave 单向主从
2) master <-----------> master 双向主从
3) master -------->slave1------>slave2 级联主从
4) master ------->slave 一主多从
|--------------->slave2
5)master------------>slave 多主一从
master--------------|^
主从复制原理:
主库 :I/O-dump-thread
从库:I/O-thread SQL-thread
从库的I/O-thread向主库请求binlog文件,主库I/O-dump-thread响应写入从库的relaylog文件
从库的SQL-thread从relaylog文件中读取语句,转成SQL语句,写入从库
主从复制的重点参数
log-bin 搭建主从复制,必须开启二进制日志
server-id :主从服务器的标识,主从服务器不能一致
server-uuid : mysql5.6 新增 ,数据库启动自动生成,保存在数据目录下的auto.cnf中
read_only: 设置从库为只读状态 super_read_only: 所有账号包括管理员都会禁止写
binlog_format: 必须为row
log_slave_updates: 获取主服务器上的数据变更信息,写到从服务器的二进制日志中
binlog_error_action: 5.7新增 =ABORT_SERVER 不能写binlog时,mysql会退出(默认)
=IGNORE_ERROR 不能写入binlog时,关闭binlog功能,会导致主从出现数据库数据不一致的情况
binlog-do-db: (主库) 选择复制哪个数据库
binlog-ignore-db: 不复制哪个数据库
gtid_mode:决定是否开启gtid模式
enforce-gtid-consistency: 开启gtid模式,该参数也开启
gtid_next: session级别的变量,下一个gtid,默认automatic
gtid_purged:丢掉的gtid
relay_log:记录从库的IO thread从主库读取来的binlog
replicate_do_table:(从库) 复制指定表
replicate_ignore_table:(从库) 不复制指定表
replicate_do_db:(从库)只复制指定库
replicate_ignore_db:(从库) 不复制指定库
replicate-wild-do-table:使用通配符复制指定表
replicate-wild-ignore-table:使用通配符不复制指定表
master-info-repository:=table 记录主从状态&配置
relay_log_info_repository:=table 记录斌binlog应用位置记录到relay.info中
relay_log_recovery:(从库)(默认关闭)(建议开启=1)
relay_log_purge:(从库)(建议开启)清除已执行的relaylog
slave_net_timeout :设置网络超时时间点
并行复制参数:
slave_parallel_type='LOGICAL_CLOCK'
slave_parallel_workers = X 设置X线程执行relaylog中向主库提交的事务
部署:
主MySQL部署
1)安装MySQL
MYSQL_TAR=mysql tar包
tar zxvf $MYSQL_TAR -C /opt/
MYSQL=`echo $MYSQL_TAR | sed -r "s/.tar[.a-zA-Z0-9]+//g"`
mv /opt/${MYSQL} /opt/mysql
ln -s /opt/mysql /usr/local/mysql
mkdir -p /opt/mysql/data
groupadd mysql
useradd -r -g mysql mysql
rpm -qa | grep libaio > /dev/null
2)修改 /etc/my.cnf
cat > /etc/my.cnf << EOF
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/usr/local/mysql/data/mysql_error
pid-file=/usr/local/mysql/data/`hostname`.pid
!includedir /etc/my.cnf.d
EOF
3)环境修改
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /opt/mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod 777 /etc/init.d/mysql
echo "export PATH=$PATH:/opt/mysql/bin" >> /etc/profile
source /etc/profile
4)重设密码
service mysql stop 2>&1 > /dev/null
/opt/mysql/bin/mysqld_safe --user=mysql --skip-grant-tables > /opt/log &
sleep 3s
/opt/mysql/bin/mysql -u root << EOF
use mysql;
update user set authentication_string=password('123456') where user='root';
update user set password_expired='N' where user='root';
flush privileges;
EOF
#service mysql stop
pgrep mysqld | xargs kill -9
5)主修改
cat > /etc/my.cnf << EOF
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
server_id=101
log_bin=/usr/local/mysql/mysql-bin
[mysqld_safe]
log-error=/usr/local/mysql/data/mysql_error
pid-file=/usr/local/mysql/data/`hostname`.pid
!includedir /etc/my.cnf.d
EOF
6)自动登陆设置&创建同步用户
service mysql stop
echo '[client]' >> /etc/my.cnf
echo 'host=localhost' >> /etc/my.cnf
echo "user=$MASTER_mysql_user" >> /etc/my.cnf
echo "password=$MASTER_mysql_pass" >> /etc/my.cnf
service mysql start
sleep 10s
mysql << EOF
grant replication slave ,replication client on *.* to user@'%' identified by '123456';
flush privileges;
EOF
7)防火墙设置
MySQL的PORT=3306
iptables -A INPUT -p tcp --dport $PORT -j ACCEPT
iptables -A OUTPUT -p tcp --dport $PORT -j ACCEPT
################setenforce
setsebool -P mysql_connect_any on
setsebool -P selinuxuser_mysql_connect_enabled on
8)检查主状态
mysql <<EOF > conf_master
show master status;
EOF
从MySQL部署
1)MySQL安装
同上
2)从 /etc/my.cnf修改
cat > /etc/my.cnf << EOF
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
server_id=102
log_bin=/usr/local/mysql/mysql-bin.log
relay-log=/usr/local/mysql/mysql-relay-bin.log
#read_only=1
[mysqld_safe]
log-error=/usr/local/mysql/data/mysql_error
pid-file=/usr/local/mysql/data/`hostname`.pid
!includedir /etc/my.cnf.d
EOF
3)修改环境
同上
4)修改MySQL密码
同上
5)设置自动登陆
同上
6)防火墙设置
同上
7)从MySQL设置
MASTER_LOG=`cat /opt/conf_master | grep mysql | cut -d ' ' -f1`
MASTER_POS=`cat /opt/conf_master | grep mysql | cut -d ' ' -f2`
conf_master 是主MySQL检查状态传过来的文件
mysql << EOF
change master to master_host='$MASTER_IP',master_user='user',master_password='123456',master_log_file='${MASTER_LOG}',master_log_pos=${MASTER_POS};
flush privileges;
start slave;
EOF
8)检查从MySQL状态
mysql <<EOF > conf_slave
show slave statusG;
EOF