mysql的主从复制
一 主从复制搭建
1. 准备三台主机 (这个是多实例)
3307 master
3308 salve1
3309 salve2
2. master 节点设置
[mysqld] log-bin=/data/3307/mysql-bin -- 打开二进制 binlog_format=row -- 日志格式 skip-name-resolve -- 关闭域名解析 server-id=3307
3. salve 节点设置
[mysqld] server-id= skip-name-resolve
4. 启动多实例
5. maste创建复制账户
grant replication slave on *.* to repl@'10.0.0.%' identified by '123456'; flush privileges;
6.分析master节点情况
1) 全新环境,不需要备份主库数据
直接从第一个binlog(mysql-bin.000001)的开头位置(120)
2) 主库工作有一段时间,做主从复制
①备份主库
mysqldump -uroot -p123456 -S /data/3307/mysql.sock -A -R --triggers --master-data=2 --single-transaction >/tmp/full.sql
[root@db2 ~]# sed -n '22p' /tmp/full.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=1036;
② 恢复到从库
salve节点操作
mysql -S /data/3308/mysql.sock mysql> set sql_log_bin=0; mysql> source /tmp/full.sql
7 从库开启主库同步
帮助:
help change master to
mysql -S /data/3308/mysql.sock CHANGE MASTER TO MASTER_HOST='10.0.0.87', MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_PORT=3307, MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=1036;
8 开启主从
开启IO和SQL线程
salve 节点操作
mysql> start slave;
9 查看主从状态
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.87 Master_User: repl Master_Port: 3307 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 1448 Relay_Log_File: 3308-relay-bin.000002 Relay_Log_Pos: 695 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1448 Relay_Log_Space: 867 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 3307 Master_UUID: 8737f473-d9e6-11e9-afbf-000c2967cb96 Master_Info_File: /data/3308/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 1 row in set (0.00 sec)
二 主从复制高级功能
1. 延迟从库
在其中一台从库上的操作
配置SQL_thread延迟
mysql>stop slave; mysql>CHANGE MASTER TO MASTER_DELAY = 60; mysql>start slave; mysql> show slave status \G SQL_Delay: 300
取消延迟
mysql> stop slave; mysql> CHANGE MASTER TO MASTER_DELAY = 0; mysql> start slave;
2. 复制过滤
主库方面控制(不建议使用)
白名单:只记录白名单中列出的库的二进制日志
binlog-do-db
黑名单:不记录黑名单列出的库的二进制日志
binlog-ignore-db
从库方面控制
白名单:只执行白名单中列出的库或者表的中继日志
--replicate-do-db=test
--replicate-do-table=test.t1
--replicate-wild-do-table=test.x*
黑名单:不执行黑名单中列出的库或者表的中继日志
--replicate-ignore-db
--replicate-ignore-table
--replicate-wild-ignore-table
三 主从复制新特性——GTID复制
GTID 是5.6的新特性
GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。
它的官方定义如下:
GTID = source_id :transaction_id
7E11FA47-31CA-19E1-9E56-C43AA21293967:29
每一台mysql实例中,都会有一个唯一的uuid,标识实例的唯一性
auto.cnf,存放在数据目录下
1 重要参数介绍
[mysqld] gtid-mode=on --启用gtid类型,否则就是普通的复制架构 enforce-gtid-consistency=true --强制GTID的一致性 log-slave-updates=1 --slave更新是否记入日志
2. 实验 机器准备
3. 修改my.cnf 文件
master:
[mysqld] datadir=/data/mysql/data basedir=/data/mysql socket=/data/mysql/tmp/mysql.sock port=3306 server_id=101 log-error=/var/log/mysql.log log-bin=/data/binlog/my-bin binlog_format=row autocommit=0 skip_name_resolve sync_binlog=1 gtid-mode=on enforce-gtid-consistency=true log-slave-updates=1 [client] socket=/data/mysql/tmp/mysql.sock
salve1:
[mysqld] datadir=/data/mysql/data basedir=/data/mysql socket=/data/mysql/tmp/mysql.sock port=3306 server_id=102 log-error=/var/log/mysql.log log-bin=/data/binlog/my-bin binlog_format=row autocommit=0 skip_name_resolve sync_binlog=1 gtid-mode=on enforce-gtid-consistency=true log-slave-updates=1 [client] socket=/data/mysql/tmp/mysql.sock
salve2:
[mysqld] datadir=/data/mysql/data basedir=/data/mysql socket=/data/mysql/tmp/mysql.sock port=3306 server_id=103 log-error=/var/log/mysql.log log-bin=/data/binlog/my-bin binlog_format=row autocommit=0 skip_name_resolve sync_binlog=1 gtid-mode=on enforce-gtid-consistency=true log-slave-updates=1 [client] socket=/data/mysql/tmp/mysql.sock
4. 三台服务器 都做初始化操作
/data/mysql/scripts/mysql_install_db --basedir=/data/mysql/ --datadir=/data/mysql/data --user=mysql
设置密码
查看三个节点
mysql -uroot -p123456 -e "show variables like 'server_id'"
5. 都准备完毕开始设置
master 节点操作
grant replication slave on *.* to repl@'10.0.0.%' identified by '123456';
salve1 salve2 操作
change master to master_host='10.0.0.51',master_user='repl',master_password='123456' ,MASTER_AUTO_POSITION=1; start slave;
6.测试