MySQL多线程备份工具mydumper

目录

一、mydumper简介

二、mydumper安装

1、yum安装mydumper

2、源码安装mydumper

三、mydumper参数介绍

四、myloader参数介绍

五、使用例子


一、mydumper简介

mydumper 是一款社区开源的逻辑备份工具。该工具主要由 C 语言编写,目前由 MySQL 、Facebook 等公司人员开发维护。

参考官方介绍,mydumper 主要有以下几点特性:

  • 支持多线程导出数据,速度更快。
  • 支持一致性备份。
  • 支持将导出文件压缩,节约空间。
  • 支持多线程恢复。
  • 支持以守护进程模式工作,定时快照和连续二进制日志。
  • 支持按照指定大小将备份文件切割。
  • 数据与建表语句分离。

mydumper 官网:https://launchpad.net/mydumper

mydumper github: https://github.com/mydumper/mydumper

mydumper下载:https://launchpadlibrarian.net/225370879/mydumper-0.9.1.tar.gz

wget https://launchpadlibrarian.net/225370879/mydumper-0.9.1.tar.gz

二、mydumper安装

1、yum安装mydumper

## RedHat / Centos

release=$(curl -Ls -o /dev/null -w %{url_effective} https://github.com/mydumper/mydumper/releases/latest | cut -d'/' -f8)
yum install https://github.com/mydumper/mydumper/releases/download/${release}/mydumper-${release:1}.el7.x86_64.rpm
yum install https://github.com/mydumper/mydumper/releases/download/${release}/mydumper-${release:1}.el8.x86_64.rpm

2、源码安装mydumper

        根据个人使用经验,推荐mydumper-0.9.1版本比较稳定,github上边的比较新,会出现预想不到bug。

# Dependencies for building mydumper

yum install -y cmake gcc gcc-c++ git make

yum install -y glib2-devel openssl-devel pcre-devel zlib-devel libzstd-devel

yum install -y mysql-devel

yum install -y Percona-Server-devel-57

yum install -y mariadb-devel
wget https://launchpadlibrarian.net/225370879/mydumper-0.9.1.tar.gz

tar -xzvf mydumper-0.9.1.tar.gz

cd mydumper-0.9.1 && cmake . && make

chmod a+x mydumper myloader

cp mydumper myloader /usr/local/bin/

三、mydumper参数介绍

mydumper 和 myloader 是相对应的一组可执行程序,二者的作用分别是导出数据与导入数据。

## mydumper --help

Usage:
  mydumper [OPTION?] multi-threaded MySQL dumping

Help Options:
  -?, --help                  Show help options

Application Options:
  -B, --database              Database to dump
  -T, --tables-list           Comma delimited table list to dump (does not exclude regex option)
  -O, --omit-from-file        File containing a list of database.table entries to skip, one per line (skips before applying regex option)
  -o, --outputdir             Directory to output files to
  -s, --statement-size        Attempted size of INSERT statement in bytes, default 1000000
  -r, --rows                  Try to split tables into chunks of this many rows. This option turns off --chunk-filesize
  -F, --chunk-filesize        Split tables into chunks of this output file size. This value is in MB
  -c, --compress              Compress output files
  -e, --build-empty-files     Build dump files even if no data available from table
  -x, --regex                 Regular expression for 'db.table' matching
  -i, --ignore-engines        Comma delimited list of storage engines to ignore
  -N, --insert-ignore         Dump rows with INSERT IGNORE
  -m, --no-schemas            Do not dump table schemas with the data
  -d, --no-data               Do not dump table data
  -G, --triggers              Dump triggers
  -E, --events                Dump events
  -R, --routines              Dump stored procedures and functions
  -W, --no-views              Do not dump VIEWs
  -k, --no-locks              Do not execute the temporary shared read lock.  WARNING: This will cause inconsistent backups
  --no-backup-locks           Do not use Percona backup locks
  --less-locking              Minimize locking time on InnoDB tables.
  -l, --long-query-guard      Set long query timer in seconds, default 60
  -K, --kill-long-queries     Kill long running queries (instead of aborting)
  -D, --daemon                Enable daemon mode
  -I, --snapshot-interval     Interval between each dump snapshot (in minutes), requires --daemon, default 60
  -L, --logfile               Log file name to use, by default stdout is used
  --tz-utc                    SET TIME_ZONE='+00:00' at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones, defaults to on use --skip-tz-utc to disable.
  --skip-tz-utc               
  --use-savepoints            Use savepoints to reduce metadata locking issues, needs SUPER privilege
  --success-on-1146           Not increment error count and Warning instead of Critical in case of table doesn't exist
  --lock-all-tables           Use LOCK TABLE for all, instead of FTWRL
  -U, --updated-since         Use Update_time to dump only tables updated in the last U days
  --trx-consistency-only      Transactional consistency only
  --complete-insert           Use complete INSERT statements that include column names
  -h, --host                  The host to connect to
  -u, --user                  Username with the necessary privileges
  -p, --password              User password
  -a, --ask-password          Prompt For User password
  -P, --port                  TCP/IP port to connect to
  -S, --socket                UNIX domain socket file to use for connection
  -t, --threads               Number of threads to use, default 4
  -C, --compress-protocol     Use compression on the MySQL connection
  -V, --version               Show the program version and exit
  -v, --verbose               Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2
  --defaults-file             Use a specific defaults file

四、myloader参数介绍

#  myloader --help

Usage:
  myloader [OPTION?] multi-threaded MySQL loader

Help Options:
  -?, --help                        Show help options

Application Options:
  -d, --directory                   Directory of the dump to import
  -q, --queries-per-transaction     Number of queries per transaction, default 1000
  -o, --overwrite-tables            Drop tables if they already exist
  -B, --database                    An alternative database to restore into
  -s, --source-db                   Database to restore
  -e, --enable-binlog               Enable binary logging of the restore data
  -h, --host                        The host to connect to
  -u, --user                        Username with the necessary privileges
  -p, --password                    User password
  -a, --ask-password                Prompt For User password
  -P, --port                        TCP/IP port to connect to
  -S, --socket                      UNIX domain socket file to use for connection
  -t, --threads                     Number of threads to use, default 4
  -C, --compress-protocol           Use compression on the MySQL connection
  -V, --version                     Show the program version and exit
  -v, --verbose                     Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2
  --defaults-file                   Use a specific defaults file

五、使用例子

       这里是我根据自己的环境优化的备份和还原常用参数

## cat backupdata.sh

#!/bin/bash

## 备份源库数据
copydata() {
mydumper -u 源库用户  -p '源库密码' -h 源库IP  -P 3306 --regex '^(?!(mysql|sys|information_schema|performance_schema|test))' -D -E -R -G -C -c  -t 4   -F 1 -s 4000000 -v 2 -l 360 --logfile log.log  -o data
}

# 还原到目标库
restore() {
nohup myloader -u 目标库用户  -p '目标库密码' -h 127.0.0.1  -P 3306  -C -t 4 -o  -v 2 --directory data/0 &
}

# 由于我的mydumper安装在目标库上,所有目标库IP 使用 127.0.0.1 

copydata
restore
-D守护进程方式运行mydumper
-E备份事件
-R

备份存储和函数 

-G备份触发器
-C在MySQL连接上使用压缩
-c压缩输出文件
-t要使用的线程数,默认值为4
-F将表拆分为此输出文件大小的块。此值以MB为单位
-sINSERT语句的尝试大小(以字节为单位),默认值为1000000
-v输出的详细性,0=无提示,1=错误,2=警告,3=信息,默认值2
-l以秒为单位设置长查询计时器,默认值为60
--logfile要使用的日志文件名,默认情况下使用stdout

-o

(--outputdir)

mydumper的-o表示:要将文件输出到的目录

-o

(--overwrite-tables)

myloader的-o表示:删除表(如果它们已经存在)
--directory要导入的转储目录,即mydumper备份的文件位置

  • 10
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Python的`threading`模块来实现多线程备份MySQL数据的脚本。以下是一个示例代码: ```python import threading import time import pymysql # 备份函数 def backup_data(thread_id, start, end): # 连接到MySQL数据库 connection = pymysql.connect( host='localhost', # 数据库主机地址 user='your_username', # 数据库用户名 password='your_password', # 数据库密码 database='your_database' # 数据库名称 ) try: # 创建游标对象 cursor = connection.cursor() # 构建查询语句 select_query = f"SELECT * FROM your_table LIMIT {start}, {end}" # 执行查询语句 cursor.execute(select_query) # 获取查询结果 data = cursor.fetchall() # 构建备份文件名 backup_filename = f"backup_{thread_id}.txt" # 将数据写入备份文件 with open(backup_filename, 'w') as file: for row in data: file.write(str(row) + '\n') print(f"Thread {thread_id} backup completed.") finally: # 关闭连接 connection.close() # 多线程备份数据 def multi_threaded_backup(num_threads): # 连接到MySQL数据库 connection = pymysql.connect( host='localhost', # 数据库主机地址 user='your_username', # 数据库用户名 password='your_password', # 数据库密码 database='your_database' # 数据库名称 ) try: # 创建游标对象 cursor = connection.cursor() # 构建查询语句 count_query = "SELECT COUNT(*) FROM your_table" # 执行查询语句 cursor.execute(count_query) # 获取总记录数 total_records = cursor.fetchone()[0] # 每个线程处理的记录数 records_per_thread = total_records // num_threads # 创建线程列表 threads = [] # 启动线程 for i in range(num_threads): start = i * records_per_thread end = start + records_per_thread if i == num_threads - 1: end = total_records # 最后一个线程处理剩余的记录 thread = threading.Thread(target=backup_data, args=(i+1, start, end)) thread.start() threads.append(thread) # 等待所有线程完成 for thread in threads: thread.join() print("Backup completed.") finally: # 关闭连接 connection.close() # 在这里指定要使用的线程数 num_threads = 4 # 多线程备份数据 multi_threaded_backup(num_threads) ``` 请确保替换示例代码中的`your_username`,`your_password`,`your_database`和`your_table`为你实际的数据库信息。 这段代码将使用指定数量的线程并行备份MySQL数据。它首先获取表中的总记录数,然后根据线程数计算每个线程应处理的记录数。然后,每个线程将处理一部分数据,并将其写入一个以线程ID命名的备份文件中。 请注意,多线程备份数据可能会对数据库服务器产生一定的负载,具体取决于系统性能和数据库配置。确保选择合适的线程数以避免过多的资源消耗。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值