mysql高可用配置(一)

耀灵1年前技术文章533

一、简介

MySQL使用双向半同步复制模式,通过开源的keepalived实现自动切换,应用通过vip连接数据库。配合自定义脚本,实现故障安全切换,切换过程对应用透明。

二、部署主从

2.1、在主备节点部署:

[root@hzyg-ip-bd32 mnt]# tar xf percona-xtrabackup-2.4.20-Linux-x86_64.el7.tar.gz

##主节点上传包并解压

2.2、备份主库数据:

安装的xtrabackup,bin路径下执行该命令,其中defaults-file参数填写主库的my.cnf文件路径,user参数填写登录数据库用户名,password是登录数据库用户的密码,/mnt/data/backup表示将备份的数据存放在什么路径下。

[root@hzyg-ip-bd32 bin]# ./innobackupex --defaults-file=/etc/my.cnf --user=root --password=h7fXy0%z#Ci2 --no-timestamp /mnt/data/backup/

查看位点及所在binlog文件

[root@hzyg-ip-bd32 backup]# cat xtrabackup_binlog_info 
binlog.000002   6975146
[root@hzyg-ip-bd32 backup]#

2.3、应用备份期间产生的日志

此步骤在目标点和源端执行均可,还是在bin路径下执行,执行完毕后,将/mnt/data/backup/拷贝到从库

./innobackupex --apply-log /mnt/data/backup/

2.4、停止从数据库

[root@hzyg-ip-bd33 ~]# systemctl stop mysqld

确认全备已应用日志(主要看backup_type)

[root@hzyg-ip-bd33 backup]# cat xtrabackup_checkpoints 
backup_type = full-prepared
from_lsn = 0
to_lsn = 2235041313
last_lsn = 2235041322
compact = 0
recover_binlog_info = 0
flushed_lsn = 2235041322
[root@hzyg-ip-bd33 backup]#

查看位点

[root@hzyg-ip-bd33 backup]# cat xtrabackup_info 
uuid = b82a5192-9ea3-11ec-bb9a-6805cad60b78
name = 
tool_name = innobackupex
tool_command = --defaults-file=/etc/my.cnf --user=root --password=... --no-timestamp /mnt/data/backup/
tool_version = 2.4.20
ibbackup_version = 2.4.20
server_version = 5.7.35-log
start_time = 2022-03-08 13:50:52
end_time = 2022-03-08 13:50:54
lock_time = 0
binlog_pos = filename 'binlog.000002', position '6975146'
innodb_from_lsn = 0
innodb_to_lsn = 2235041313
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N
[root@hzyg-ip-bd33 backup]#

2.5、全量数据恢复到从库

[root@hzyg-ip-bd33 backup]# ./innobackupex --defaults-file=/etc/my.cnf --copy-back /mnt/data/backup/

备注:原来的data目录可以移走,数据恢复以后检查下/data目录下文件的属主属组权限都为mysql,不然mysql启动报错

2.6、启动备库

[root@hzyg-ip-bd33 backup]# systemctl start mysqld

2.7、配置双向半同步复制

由于我们未开启GTID,根据上文中查看的binlog文件、位点进行同步 ,其中MASTER_LOG_FILE参数填写binlog文件名,MASTER_LOG_POS参数填写具体位点。

创建复制账号

grant replication slave,replication client on *.* to repuser@'%' identified by 'repuser';

备库执行,建立复制关系:

change master to master_host='hzyg-ip-bd32',master_port=3306,master_user='repuser',master_password='repuser',master_auto_position=1;
CHANGE MASTER TO
MASTER_HOST='hzyg-ip-bd32',
MASTER_PORT=3306,
MASTER_USER='repuser',
MASTER_PASSWORD='repuser',
MASTER_LOG_FILE='binlog.000002',
MASTER_LOG_POS=6795146;

主库也要执行,建立复制关系,开启双向复制:

change master to master_host='hzyg-ip-bd33',master_port=3306,master_user='repuser',master_password='repuser',master_auto_position=0;

开启主从;

start slave

2.8、检查主从同步

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: hzyg-ip-bd32
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000005
          Read_Master_Log_Pos: 1743713
               Relay_Log_File: hzyg-ip-bd33-relay-bin.000015
                Relay_Log_Pos: 1740851
        Relay_Master_Log_File: binlog.000005
             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: 1743713
              Relay_Log_Space: 1741065
              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: 323306
                  Master_UUID: d68b624b-6eae-11ec-8671-6805cad60b78
             Master_Info_File: /data1/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql>

备注:

slave_io_running和slave_sql_running显示yes,seconds_behind_master为0时,表示主从已经恢复


相关文章

Pod终止-preStop

由于 Pod 所代表的是在集群中节点上运行的进程,当不再需要这些进程时允许其体面地 终止一般不应武断地使用 KILL 信号终止它们设计的目标是令你能够请求删除进程,并且知道进程何时被终止,同时也能够确...

Linux解锁线程基本概念和线程控制,步入多线程学习的大门(1)

Linux解锁线程基本概念和线程控制,步入多线程学习的大门(1)

1、线程初识1.1线程的概念线程是进程内部的一个执行分支,线程是CPU调度的基本单位那什么是进程呢?我们之前学习了解到的进程是加载到内存中的程序进程 = 内核数据结构 + 进程代码和数据。今天我们要推...

大数据自动化巡检系统(一)

大数据自动化巡检系统(一)

一、产品简介EasyCare大数据自动化巡检平台是袋鼠云自研的一款大数据集群自动化巡检调优运维平台,支持针对各类大数据组件、服务接口、其他组件等可用性、稳定性进行自动巡检,实现主动实时监控,准确定位问...

Linux命令traceroute—追踪网络路由利器

说明:通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径。当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能...

fio磁盘io压测

fio磁盘io压测

fio tar包地址:https://brick.kernel.dk/snaps/yum 安装gcc、fio使用到的ibaio-devel 引擎yum install -y gcc yum inst...

DBMS_SESSION包跟踪10046

注意:DBMS_SESSION包:只能跟踪当前会话,不能指定会话DBMS_SESSION.SET_SQL_TRACE=ALTER SESSION SET SQL_TRACE; =ALTER SESSI...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。