一、主主优点
双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步。
对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致。这样做的好处:
(1)可以做灾备,其中一个坏了可以切换到另一个。
(2)可以做负载均衡,可以将请求分摊到其中任何一台上,提高网站吞吐量。
二、环境
系统环境:linux
Mysql版本:5.6.24
Master1IP:192.168.0.1
Master2IP:192.168.0.2
三、操作步骤
(1)在master1上创建专门备份的用户
【master1】
mysql –uroot -p grant replication slave on *.* to ‘repl_user‘@‘192.168.0.2‘identified by ‘repl_user‘; flush privileges;
(2)开启主服务器的binarylog
【master1】
vi /etc/my.cnf server_id = 1 log-bin=master-bin binlog_format=mixed read-only=0 binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-ignore-db=performance_schema auto-increment-increment=5 auto-increment-offset=1
(3)获取主服务器的状态和同步状态
先锁定数据库: flush tables with read lock; 导出数据: mysqldump -root -p test>/tmp/test_0511.sql 查看master1的binary日志位置: show master status\G; *************************** 1. row*************************** File: master-bin.000009 Position: 6001 Binlog_Do_DB: Binlog_Ignore_DB:mysql,information_schema,performance_schema Executed_Gtid_Set: 1 row in set (0.00 sec) ERROR: No query specified 解除锁定: unlock tables;
(4)设置master2需要复制的数据库
【master2】
vi /etc/my.cnf server_id = 2 log-bin = master-bin binlog_format = mixed replicate-ignore-db = mysql replicate-ignore-db = information_schema replicate-ignore-db = performance_schema relay_log=mysqld-relay-bin log-slave-updates = ON /etc/init.d/mysqld restart
(5)导入初态, 开始同步。
create database test default charset utf8; use test source /tmp/test_0511.sql CHANGE MASTER TO MASTER_HOST=‘192.168.0.1‘, MASTER_USER=‘repl_user‘, MASTER_PASSWORD=‘repl_user‘, MASTER_LOG_FILE=‘master-bin.000009‘, MASTER_LOG_POS=6001; start slave; show slave status\G;
注意图中的红框,两个都是Yes,说明开启成功。
(6)在master2上创建专门备份的用户
【master2】
mysql –uroot -p grant replication slave on *.* to ‘repl_user‘@‘192.168.0.1‘identified by ‘repl_user‘; flush privileges;
(7)开启master2主服务器的binarylog
vi /etc/my.cnf read-only=0 binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-ignore-db=performance_schema auto-increment-increment=10 auto-increment-offset=2 /etc/init.d/mysqld restart
(8) 开启主服务器的binarylog
Master2日志状态:
show master status\G; *************************** 1. row*************************** File: master-bin.000002 Position: 120 Binlog_Do_DB: Binlog_Ignore_DB:mysql,information_schema,performance_schema Executed_Gtid_Set: 1 row in set (0.00 sec)
(9)修改master1配置文件
【maste1】开启中继
vi /etc/my.cnf replicate-ignore-db = mysql replicate-ignore-db = information_schema replicate-ignore-db = performance_schema relay_log=mysqld-relay-bin log-slave-updates = ON /etc/init.d/mysqld restart
(10)启动同步
CHANGE MASTER TO MASTER_HOST=‘192.168.0.2‘, MASTER_USER=‘repl_user‘, MASTER_PASSWORD=‘repl_user‘, MASTER_LOG_FILE=‘master-bin.000002‘, MASTER_LOG_POS=120; start slave; show slave status\G;
出现错误: [ERROR] Slave SQL: Slave failed toinitialize relay log info structure from the repository, Error_code: 1872 解决方法: reset slave;
时间: 2024-12-14 23:36:07