基于xtrabackup的主从同步

作者

刘畅

时间

2020-9-21

服务器版本:CentOS Linux release 7.5.1804

主机名

ip地址

服务器配置

安装软件

密码

mysql01

172.16.1.151

2核/2G/60G

01
mysql5.7.31(二进制版),主节点。

02 percona-xtrabackup

mysql5.7@2021

mysql02

172.16.1.152

2核/2G/60G

01 mysql5.7.31(二进制版),未初始化的空库,从节点。

02 percona-xtrabackup

目录

1 基于gtid方式增加从库 1

1.1 前提条件 1

1.2 主库操作(172.16.1.151) 4

1.3 从库操作(172.16.1.152) 5

1.4 补充 7

2 基于传统二进制增加从库 8

2.1 前提条件 8

2.2 主库操作(172.16.1.151) 11

2.3 从库操作(172.16.1.152) 12

 

1 基于gtid方式增加从库

1.1
前提条件

1 主库(172.16.1.151)my.cnf配置文件

# cat /etc/my.cnf

[client]

port = 3306

default-character-set = utf8

socket =
/usr/local/mysql/data/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

user = mysql

port = 3306

basedir = /usr/local/mysql

datadir = /usr/local/mysql/data

socket =
/usr/local/mysql/data/mysql.sock

bind-address = 0.0.0.0

pid-file =
/usr/local/mysql/data/mysqld.pid

character-set-server = utf8

collation-server = utf8_general_ci

log-error =
/usr/local/mysql/data/mysqld.log

slow_query_log = ON

long_query_time = 2

slow_query_log_file =
/usr/local/mysql/data/mysql-slow.log

max_connections = 10240

open_files_limit = 65535

innodb_buffer_pool_size = 1G

innodb_flush_log_at_trx_commit = 2

innodb_log_file_size = 256M

transaction_isolation =
READ-COMMITTE

default-storage-engine = innodb

innodb_file_per_table = on

symbolic-links = 0

explicit_defaults_for_timestamp = 1

skip-name-resolve

lower_case_table_names = 1

server-id = 1

log-bin =
/usr/local/mysql/data/mysql-bin

binlog_cache_size = 4M

binlog_format = mixed

max_binlog_cache_size = 8M

max_binlog_size = 512M

expire_logs_days = 14

gtid_mode = ON

enforce_gtid_consistency = ON

[mysqldump]

quick

max_allowed_packet = 32M

创建mysql用户

# groupadd -g 1600 mysql

# useradd -g mysql -u 1600 -M -s /sbin/nologin
mysql

# id mysql

uid=1600(mysql) gid=1600(mysql) 组=1600(mysql)

初始化mysql数据库

# /usr/local/mysql/bin/mysqld --initialize --user=mysql
--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

修改数据库密码

# grep 'temporary password'
/usr/local/mysql/data/mysqld.log

2020-09-03T15:36:48.970741Z 1 [Note] A temporary
password is generated for root@localhost: Kf;Aj:zNk7&3

# mysql -uroot -p'Kf;Aj:zNk7&3'

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY
'mysql5.7@2021';

mysql> exit;

创建复制账户

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'
IDENTIFIED BY 'repl@2020';

2 从库(172.16.1.152)my.cnf配置文件

# cat /etc/my.cnf

[client]

port = 3306

default-character-set = utf8

socket =
/usr/local/mysql/data/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

user = mysql

port = 3306

basedir = /usr/local/mysql

datadir = /usr/local/mysql/data

socket =
/usr/local/mysql/data/mysql.sock

bind-address = 0.0.0.0

pid-file =
/usr/local/mysql/data/mysqld.pid

character-set-server = utf8

collation-server = utf8_general_ci

log-error =
/usr/local/mysql/data/mysqld.log

slow_query_log = ON

long_query_time = 2

slow_query_log_file =
/usr/local/mysql/data/mysql-slow.log

max_connections = 10240

open_files_limit = 65535

innodb_buffer_pool_size = 1G

innodb_flush_log_at_trx_commit = 2

innodb_log_file_size = 256M

transaction_isolation =
READ-COMMITTE

default-storage-engine = innodb

