mysql的双机热备自动切换_mysql的双机热备

本文详细介绍了MySQL(MariasDB)的双机热备配置过程,包括创建用户授权、开启binlog、设置服务器ID、同步配置以及状态检查,确保数据实时同步,并讨论了实现双向同步的方法。
摘要由CSDN通过智能技术生成

mysql的双机热备

双机热备,就是要保持两个数据库的状态自动同步。对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致。

这样的做法好处在于:

1、可以做灾备,其中一个坏了可以切换到另一个;

2、可以做负载均衡,可以请求分摊到其中任何一台上,提高网站吞吐量。

这里我使用的是mariadb

[root@nginx2 ~]# mysql -V

mysql  Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1

1、在数据库A上创建用户并授权登录

grant replication slave on *.* to 'rep'@'172.25.254.131' identified by '123456';

2、开启A服务器的binarylog

vim /etc/my.cnf

log-bin = mysql-bin         #开启mysql的binlog日志功能

binlog_format = mixed    #binlog日志格式

server-id = 1                    #服务器id号

read-only=0                    #关闭只读,可以读写

binlog-do-db=laravel       #指定对db_nameA记录二进制日志

binlog-ignore-db=mysql  #指定不对db_nameB记录二进制日志

#relay_log=mysql-relay-bin#开启relay-log日志,relay-log日志记录的是从服务器I/O线程将主服务器的二进制日志读取过来记录到从服务器本地文件,然后SQL线程会读取relay-log日志的内容并应用到从服务器

3、登录到B服务器上,开启中继

vim /etc/my.cnf

log-bin = mysql-bin         #开启mysql的binlog日志功能

binlog_format = mixed    #binlog日志格式

server-id = 1                    #服务器id号

read-only=0                     #关闭只读,可以读写

binlog-do-db=laravel       #指定对db_nameA记录二进制日志

binlog-ignore-db=mysql  #指定不对db_nameB记录二进制日志

relay_log=mysql-relay-bin#开启relay-log日志,relay-log日志记录的是从服务器I/O线程将主服务器的二进制日志读取过来记录到从服务器本地文件,然后SQL线程会读取relay-log日志的内容并应用到从服务器

4、在B服务器上开启同步

CHANGE MASTER TO MASTER_HOST='172.25.254.134', MASTER_USER='rep', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=610;

5、重启B服务器数据库

systemctl restart mariadb

6、检查同步的状态

MariaDB [(none)]> show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 172.25.254.134

Master_User: rep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000006

Read_Master_Log_Pos: 610

Relay_Log_File: mariadb-relay-bin.000003

Relay_Log_Pos: 529

Relay_Master_Log_File: mysql-bin.000006

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: 610

Relay_Log_Space: 825

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: 2

7、上述,这样的只能实现服务器A改变同步到服务器B,是单向的。

要将服务器B的改变反回来也可以同步到服务器A,只需将以上操作再来一次即可!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值