如何在两个MySQL实例之间迁移数据

警示:尊重他人,有始有终

##

如何在两个MySQL实例之间迁移数据


跨实例迁移数据在mysql shell中变得更简单了,在mysql shell 8.3中新增了

util.copyInstance、util.copySchemas、util.copyTables

下面是util.copyTables的案例

迁移单表有非常多的方法(mysqldump,表空间传输等等),今天使用mysql shell来完成并测试了一下速度。

1、环境准备

主机1:192.168.5.130,准备一张1千万的表

[root@mydb01 ~]# sysbench --db-driver=mysql --time=300 --threads=20 --report-interval=5 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=1 --table_size=10000000 oltp_read_write --db-ps-mode=disable prepare

主机2:192.168.5.140,创建目标库

create database db01;

2、迁移

 MySQL  192.168.5.130:3306 ssl  JS > util.copyTables('test_db', ['sbtest1'], 'root@192.168.5.140:3306',{schema: "db01",threads:"8",deferTableIndexes:"all"skipBinlog:"true"})
Copying DDL and Data from in-memory FS, source: 192.168.5.130:3306, target: 192.168.5.140:3306.
SRC: Acquiring global read lock
SRC: Global read lock acquired
Initializing - done
SRC: 1 tables and 0 views will be dumped.
Gathering information - done
SRC: All transactions have been started
SRC: Locking instance for backup
SRC: Global read lock has been released
SRC: Writing global DDL files
SRC: Running data dump using 8 threads.
NOTE: SRC: Progress information uses estimated values and may not be accurate.
TGT: Opening dump...ata  0 / 1
NOTE: TGT: Dump is still ongoing, data will be loaded as it becomes available.
TGT: Target is MySQL 8.0.35. Dump was produced from MySQL 8.0.35
TGT: Scanning metadata...
TGT: Scanning metadata - done
TGT: Checking for pre-existing objects...
TGT: Executing common preamble SQL
TGT: Executing DDL...
TGT: Executing DDL - done
TGT: Executing view DDL...
Writing schema metadata - done
Writing DDL - done
Writing table metadata - done
SRC: Starting data dump
TGT: Executing view DDL - done
TGT: Loading data...
TGT: Starting data load
TGT: Recreating indexes...
103% (10.00M rows / ~9.62M rows), 65.02K rows/s, 12.10 MB/s
SRC: Dump duration: 00:02:12s
SRC: Total duration: 00:02:12s
SRC: Schemas dumped: 1
SRC: Tables dumped: 1
SRC: Data size: 1.96 GB
SRC: Rows written: 10000000
SRC: Bytes written: 1.96 GB
SRC: Average throughput: 14.77 MB/s
1 thds loading  ?% (1.96 GB / ?), 11.66 MB/s, 1 / 1 tables done
TGT: Executing common postamble SQL
Recreating indexes - done
TGT: 35 chunks (10.00M rows, 1.96 GB) for 1 tables in 1 schemas were loaded in 2 min 47 sec (avg throughput 14.61 MB/s)
TGT: 0 warnings were reported during the load.

---

Dump_metadata:
  Binlog_file: binlog.000006
  Binlog_position: 835069358
  Executed_GTID_set: 07ae84d6-9a51-11ee-b481-000c29d15c4d:1-106365

 MySQL  192.168.5.130:3306 ssl  JS >

3、总结

这里开了8个线程,并且推迟了所有的二级索引创建。不到3分钟就完成了。

实际生产环境应该更快(测试环境为3千元的洋垃圾)。

迁移的参数:源库的库和表,目标库的连接地址以及库,其他可选参数。


原文始发于微信公众号(库海无涯):如何在两个MySQL实例之间迁移数据

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/241213.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!