使用mysql的master/slave部署已经有一段时间。这种架构不能从根本上弥补数据结构设计失误带来的性能问题。聊胜于无。 master/slave模式中,数据同步非常快。而master/slave/slave则会相对慢一些(就是master->slave(作为新的master)->slave,是串联的三个节点),有时候可能会有延迟,不过不严重。 最近使用起来主要是在维护上积累了一些经验。以案例说明吧。
一、需要把正式环境的数据导出一份到测试数据库 这个有几种办法,可以使用master/slave同步,也可以导出成SQL,然后再倒入。我喜欢导入/导出的方式。假设 192.168.0.10是master,192.168.0.11-14是slave。 #mysql -h192.168.0.10 -P3306 -uroot -ppassword databasename > /home/xieping/data.sql #mysql -h192.168.0.11 -P3306 -uroot -ppassword databasename < /home/xieping/data.sql 上面的命令要相对简单一些,也可以使用mysqldump命令,或者tar命令。
导出后再设置slave,参考下面的第二节。
二、需要添加一个slave 添加slave设置好my.conf就可以了。现在问题是在同步的过程中,IO部分完成了,但是SQL部分一直报错。 使用 #stop slave; #set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; #start slave; 这种操作还是报错中断。
这种情况需要查看master的状态。 #mysql -h192.168.0.10 -uroot -p
mysql> show master status\G; *************************** 1. row *************************** File: mysql-bin.000012 Position: 108822735 Binlog_Do_DB: Binlog_Ignore_DB: 1 row in set (0.00 sec)
ERROR: No query specified
通过该命令获得File和Position,在slave中有用。
#mysql -h192.168.0.11 -uroot -p mysql>stop slave; mysql>change master to >Master_Log_File = 'mysql-bin.000012', >Master_Log_Pos = '108822735'; mysql>show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.10 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000012 Read_Master_Log_Pos: 108942588 Relay_Log_File: localhost-relay-bin.000035 Relay_Log_Pos: 22317781 Relay_Master_Log_File: mysql-bin.000012 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: databasename 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: 108942317 Relay_Log_Space: 22318211 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: 5 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec)
ERROR: No query specified 现在就正常了, Seconds_Behind_Master: 5 表明slave比master略有延迟。
三、更换master 更换master如果通过修改my.conf的话比较麻烦。不但要重启mysql进程,而且还不一定成功。可以通过 mysql> stop slave; mysql> reset slave; mysql>start slave; 不过这样一来就重置了更新进度,更新会从头开始。
而是要change master to命令则可以通过命令设置,但这个命令不会影响my.conf文件,重启mysql后,设置就无效了。
|
|
来自: Frank__Library > 《Mysql》