高可用方案之多级复制

高可用方案之多级复制,第1张

MySQL高可用方案之多级复制

在大多数场景下,我们使用MySQL主从复制来完成数据库的冗余。这里,我们使用多级复制来解决数据库的常见故障。数据库有A、B、C服务器。正常情况下,A是主导服务器,B是A的从服务器,C是B的从服务器。

a->;b->;C

当A出现问题时,设置B为领导,C为B的奴隶,一切正常后A将成为C的奴隶。

b->;c->;A

当B出现问题时,C占优,A是C的奴隶,B是A的奴隶,可以很快解决问题。


persona IP IP地址 数据库版本号 大师 192.168.2.241 DB1 5.6.29
备份 192.168.2.242 DB2 5.6.29 备用 192.168.2.243 DB3 5.6.29

注意在那种场景下数据库的版本号一定要一致,否则会因为版本号之间的兼容性而出现问题。


  • 创建一个副本帐户

  • 配备数据库设备

    备份主数据库的数据,然后将其导入备份数据库。

    配备主从关系



    1.建立一个副本帐户。

    mysql>grant repication slave on *.* to 'repl'@'192.168.2.%' identified by 'repl'; mysql>flush privileges;


    2.打开数据库binlog,设置server-id并打开log_slave_updates。

    它显示log_slave_updates将从服务器从主服务器收到的升级记录到从服务器本身的二进制日志文档中。

    如果log_slave_updates没有打开,它将在a->:B->;在场景C中,C将无法从b获取数据信息。

    在MySQL环境变量/etc/my.cnf中的[mysqld]下添加下面这句话

    log-bin=mysqlbin server-id=241      #这儿每台服务器都务必不一致,最好IP的尾端 log_slave_updates=1 expire_logs_days=7

    记得重新启动数据库。


    3.备份主数据库的数据,然后将其导入备份数据库。


    mysql> flush tables with read lock; mysql> show master status; ---------------------------------------------------------------------------- | File       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | ---------------------------------------------------------------------------- | binlog.000002 |    409 |         |             |             | ---------------------------------------------------------------------------- 1 row in set (0.00 sec)


    注意:这里不能退出mysql命令关联。此外,打开另一个对话框来导出数据库。如果退出对话框,表格锁定将自动消除。

    [root@db1 ~]# mysqldump -uroot -p --all-database --add-drop-table >all_database.sql

    引导从上面导出的all_database.sql到其他db2和db3。

    [root@db2 ~]# mysql -uroot -p <all_database.sql [root@db3 ~]# mysql -uroot -p <all_database.sql

    4.打开主从复制。

    在db2上:

    mysql> change master to master_host='192.168.2.241',master_user='repl',master_password='repl',master_log_file='binlog.000002',master_log_pos=409; mysql> start slave; mysql> show master status; ---------------------------------------------------------------------------- | File     | Position| Binlog_Do_DB| Binlog_Ignore_DB| Executed_Gtid_Set| ---------------------------------------------------------------------------- | binlog.000002| 647569 |       |         |          | ----------------------------------------------------------------------------


    在db3上:

    mysql> change master to master_host='192.168.2.242',master_user='repl',master_password='repl',master_log_file='binlog.000002',master_log_pos=647569; mysql> start slave;

    然后,分别在db2和db3上实现showslavestatus\G;查询有什么问题吗?

    mysql> show slave status\G; *************************** 1. row ***************************                Slave_IO_State: Waiting for master to send event                   Master_Host: 192.168.2.242                   Master_User: repl                   Master_Port: 3306                 Connect_Retry: 60               Master_Log_File: binlog.000002           Read_Master_Log_Pos: 647569                Relay_Log_File: db3-relay-bin.000002                 Relay_Log_Pos: 280         Relay_Master_Log_File: binlog.000002              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: 647569               Relay_Log_Space: 451               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: 242                   Master_UUID: 25a2315a-d9f0-11e5-9aa9-000c296e3855              Master_Info_File: /var/lib/mysql/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)

    能够看见

    Slave_IO_Running:是

    Slave_SQL_Running:是

    是的,说明一切正常,可以复印。


    然后测试它:

    将数据信息插入数据库,然后在db1、db2和db3上查看。


    如有疑问,请出示奴隶身份\G;查询不正确吗?

    如果您遇到不正确的1062,您可以忽略它,并且您可以立即

    mysql> stop slave; mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; mysql> start slave;



    运行一段时间后,db1出现问题,导致无法修复的常见故障,所以db2上只能实现stopslave

    然后,db1修复后,从db3导出数据并记录点,然后将母版更改为db3。


    为了更好地避免在从属数据库中意外加载,还可以将read_only=1
    添加到从属数据库的环境变量中。





    欢迎分享,转载请注明来源:内存溢出

    原文地址:https://54852.com/zz/778851.html

    (0)
    打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
    上一篇 2022-05-03
    下一篇2022-05-03

    发表评论

    登录后才能评论

    评论列表(0条)

      保存