innodb_file_per_table = on

symbolic-links = 0

explicit_defaults_for_timestamp = 1

skip-name-resolve

lower_case_table_names = 1

server-id = 2

slave-skip-errors=1007,1022,1050,1062,1169

relay-log=/usr/local/mysql/data/relay-log

max_relay_log_size=512M

relay-log-purge=ON

read-only

gtid_mode = ON

enforce_gtid_consistency = ON

[mysqldump]

quick

max_allowed_packet = 32M

创建mysql
用户

# groupadd -g 1600 mysql

# useradd -g mysql -u 1600 -M -s /sbin/nologin
mysql

# id mysql

uid=1600(mysql) gid=1600(mysql) 组=1600(mysql)

3 mysqld.service

# cat /usr/lib/systemd/system/mysqld.service

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

Type=forking

PIDFile=/usr/local/mysql/data/mysqld.pid

TimeoutSec=0

PermissionsStartOnly=true

#
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd

ExecStart=/usr/local/mysql/bin/mysqld
--defaults-file=/etc/my.cnf --daemonize
--pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS

EnvironmentFile=-/etc/sysconfig/mysql

LimitNOFILE = 65535

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

1.2
主库操作(172.16.1.151)

1 登录主库创建库、表、数据

mysql> create database test;

mysql> use test;

mysql> create table user (name
varchar(50));

mysql> insert into user(name)
values("zhangsan");

mysql> show global variables like
"%gtid%";

2 使用xtrabackup在线备份主库

# mkdir -p /mysql_backup/

# innobackupex -uroot -p"mysql5.7@2021" --stream=tar
/mysql_backup/ 2>/dev/null |gzip >/mysql_backup/`date
+%F_%H-%M-%S`.tar.gz

# du -sh
/mysql_backup/2020-10-21_14-07-57.tar.gz

580K /mysql_backup/2020-10-21_14-07-57.tar.gz

3 将备份的主库发送到从库

# scp -rp /mysql_backup/2020-10-21_14-07-57.tar.gz
root@172.16.1.152:/tmp/

1.3
从库操作(172.16.1.152)

1 解压主库备份

# mkdir -p /tmp/mysql_data/

# cd /tmp/mysql_data/

# tar -xzf
/tmp/2020-10-21_14-07-57.tar.gz

# ls -l

2 回滚事务日志

# innobackupex --apply-log --redo-only
/tmp/mysql_data/

3 完全还原备份库

# innobackupex --defaults-file=/etc/my.cnf --copy-back
/tmp/mysql_data/

# ls -l --full-time
/usr/local/mysql/data/

4 查看主库的binlog_pos点

# cat
/usr/local/mysql/data/xtrabackup_info

binlog_pos = filename 'mysql-bin.000003', position

'750', GTID of the last change
'994634a3-f6fc-11ea-a9d8-000c290fbdd4:1-5'

5 启动从数据库

# chown -R mysql.mysql
/usr/local/mysql/

# systemctl start mysqld

# cat /usr/local/mysql/data/auto.cnf

[auto]

server-uuid=6651dc51-1366-11eb-a67e-000c29c5b550

6 设置已执行过的gtid

# mysql -uroot -p'mysql5.7@2021'

mysql> reset slave;

# 清空从节点信息

mysql> reset master;

# 清空mysql.gtid_executed表信息

mysql> SET GLOBAL
GTID_PURGED='994634a3-f6fc-11ea-a9d8-000c290fbdd4:1-5';

# 设置已执行过的gtid

mysql> show global variables like
"%gtid%";

mysql> select * from
mysql.gtid_executed;

7 配置连接主库信息

# mysql> CHANGE MASTER TO

MASTER_HOST='172.16.1.151',

MASTER_PORT=3306,

MASTER_USER='repl',

MASTER_PASSWORD='repl@2020',

MASTER_AUTO_POSITION=1;

8 启动主从复制

mysql> start slave;

mysql> show slave status\G;

1.4
补充

1 mysql.gtid_executed表

GTID持久化的介质,MySQL启动阶段会读取这个表来获取gtid_executed变量的值。mysql.gtid_executed表修改时机为在BINLOG发生切换的时候保存,直到上一个BINLOG文件

