使用xtrabackup搭建mysql主从

在用xtrabackup搭建主从前,先理解几个xtrabackup的参数

--prepare (非常重要!)          prepare a backup for starting mysql server on the backup
  一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据 文件仍处理不一致状态。  --prepare的作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态。相当于oracle的recover,数据库restore后,需要recover应用redo使控制文件,数据文件s的scn达到一致。
  xtrabackup相当于提前做了这步,然后再恢复
--use-memory crash recovery时分配的buffer_pool大小

--copy-back  cp拷贝备份到某个路径

--move-back move备份到某个路径

本次备份恢复目标:

备份3306数据库,恢复到本地的3309端口,并开启主从同步

实施步骤:

1 主库用xtrabackup做热备

2 安装从库软件(略)
3 从库恢复
4 配置主从参数 (略,参考我的文章https://blog.csdn.net/qq_40687433/article/details/107822180
5 开启同步

扫描二维码关注公众号,回复: 12121784 查看本文章

1 、3306物理热备份

xtrabackup --defaults-file=/etc/my.cnf  -uroot -poracle -p3306 -S/tmp/mysql.sock --backup  --target-dir=/data/bak/xtrabak
 

2、同一台机器,mysql软件已经安装了

3、从库恢复

3.1 、 prepare

xtrabackup --prepare  --target-dir=/data/bak/xtrabak
xtrabackup发现备份没有prepare,然后发起crash recovery,recover完后shutdown

3.2、准备cnf文件

准备cnf文件,配置data dir路径,端口号等等

参考cnf 3309,直接cp的3306配置,红色标注是我做了修改的:

[root@lzl mysql3309]# cat /etc/my3309.cnf 
[mysqld]
basedir=/usr/local/mysql
datadir = /data/mysql3309
socket = /tmp/mysql3309.sock
character_set_server = utf8
default-time-zone = '+08:00'
port = 3309
user=mysql
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
server-id=1003

gtid-mode=on 
enforce-gtid-consistency

[mysqld_safe]
basedir=/usr/local/mysql
log-error=/data/mysql/mysql3309.log
pid-file=/data/mysql/mysqld3309.pid

user=mysql

3.3  copy-back恢复数据库
xtrabackup --defaults-file=/etc/my3309.cnf  --target-dir=/data/bak/xtrabak  --copy-back

修改权限(root copy的,恢复出来的文件都是root权限,mysql无法读取)

cd /data/mysql3309/

chown -R mysql.mysql *

3.4 启动数据库

mysqld_safe --defaults-file=/etc/my3309.cnf &

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lzldb              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.02 sec)
mysql> show slave status\G
Empty set (0.01 sec)

mysql> show master status\G
Empty set (0.00 sec)

3.5 开启主从同步

查看binlog pos
[root@lzl mysql3309]# cat xtrabackup_binlog_pos_innodb 
master-bin.000005       824

因为主备都打开了GTID,所以我在start slave的时候不需要指定pos,备库会自动寻找GTID的位置。如果没有开启GTID需要指定binlog文件和pos位置。
change master to
master_host='127.0.0.1',
master_port=3306,
master_user='replicator',
master_password='oracle',
master_auto_position=1;

start slave;

show slave status\G  查看状态正常

=====================================================

搭建过程中遇到的问题:

怎么恢复cnf? xtrabackup备份了cnf文件,自然也可以自己配置一个出来,启动的时候指定这个文件就行了
怎么恢复到另一个端口? 原理跟cnf一样,端口在cnf中配置即可

第一次启动mysqld时失败,查看error-log如下
error-log:
[mysql@lzl mysql]$ tail -200f mysql3309.log 
 
2020-08-14T06:42:09.340896Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-14T06:42:09.341107Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2020-08-14T06:42:09.341160Z 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.20) starting as process 28821 ...
2020-08-14T06:42:09.367569Z 0 [Note] InnoDB: PUNCH HOLE support available
2020-08-14T06:42:09.367613Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-08-14T06:42:09.367619Z 0 [Note] InnoDB: Uses event mutexes
2020-08-14T06:42:09.367623Z 0 [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
2020-08-14T06:42:09.367627Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2020-08-14T06:42:09.367630Z 0 [Note] InnoDB: Using Linux native AIO
2020-08-14T06:42:09.367861Z 0 [Note] InnoDB: Number of pools: 1
2020-08-14T06:42:09.368133Z 0 [Note] InnoDB: Using CPU crc32 instructions
2020-08-14T06:42:09.370769Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-08-14T06:42:09.386467Z 0 [Note] InnoDB: Completed initialization of buffer pool
2020-08-14T06:42:09.388701Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2020-08-14T06:42:09.398876Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2020-08-14T06:42:09.398926Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable

2020-08-14T06:42:09.398938Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2020-08-14T06:42:10.000697Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2020-08-14T06:42:10.000755Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2020-08-14T06:42:10.000768Z 0 [ERROR] Failed to initialize plugins.
2020-08-14T06:42:10.000781Z 0 [ERROR] Aborting

检查发现data目录文件全是root(因为是root  copy-back的)所以需要修改权限

data dir及其文件全部改成mysql:mysql权限
[root@lzl mysql3309]# chown -R mysql.mysql *

2020-08-14T06:42:10.001988Z 0 [Note] Binlog end
2020-08-14T06:42:10.006377Z 0 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

文章参考:  https://www.cnblogs.com/zhoujinyi/p/5893333.html

猜你喜欢

转载自blog.csdn.net/qq_40687433/article/details/108004966