mysql主从服务器切换

  mysql1主服务器
mysql2 mysql-search从服务器

从服务器的--log-slave-updates是关闭的 show variables;确认是关闭的
要关闭的原因是为了防止从服务器可能会收到2次更新。
假定slave 1 --log-slave-updates 是开着的,他将把更新写到自己的二进制日志中。
当slave2换主服务器为slave1的时候,就可能收到已经接收过master的更新
it may receive updates from Slave 1  that it has already received from Master

mysql> select * from t;
+------+-------+
| i    | name  |
+------+-------+
|    2 | name2 |
|    1 | name1 |
+------+-------+
2 rows in set (0.00 sec)

现在模拟主服务器挂了/etc/rc.d/init.d/mysql1 stop



he reason for running the slave without --log-slave-updates is to prevent slaves from receiving updates twice in case you cause one of the slaves to become the new master. Suppose that Slave 1 has --log-slave-updates enabled. Then it will write updates that it receives from Master to its own binary log. When Slave 2 changes from Master to Slave 1 as its master, it may receive updates from Slave 1  that it has already received from Master


在每个从服务器上执行 stop slave io_thread;
然后show processlist;确保Has read all relay log;

mysql2设为新的主服务器。

stop slave;
reset master;reset master清空了master的binlog以后创建了新的binlog

从服务器mysqlsearch重设主服务器。

stop slave;

CHANGE MASTER TO
MASTER_HOST='localhost',
MASTER_USER='repl',
MASTER_PASSWORD='123',
MASTER_PORT=3308;
这里不需要设定日志的位置,因为主服务器清空了日志创建新的binlog

最后mysqlsearch 启动slave: start slave;

mysql2> insert into t values(3,'name3');
Query OK, 1 row affected (0.00 sec)

mysqlsearch> select * from t;
+------+-------+
| i    | name  |
+------+-------+
|    3 | name3 |
|    2 | name2 |
|    1 | name1 |
+------+-------+
3 rows in set (0.00 sec)

当mysql1恢复的时候,可以让mysql1成为mysql2的从服务器,这样可以得到mysql1挂了以后的mysql2更新
/etc/rc.d/init.d/mysql1 start

CHANGE MASTER TO
MASTER_HOST='localhost',
MASTER_USER='repl',
MASTER_PASSWORD='123',
MASTER_PORT=3308;


mysql1> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql1> select * from t;
+------+-------+
| i    | name  |
+------+-------+
|    3 | name3 |
|    2 | name2 |
|    1 | name1 |
+------+-------+
3 rows in set (0.00 sec)

现在mysql2是主服务器mysql1,mysqlsearch是从服务器。
假如想设回mysql1为主服务器。

这个时候应该把mysql2的服务器设为不能更新,因为他要变回从服务器。
stop slave io_thread;
确保所有服务器Has read all relay log;

mysql1> stop slave;
mysql1> reset master;

mysql2,mysqlsearch执行

stop slave;

CHANGE MASTER TO
MASTER_HOST='localhost',
MASTER_USER='repl',
MASTER_PASSWORD='123',
MASTER_PORT=3307;

最后启动slave: start slave;

show processlist;

mysql> insert into t values(4,'name4');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t;
+------+-------+
| i    | name  |
+------+-------+
|    4 | name4 |
|    3 | name3 |
|    2 | name2 |
|    1 | name1 |
+------+-------+
4 rows in set (0.00 sec)

可以了

总结:切换主从服务器的条件:从服务器的--log-slave-updates关闭,log-bin 开着
注意的问题:stop slave io_thread;确保Has read all relay log;
从服务器change master 之前主服务器reset master;

你可能感兴趣的:(mysql,主从服务器切换)