执行过的全部GTID,它不是实时更新的。

2 gtid_executed变量

MySQL数据库已经执行了哪些GTID事务,处于内存中。show slave status中的executed_gtid_set也取自这里。

3 gtid_purged变量

由于BINLOG文件的删除(如purge binary logfiles或者超过expire_logs_days设置)已经丢失的GTID事务,同时在搭建备库的我们使用set global
gtid_purged变量来提示MySQL哪些GTID事务我已经执行过了。

4 通过percona-xtrabackup备份库,备份的是数据库及库中的表文件,其它文件不会备份(例如:主库的binlog文件,从库的relay-log、master.info文件),当启动数据库时根据my.cnf文件再生成相应的文件。

2 基于传统二进制增加从库

2.1 前提条件

1 主库(172.16.1.151)my.cnf配置文件

# cat /etc/my.cnf

[client]

port = 3306

default-character-set = utf8

socket =
/usr/local/mysql/data/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

user = mysql

port = 3306

basedir = /usr/local/mysql

datadir = /usr/local/mysql/data

socket =
/usr/local/mysql/data/mysql.sock

bind-address = 0.0.0.0

pid-file =
/usr/local/mysql/data/mysqld.pid

character-set-server = utf8

collation-server = utf8_general_ci

log-error =
/usr/local/mysql/data/mysqld.log

slow_query_log = ON

long_query_time = 2

slow_query_log_file =
/usr/local/mysql/data/mysql-slow.log

max_connections = 10240

open_files_limit = 65535

innodb_buffer_pool_size = 1G

innodb_flush_log_at_trx_commit = 2

innodb_log_file_size = 256M

transaction_isolation =
READ-COMMITTE

default-storage-engine = innodb

innodb_file_per_table = on

symbolic-links = 0

explicit_defaults_for_timestamp = 1

skip-name-resolve

lower_case_table_names = 1

server-id = 1

log-bin =
/usr/local/mysql/data/mysql-bin

binlog_cache_size = 4M

binlog_format = mixed

max_binlog_cache_size = 8M

max_binlog_size = 512M

expire_logs_days = 14

[mysqldump]

quick

max_allowed_packet = 32M

创建mysql用户

# groupadd -g 1600 mysql

# useradd -g mysql -u 1600 -M -s /sbin/nologin
mysql

# id mysql

uid=1600(mysql) gid=1600(mysql) 组=1600(mysql)

初始化mysql数据库

# /usr/local/mysql/bin/mysqld --initialize --user=mysql
--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

修改数据库密码

# grep 'temporary password'
/usr/local/mysql/data/mysqld.log

2020-09-03T15:36:48.970741Z 1 [Note] A temporary
password is generated for root@localhost: Kf;Aj:zNk7&3

# mysql -uroot -p'Kf;Aj:zNk7&3'

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY
'mysql5.7@2021';

mysql> exit;

创建复制账户

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'
IDENTIFIED BY 'repl@2020';

mysql> flush privileges;

2 从库(172.16.1.152)my.cnf配置文件

# cat /etc/my.cnf

[client]

port = 3306

default-character-set = utf8

socket =
/usr/local/mysql/data/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

user = mysql

port = 3306

basedir = /usr/local/mysql

datadir = /usr/local/mysql/data

socket =
/usr/local/mysql/data/mysql.sock

bind-address = 0.0.0.0

pid-file =
/usr/local/mysql/data/mysqld.pid

character-set-server = utf8

collation-server = utf8_general_ci

log-error =
/usr/local/mysql/data/mysqld.log

slow_query_log = ON

long_query_time = 2

slow_query_log_file =
/usr/local/mysql/data/mysql-slow.log

max_connections = 10240

open_files_limit = 65535

innodb_buffer_pool_size = 1G

innodb_flush_log_at_trx_commit = 2

innodb_log_file_size = 256M

transaction_isolation =
READ-COMMITTE

default-storage-engine = innodb

innodb_file_per_table = on

symbolic-links = 0

explicit_defaults_for_timestamp = 1

skip-name-resolve

lower_case_table_names = 1

server-id = 2

