centos stream 9下mysql 备份

centos stream 9下mysql 备份

文章目录

  • centos stream 9下mysql 备份
  • 备份方式
  • 一、冷备份
    • 1.逻辑备份
    • 2.暴力备份
    • 3.mydumper 备份
  • 二、热备份
    • 1.xtrabackup (下载地址 [https://www.percona.com/downloads](https://www.percona.com/downloads))


备份方式

一、冷备份

1.逻辑备份

mysql> flush tables with read lock; #主库执行
Query OK, 0 rows affected (0.00 sec)
[root@localhost local]# mysqldump -h192.168.243.134 -u slave -p wine > /home/mysql/wine.sql #从库执行
Enter password: 
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 
Warning: A dump from a server that has GTIDs enabled will by default include the GTIDs of all transactions, even those that were executed during its extraction and might not be represented in the dumped data. This might result in an inconsistent data dump. 
In order to ensure a consistent backup of the database, pass --single-transaction or --lock-all-tables or --master-data. (提示需要加上--set-gtid-purged=OFF)
[root@localhost local]# mysqldump -h192.168.243.134 -u slave -p wine --set-gtid-purged=OFF > /home/mysql/wine.sql
Enter password: 
[root@localhost local]# 
[root@localhost local]# mysql -f -u slave -p wine < /home/mysql/wine.sql  #从库执行
Enter password: 
[root@localhost local]# 

更为简便的方法可以借助Navicat 15 for MySQL 工具进行备份

2.暴力备份

停止主库,把主库下mysql 下的目录直接scp 到从库mysql下(本人并没有试过)

3.mydumper 备份

1.下载mydumper (https://github.com/mydumper/mydumper/releases)
2.安装扩展 (https://github.com/mydumper/mydumper)
3.安装mydumper

[root@localhost mydumper]# rpm -ivh mydumper-0.15.2-7.el9.x86_64.rpm  #安装
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
正在升级/安装...
   1:mydumper-0.15.1-3                ################################# [100%]
[root@localhost mydumper]# yum list |grep mydumper  #查看安装
mydumper.x86_64                      0.15.2-7                                       @System            
[root@localhost mydumper]# 
mydumper中的主要参数:
· -host,-h:连接的MySQL服务器
· -user,-u:用户备份的连接用户
· -password,-p:用户的密码
· -port,-P:连接端口
· -socket,-S:连接socket文 件
· -database,-B:需要备份的数据库
· -table-list,-T:需要备份的表,用逗号(,)分隔
· -outputdir,-o:输出的目录
· -build-empty-files,-e:默 认无数据则只有表结构文件
· -regex,-x:支持正则表达式,如mydumper-regex'(2l(mysqltest)'
· -ignore-engines,-i:忽略的存储引擎
· -no- schemas,-m:不导出表结构
· -long-query-guard:长查询,默认60s
· -kill-long-queries,-k:可以设置kill长查询
· -verbose,-v:0=silent,1=errors, 2=warmings,3=info,默认是2
· -binlogs,-b:导出binlog
· -daemon,-D:启用守护进程模式
· -snapshot-interval,-I:dump快照间隔时间,默认60s
· -logfile,-L:mysaqldumper的目志输出,一般在Daemon模式下使用
#备份全库
mydumper -h 192.168.243.134 -u slave -p slave -o /home/mysql/backup  (出现如下报错 加参数 --clear 或--dirty)
** (mydumper:11698): ERROR **: 15:42:15.176: Directory is not empty (use --clear or --dirty): /home/mysql
追踪与中断点陷阱 (核心已转储)
mydumper -h 192.168.243.134 -u slave -p slave --dirty -o /home/mysql/backup
#备份指定的数据库
mydumper -h 192.168.243.134 -u slave -p slave -B wine --dirty -o  /home/mysql/backup 
#备份多张表
mydumper -h 192.168.243.134 -u slave -p slave -B wine  -T  tabelA,tableB --dirty -o /home/mysql/backup 
#还原指定的数据库
myloader -h 192.168.243.134 -u slave -p slave -B wine -d /home/mysql/backup/
#还原指定的表
myloader -h 192.168.243.134 -u slave -p slave -B wine -o wine_sign -d /home/mysql/backup/

二、热备份

1.xtrabackup (下载地址 https://www.percona.com/downloads)

#安装
yum localinstall percona-xtrabackup-80-8.0.35-30.1.el9.x86_64.rpm

xtrabackup 主要参数
–host 指定主机
–user 指定用户名
–password 指定密码
–port 指定端口
–databases 指定数据库
–parallel 并行
–compress 压缩
–incremental 创建增量备份
–incremental-basedir 指定包含完全备份的目录
–incremental-dir 指定包含增量备份的目录
–prepare 对备份进行预处理操作 一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚 未提交的事务或已经提交但尚未同步至数据文件中的事务。 因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
–apply-log-only 不回滚未提交事务
–copy-back 恢复备份目录
–backup 备份操作,备份到target-dir指定的目录
–force-non-empty-directories datadir目录不为空的前提下会覆盖(使用此参数时需要注意datadir目录不能与backup 目录下有相同的文件)

#全量备份
[root@localhost precona-toolkit]# xtrabackup --defaults-file=/etc/my.cnf --host=192.168.243.134 --user=slave --password=slave --port=3306  --backup --target-dir=/home/mysql/backup
#全量恢复
[root@localhost ~]# systemctl stop mysqld.service  #停止mysql服务
[root@localhost ~]# rm -rf /var/lib/mysql/*  #清空mysql 目录
[root@localhost ~]# rm -rf /var/log/mysqld.log  #删除mysql 日志
[root@localhost precona-toolkit]# xtrabackup  --prepare  --target-dir=/home/mysql/backup #恢复准备
[root@localhost precona-toolkit]# xtrabackup --copy-back --target-dir=/home/mysql/backup  #恢复
[root@localhost mysql]# chown -R mysql.mysql /var/lib/mysql  #修改权限
[root@localhost mysql]# systemctl restart mysqld.service #启动mysql

#增量备份
#第一次增量备份
[root@localhost precona-toolkit]# xtrabackup --defaults-file=/etc/my.cnf --host=192.168.243.134 --user=slave --password=slave --port=3306 --databases=wine --backup --target-dir=/home/mysql/backup1 --incremental-basedir=/home/mysql/backup
#第二次增量备份
[root@localhost precona-toolkit]# xtrabackup --defaults-file=/etc/my.cnf --host=192.168.243.134 --user=slave --password=slave --port=3306 --databases=wine --backup --target-dir=/home/mysql/backup2 --incremental-basedir=/home/mysql/backup1
#增量恢复
[root@localhost mysql]# systemctl stop mysqld.service #停止mysql 服务
[root@localhost precona-toolkit]# xtrabackup --defaults-file=/etc/my.cnf  --prepare --apply-log-only --target-dir=/home/mysql/backup
#第一次增量恢复
[root@localhost precona-toolkit]# xtrabackup  --defaults-file=/etc/my.cnf --prepare  --apply-log-only   --target-dir=/home/mysql/backup   --incremental-dir=/home/mysql/backup1
#第二次增量恢复
[root@localhost precona-toolkit]# xtrabackup --defaults-file=/etc/my.cnf  --prepare  --target-dir=/home/mysql/backup   --incremental-dir=/home/mysql/backup2
#恢复
[root@localhost precona-toolkit]# xtrabackup   --copy-back --target-dir=/home/mysql/backup 
[root@localhost mysql]# chown -R mysql.mysql /var/lib/mysql  #修改权限
[root@localhost mysql]# systemctl restart mysqld.service #启动mysql

你可能感兴趣的:(centos,mysql,数据库)