备份mysql

mysqldump

order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

all-databases

1
mysqldump -uroot -proot --all-databases >/tmp/all.sql

databases

1
mysqldump -uroot -proot --databases db1 db2 >/tmp/user.sql
1
2
3
4
5
#不导出表结构 仅导出数据  -t
mysqldump -uroot -p123456 -P3305 --databases k8s_grafana -t >~/backup/k8s_grafana_data.sql
#不导出任何数据,只导出数据库表结构 -d
mysqldump -uroot -p123456 -P3305 --databases grafana -d >~/backup/k8s_grafana_table.sql

tables

1
mysqldump -uroot -proot --databases db1 --tables a1 a2  >/tmp/db1.sql

where 条件过滤

1
mysqldump -uroot -proot --databases db1 --tables a1 --where='id=1'  >/tmp/a1.sql

no-data 表结构

1
mysqldump -uroot -proot --no-data --databases db1 >/tmp/db1.sql

-F 生成新binlog

1
mysqldump -uroot -proot --databases db1 -F >/tmp/db1.sql

https://www.cnblogs.com/chenmh/p/5300370.html

冲突

Mycli also reads the [client] section of MySQL’s option file, ~/.my.cnf (on Windows: C:\Users\<username>\.my.cnf). See the example file at the bottom of this page for more information.

1
2
3
4
5
6
7
8
[client]
# The client section is read by mycli and all MySQL applications.
.....
# Turn on the LOAD DATA INFILE statement
local-infile = on

# Configure the pager
pager = 'vim -'

mysqldump: [ERROR] unknown variable ‘local-infile=

mysqldump: [ERROR] unknown variable ‘pager=

导入

1
2
❯ mysql -uroot -p123456 -P3305  k8s_grafana   < ~/backup/k8s_grafana_data.sql

test

1
2
3
4
#查出结果复制执行,清空表数据
SELECT CONCAT('truncate table ',TABLE_NAME,';') AS a FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'k8s_grafana' ;
#查出结果复制执行,清空表
SELECT concat('DROP TABLE IF EXISTS ', table_name, ';') FROM information_schema.tables WHERE table_schema = 'k8s_grafana';

xtrabackup

安装

1
2
wget https://downloads.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.0.26-18/binary/debian/focal/x86_64/percona-xtrabackup-80_8.0.26-18-1.focal_amd64.deb
sudo dpkg -i percona-xtrabackup-80_8.0.26-18-1.focal_amd64.deb

sudo apt-get -f install

解压缩

1
sudo apt install lz4

指定

部分表
  • –tables=name:用正则表达式的方式指定包括的表
  • –tables-file=name:将要包括的表名放在指定的文件中
  • –tables-exclude=name:用正则表达式的方式指定排除的表,该选项比 –tables 的优先级高
部分数据库
  • –databases=name:用正则表达式的方式指定包括的库
  • –databases-file=name:将要包括的库名放在指定的文件中
  • –databases-exclude=name:用正则表达式的方式指定排除的库,该选项比 –databases 的优先级高

backup

全备份

1
xtrabackup --backup  --target-dir=/data/backups
1
2
❯ xtrabackup --defaults-file=/etc/my.cnf --backup --databases=‘k8s_grafana’ --user=root --host=127.0.0.1 -P 3305 --password=123456 --target-dir=~/backup

Please upgrade PXB, if a new version is available. To continue with risk, use the option –no-server-version-check. 版本

prepare

全备份还原准备

1
xtrabackup --prepare --target-dir=/data/backups/
点击打赏
文章目录
  1. 1. mysqldump
    1. 1.1. all-databases
    2. 1.2. databases
    3. 1.3. tables
    4. 1.4. where 条件过滤
    5. 1.5. no-data 表结构
    6. 1.6. -F 生成新binlog
    7. 1.7. 冲突
    8. 1.8. 导入
  2. 2. xtrabackup
    1. 2.1. 指定
      1. 2.1.1. 部分表
      2. 2.1.2. 部分数据库
    2. 2.2. backup
    3. 2.3. prepare
载入天数...载入时分秒... ,