slave-skip-errors=1007,1022,1050,1062,1169

relay-log=/usr/local/mysql/data/relay-log

max_relay_log_size=512M

relay-log-purge=ON

read-only

[mysqldump]

quick

max_allowed_packet = 32M

创建mysql
用户

# groupadd -g 1600 mysql

# useradd -g mysql -u 1600 -M -s /sbin/nologin
mysql

# id mysql

uid=1600(mysql) gid=1600(mysql) 组=1600(mysql)

3 mysqld.service

# cat /usr/lib/systemd/system/mysqld.service

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

Type=forking

PIDFile=/usr/local/mysql/data/mysqld.pid

TimeoutSec=0

PermissionsStartOnly=true

#
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd

ExecStart=/usr/local/mysql/bin/mysqld
--defaults-file=/etc/my.cnf --daemonize
--pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS

EnvironmentFile=-/etc/sysconfig/mysql

LimitNOFILE = 65535

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

2.2
主库操作(172.16.1.151)

1 登录主库创建库、表、数据

mysql> reset master;

# 因为在上面我修改了root密码、创建了用于主从复制的'repl'@'%'用户,并刷新了

# 权限表,这些操作的pos点都被记录了下来,为了下面实验一致,我将binlog日

# 志重置,从重新开始。

mysql> create database test;

mysql> use test;

mysql> create table user (name
varchar(50));

mysql> insert
into user(name) values("zhangsan");

mysql> show master status;

2 使用xtrabackup在线备份主库

# mkdir -p /mysql_backup/

# innobackupex -uroot -p"mysql5.7@2021" --stream=tar
/mysql_backup/ 2>/dev/null |gzip >/mysql_backup/`date
+%F_%H-%M-%S`.tar.gz

# du -sh
/mysql_backup/2020-10-21_16-23-39.tar.gz

580K /mysql_backup/2020-10-21_16-23-39.tar.gz

3 将备份的主库发送到从库

# scp -rp /mysql_backup/2020-10-21_16-23-39.tar.gz
root@172.16.1.152:/tmp/

2.3
从库操作(172.16.1.152)

1 解压主库备份

# mkdir -p /tmp/mysql_data/

# cd /tmp/mysql_data/

# tar -xzf
/tmp/2020-10-21_16-23-39.tar.gz

# ls -l --full-time

2 回滚事务日志

# innobackupex --apply-log --redo-only
/tmp/mysql_data/

3 完全还原备份库

# innobackupex --defaults-file=/etc/my.cnf --copy-back
/tmp/mysql_data/

# ls -l --full-time
/usr/local/mysql/data/

4 查看主库的binlog_pos点

# cat
/usr/local/mysql/data/xtrabackup_info

binlog_pos = filename 'mysql-bin.000001', position
'750'

5 启动从数据库

# chown -R mysql.mysql
/usr/local/mysql/

# systemctl start mysqld

# cat /usr/local/mysql/data/auto.cnf

[auto]

server-uuid=6651dc51-1366-11eb-a67e-000c29c5b550

6 清空主库信息

# mysql -uroot -pmysql5.7@2021

mysql> reset slave;

# 清空从节点信息

mysql> reset master;

# 清空mysql.gtid_executed表信息

7 配置连接主库信息

# mysql> CHANGE MASTER TO

MASTER_HOST='172.16.1.151',

MASTER_PORT=3306,

MASTER_USER='repl',

MASTER_PASSWORD='repl@2020',

MASTER_LOG_FILE='mysql-bin.000001',

MASTER_LOG_POS=750;

8 启动主从复制

mysql> start slave;

mysql> show slave status\G;

