Skip to content

3.命令行使用方式说明

woqutech-qdecoder edited this page May 5, 2022 · 1 revision

MySQL命令行使用方式说明

源/目标端数据库授权要求

########### 源端 MySQL ###########

# MySQL数据库中执行

## 源端创建校验元数据表(微数据量,不造成任何压力)
CREATE DATABASE dbmotion_internal_schema;
CREATE TABLE dbmotion_internal_schema.ptcheck_nofense (
modified datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
tbl varchar(64) NOT NULL,
chunk_id int(11) NOT NULL,
sql_where text,
chunk_rows bigint(20) DEFAULT NULL,
chunk_crc bigint(20) DEFAULT NULL,
PRIMARY KEY (tbl,chunk_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


create user dbmotionS@'%' identified by 'dbmotionS';
grant process,select,event,create,trigger,reload,replication client,SHOW VIEW on *.* to dbmotionS@'%';
grant all on dbmotion_internal_schema.ptcheck_nofense to dbmotionS@'%';
flush privileges;

# my.cnf中[mysqld]设置

## 源端须开启binlog(修改my.cnf并重启mysql)
log-bin=mysql-bin
binlog-format=ROW


## 设置server-id,跟迁移目标库不一样
server-id=33061


## 源端须启用GTID模式
gtid-mode=on
enforce-gtid-consistency=true
########### 目标 MySQL(注意目标不能是Slave) ###########
# MySQL数据库上执行
create user dbmotionT@'%' identified by 'dbmotionT';
grant all privileges on *.* to dbmotionT@'%' with grant option;
flush privileges;

# my.cnf中[mysqld]设置
## 设置server-id,跟迁移源端库不一样
server-id=33062

## 源端须启用GTID模式
gtid-mode=on
enforce-gtid-consistency=true # GTID only

## 设置复制元信息记录在表中
master_info_repository =table
relay_log_info_repository =table

常用命令

  • 迁移同步预先检查 迁移之前,你可以使用--pre-check=text进行源端/目标的环境检查,dbmotion会返回检查清单
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
          --target=dbuser/pwd@10.10.98.239:3306 /
		  --schemas=saledb,orderdb --pre-check=text
  • 迁移整个数据库(saledb,orderdb)到目标MySQL,包含结构,权限,数据
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
          --target=dbuser/pwd@10.10.98.239:3306 /
		  --schemas=saledb,orderdb  /
		  --move-model=all --exists-handle=ignore --do-truncate=n
  • 增量同步变化数据(saledb,orderdb)到目标MySQL
# 默认同步会自动使用全量保存的Pos位点,你也可以指定同步开始位点
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
      --target=dbuser/pwd@10.10.98.239:3306 /
      --schemas=saledb,orderdb --work-threads=8 /
      --move-model=onlychanged --binlog=mysql-bin.000035 --pos=9527 or --gtid=xxxxxxx
  • 校验数据和对象一致性
# ptcheck校验无需停止业务(要配合slave同步模式使用)

./dbmotion --source=squids/123456@10.10.120.192:3306 /
    --target=root/123456@10.10.120.235:3306 / 
    --schemas=saledb,orderdb /
    --data-check=y --check-mode=ptchecksum --work-threads=8
  • 迁移grds.table1-3数据到目标MySQL,无truncate,8线程工作
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
          --target=dbuser/pwd@10.10.98.239:3306 /
		  --tables=grds.table1,grds.table2,grds.table3 /
		  --move-model=onlydata --do-truncate=n --work-threads=8
  • 仅仅迁移对象结构到目标MySQL,如果对象存在就忽略
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
          --target=dbuser/pwd@10.10.98.239:3306 /
		  --tables=grds.table1,grds.table2 --views=grds.view1 --procedures=p_usercount --triggers=tri_firewall /
		  --move-model=onlymeta --exists-handle=ignore
  • 导出表数据到Text文件,列分隔符为逗号,换行符为'\n'(默认)
#this example is exporting all tables in saledb and orderdb
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
	   --export-data=y --file-format=text --column-separator=, /
	   --data-dir=/tmp/backupdir --schemas=saledb,orderdb
  • 导出表数据到HDFS,按照Parquet格式存放(不提供--to-hdfs,会保存本地)
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
	   --export-data=y --file-format=parquet --to-hdfs=hdfsuser@192.168.0.1:9000 /
	   --data-dir=/tmp/backupdir --schemas=saledb,orderdb
  • 调用mysqldump导出对象DDL到.sql文件
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
      --target=dbuser/pwd@10.10.120.235:3306 /
	  --clear-status=y --struct-dump=y --schemas=mybase,yourbase
  • 调用mysql client执行.sql文件
./dbmotion --source=dbuser/pwd@10.10.98.232:3306 /
      --target=dbuser/pwd@10.10.120.235:3306 /
	   --clear-status=n --sql-file=mysql_data.sql
  • 查看数据迁移,增量同步,数据校验结果
# 对象迁移
./dbmotion --get-result=objects
# 表数据全量
./dbmotion --get-result=tables
# 增量同步
./dbmotion --get-result=sync
# 数据校验
./dbmotion --get-result=check
# json结果输出
./dbmotion --get-result=wholejson
  • 如何处理对象转换map关系
    • 源端/目标对象名,schema不一致,我们提供简单的map转换配置
    • 仅仅数据迁移,对象迁移,数据校验支持转换配置,增量同步暂不支持
# table mapping(tab1 map to target sch2:maptab1,tab2,tab3 no mapping)
--tables=sch1.tab1:sch2:maptab1,sch.tab2,sch.tab3

# schema mapping(schema must exists)
--schemas=sourceschm:targetschm

MongoDB命令行使用方式说明

源/目标数据库权限要求

迁移过程需要连接源数据库和目标数据库,需要读取源数据库的权限,和写入目标数据库的权限。

########### 源端 MongoDB ###########
db.createUser({user:"root", pwd:"dbmotion#123", roles:[ {role: "root", db:"admin"}]})


########### 目标 MongoDB ###########
db.createUser({user:"root", pwd:"dbmotion#123", roles:[ {role: "root", db:"admin"}]})

下载并运行dbmotion

  • 使用docker一键下载并运行dbmotion
docker run -it --rm --pull always woqutech/dbmotion-mongo

此时就可以在容器中运行dbmoiton命令, 例如:

dbmotion --dbtype mongo --source root/dbmotion#123@10.10.150.207:27717 \
 	 --target root/dbmotion#123@10.10.150.208:27717 --schemas ycsb \
	 --move-model all --work-threads 8 --max-connections 32  --clear-status n

常用迁移命令

  • 迁移同步预先检查

迁移之前,你可以使用--pre-check=text进行源端/目标的环境检查,dbmotion会返回检查清单

dbmotion --dbtype=mongo --source=root/dbmotion@10.10.108.157:27017 /
          --target=root/dbmotion@10.10.40.115:179 /
		  --schemas=saledb,orderdb --pre-check=text
  • 迁移整个数据库(saledb,orderdb)到目标MongoDB,包含结构,权限,数据
dbmotion --dbtype=mongo \
	--source=root/dbmotion#123@10.10.150.207:27017 \
	--target=root/dbmotion#123@10.10.150.208:27517 \
	--schemas=saledb,orderdb \
	--move-model=all --exists-handle=ignore --do-truncate=y
  • 仅仅迁移对象结构到目标MongoDB,如果对象存在就忽略
dbmotion --dbtype=mongo \
	--source=root/dbmotion#123@10.10.150.207:27017 \
	--target=root/dbmotion#123@10.10.150.208:27517 \
	--schemas=saledb,orderdb \
	--move-model=onlymeta --exists-handle=ignore --do-truncate=y --clear-status=n
  • 迁移指定的集合
dbmotion --dbtype=mongo \
	--source=root/dbmotion#123@10.10.150.207:27017 \
	--target=root/dbmotion#123@10.10.150.208:27517 \
	--tables=saledb.t1,orderdb.t2 \
	--move-model=all --do-truncate=y --clear-status=n
  • 开启增量数据同步
dbmotion --dbtype=mongo \
	--source=root/dbmotion#123@10.10.150.207:27017 \
	--target=root/dbmotion#123@10.10.150.208:27517 \
	--move-model=onlychanged --clear-status=n

注意:增量同步开始后会持续拉取源库增量变更,直到用户用Ctrl+C,或者kill命令来结束增量同步

  • 查看数据迁移,增量同步,数据校验结果
# 对象迁移
dbmotion --get-result=objects
# 表数据全量
dbmotion --get-result=tables
# 增量同步
dbmotion --get-result=sync
# 数据校验
dbmotion --get-result=check
# json结果输出
dbmotion --get-result=wholejson