MySQL主从复制(维护)

批量创建主从复制脚本

####################################################
# create by lt
#
# create slave script
####################################################
MYSQL_USER=root
MYSQL_PWD=123456
MYSQL_SOCK=/data/3307/mysql.sock
MYSQL_CMD="mysql -u${MYSQL_USER} -p${MYSQL_PWD} -S ${MYSQL_SOCK}"
BACKUP_PATH=/opt/bak
BACKUP_DATE=mysql_back.sql.gz
gzip -d ${BACKUP_PATH}/${BACKUP_DATE}
${MYSQL_CMD} < ${BACKUP_PATH}/mysql_back.sql
cat |$MYSQL_CMD<< EOF
CHANGE MASTER TO
MASTER_HOST='10.0.0.12',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000019',
MASTER_LOG_POS=472;
EOF
$MYSQL_CMD -e "start slave;"
$MYSQL_CMD -e "show slave status\G" >/opt/bak/slave.log

查看线程状态

--MASTER
mysql> show processlist\G
*************************** 1. row ***************************
     Id: 1
   User: rep
   Host: 10.0.0.12:53608
     db: NULL
Command: Binlog Dump
   Time: 21617
  State: Master has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL

--SLAVE
mysql> show processlist\G
*************************** 1. row ***************************
     Id: 1
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 25664
  State: Slave has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 2. row ***************************
     Id: 2
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 25664
  State: Waiting for master to send event
   Info: NULL

读写分离

(1)从库,连接用户权限控制
(2)web应用写指向主库,读指向从库(dbproxy代理)
(3)从库read only 授权方案

1.授权(主库用户授权变更,从库失效。从库切到主需要重新授权)
主库:select 、update、insert、delete
从库:回收权限update、insert、delete

2.mysql库不同步(授权信息记录在mysql库) ,再进行授权。(从库切到主需要重新授权)
binlog-ignore-db=mysql (master)
binlog-do-db=db (master)
replication-ignore-db=mysql (slave)

3.(最佳)不收回权限从库可以设置read-only参数。(从库切到主需要修改参数)
(read-only 对super、all privileges用户无效)

read-only参数

--read-only对root用户无效(super、all privileges用户无效)
[root@mysql 3307]# vi my.cnf 
[mysqld]
read-only
[root@mysql 3307]# ./mysql restart
[root@mysql 3307]# mysql -S /data/3307/mysql.sock  -uroot -p
mysql> create table t1(id int);
Query OK, 0 rows affected (0.04 sec)
mysql> grant select,update,delete,insert on *.* to 'user_ro'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

[root@mysql 3307]# mysql -S /data/3307/mysql.sock  -uuser_ro -p
mysql> insert into t1 values(1);
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement

故障处理

(1)
stop slave;
set global sql_slave_skip_counter = 1;
start slave;

(2)
slave-skip-errors = 1032,1062,1007

从库开启bin_log日志

1、级联主从复制
2、使用从库进行备份
log-bin = /data/3307/mysql-bin
log-slave-updates
expire_logs_days = 7

主从切换

(1)查看同步状态
[root@mysql data]# cat /data/3307/data/master.info 
18
mysql-bin.000023
107

mysql> show processlist\G
*************************** 1. row ***************************
     Id: 1
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 335
  State: Slave has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 2. row ***************************
     Id: 2
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 335
  State: Waiting for master to send event
   Info: NULL

(2)停止同步
stop slave
reset master
quit

(3)删除文件
删除master-info relay-log.info文件

(4)检查授权表

(5)将从库提升为主库
开启log-bin日志
注释掉read-only 和 log-slave-updates参数
restart

(6)
如果主服务器没宕,可去主库拉取bin-log日志补全提示为主库的从库

(7)其他从库操作
stop slave;
change master to master_host ='10.0.0.10';  --可指定同步的位置点,可使用绑定VIP
start slave;
show slave status\G

(8)有计划的主从切换
主库锁表-->查看同步状态-->修改参数-->切换

使用双主模式

--vi my.cnf
auto_increment_increment = 2 --自增ID 偏移量
auto_increment_offset = 1    --自增ID 初始量
log-slave-updates
log-bin

你可能感兴趣的:(MySQL)