基于xtrabackup的主从同步的更多相关文章

  1. 采用xtrabackup部署主从同步

    1.停掉从库192.168.1.23[root@dev-env23 tmp]# service mysqld stopStopping mysqld: [ OK ] 2.主库全备份cd /opt/xt ...

  2. Mysql 实现基于binlog的主从同步

    工作原理 1.主节点必须启用二进制日志,记录任何修改了数据库数据的事件.2.从节点开启一个线程(I/O Thread)把自己扮演成 mysql 的客户端,通过 mysql 协议,请求主节点的二进制日志 ...

  3. RocketMQ(九):主从同步的实现

    分布式系统的三大理论CAP就不说了,但是作为分布式消息系统的rocketmq, 主从功能是最最基础的了.也许该功能现在已经不是很常用了,但是对于我们理解一些分布式系统的常用工作原理还是有些积极意义的. ...

  4. 通过 XtraBackup 实现不停机不锁表搭建主从同步

    简介 Xtrabackup是由 Percona 开发的一个开源软件,可实现对 InnoDB 的数据备份,支持在线热备份(备份时不影响数据读写).备份时,Xtrabackup 会将 Master 的 b ...

  5. XtraBackup做mysql主从同步

    一.背景: 线上一个主库压力比较大,所以增加一个从库,但是不能重启或者停止主库的正常运行,不能锁库锁表影响业务的正常运行.所以这里想到了XtraBackup 二.XtraBackup介绍: Xtrab ...

  6. xtrabackup备份MySQL并主从同步

    为什么要使用xtarbackup? mysqldump备份数据库的时候,会锁库锁表,导致业务服务的暂时停滞,数据库数量小还没有感觉,当数据超过几个G的时候,使用mysqldump会严重影响服务器性能, ...

  7. SQL SERVER 基于数据库镜像的主从同步(数据库镜像实践汇总)

    SQL SERVER 基于数据库镜像的主从同步 Author:chaoqun.guo    createtime:2019-03-26 目录 SQL SERVER 基于数据库镜像的主从同步... 1 ...

  8. 基于mysql主从同步的proxy读写分离

    mysql-proxy 简介 MySQL Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包括:负载 ...

  9. percona-mysql5.7.24使用xtrabackup工具配置主从同步

    主从配置详细过程: 环境准备: 配置好服务器,主从服务器都安装并启动mysql数据库 # 添加读写账号和只读账号,应用配置中,写主库用读写账号,统计从库数据yoga只读账号 grant select, ...

随机推荐

  1. ES6对象的新增方法的使用

    Object.assign Object Object.assign(target, ...sources) 将所有可枚举属性的值从一个或多个源对象复制到目标对象 参数: target 目标对象 so ...

  2. android设置时钟

    <TextClock        android:id="@+id/timeText"        android:layout_width="match_pa ...

  3. Python3 url解码与参数解析

    Python3 url解码与参数解析 有些子节点名字直接就是编码后的url,就像下面这行一样: url='dubbo%3A%2F%2F10.4.5.3%3A20880%2Fcom.welab.auth ...

  4. Celery 架构组成

    Celery 是一个强大的 分布式任务队列 的 异步处理框架,它可以让任务的执行完全脱离主程序,甚至可以被分配到其他主机上运行.我们通常使用它来实现异步任务(async task)和定时任务(cron ...

  5. Git如何下载clone指定的tag

    Git如何下载clone指定的tag 如上图,我想下载Tags标签为solution-4 的代码,如何处理呢? 命令如下: git clone --branch solution-4 git@gith ...

  6. C# HTTP请求对外接口、第三方接口公用类

    /// <summary> /// 网络数据请求公共函数 /// </summary> public class HttpWebRequestCommon { #region ...

  7. 永远的ace 实验七 团队作业4—团队项目需求建模与系统设计(1)

    项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST/ 这个作业要求链接 https://www.cnblogs.com/nwnu-da ...

  8. OFRecord 图片文件制数据集

    OFRecord 图片文件制数据集 在 OFRecord 数据格式 和 加载与准备 OFRecord 数据集 中,分别学习了 OFRecord 数据格式,以及如何将其它数据集转为 OFRecord 数 ...

  9. CodeGen用户定义的扩展令牌

    CodeGen用户定义的扩展令牌 用户定义的扩展令牌是一种特殊的令牌,开发人员可以确定令牌的名称以及在代码生成过程中遇到令牌时要插入的值. CodeGen支持多种机制,允许通过以下方式实现用户定义的令 ...

  10. NVIDIA空中导航SDK改造5G通信

    NVIDIA空中导航SDK改造5G通信 Transforming Next-Generation Wireless with 5T for 5G and the NVIDIA Aerial SDK N ...