基于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. [bug] PyCharm远程Spark集群:Java gateway process exited before sending its port number

    原因 无法连接到集群上的java 解决 方法一: 在右上角Edit Configurations中,添加一条环境变量JAVA_HOME,值为远程机器上的java安装路径 方法二: 直接在代码里写上JA ...

  2. 寻找CPU使用率高的进程方法

    寻找CPU使用率高的进程方法 发布时间:  2017-07-13 浏览次数:  1362 下载次数:  0 问题描述 节点报CPU使用率高,甚至出现"ALM-12016 CPU使用率超过阈值 ...

  3. shell应用之简单计算器

    1 #!/bin/bash 2 while : 3 do 4 read -p "请输入计算规则:" JS 5 if [ -z $JS ];then 6 exit 7 else 8 ...

  4. System Verilog设计例化和连接

  5. Redis限制一键登录次数

    一.产生背景 之前的随笔提到过项目中写了一键登录功能.上线后除了有时候网络波动会导致登陆失败,其他情况一直稳如老狗 しかし,邮件看到有人恶意刷一键登录,这年头闲的人可真闲啊, 只能思考如何搞一搞 二. ...

  6. Java | Stream流、泛型、多线程 | 整理自用

    1.lambda 表达式 lambda 的延迟执行 可以对程序进行优化,尤其是使用 if {} else {} 条件判断,先判断条件是否成立,再传入计算好的参数. functionName( para ...

  7. 五、SVM推导过程

    SVM 时间复杂度一般为O(N³) 最重要的是推导过程 NIPS(机器学习顶级会议) 如果给定一个训练集,我们的目标是给定一个边界(一条线),离他最近的训练集样本路越宽越好 下面的几张图反映了SVM的 ...

  8. 纯C++代码实现将像素矩阵保存为bmp图片

    由于工作需要,时常需要将像素矩阵保存图片显示观看.为此,特地总结了三种使用纯C++代码生成bmp图片的方法.分别是使用自定义数据.从外界导入的txt和csv以及从图片中导入的数据. 1.使用自定义数据 ...

  9. pytorch空间变换网络

    pytorch空间变换网络 本文将学习如何使用称为空间变换器网络的视觉注意机制来扩充网络.可以在DeepMind paper 阅读更多有关空间变换器网络的内容. 空间变换器网络是对任何空间变换的差异化 ...

  10. YOLOV4知识点分析(一)

    YOLOV4知识点分析(一) 简 介 yolov4论文:YOLOv4: Optimal Speed and Accuracy of Object Detection arxiv:https://arx ...