练习目的
本次练习目的掌握从 MySQL 向 OceanBase 迁移数据的基本方法:mysqldump、datax 、canal 等。
练习内容
请记录并分享下列内容:
(必选)使用 mysqldump 将 mysql的表结构和数据同步到 OceanBase 的MySQL 租户中。
(必选)使用 datax 配置至少一个表的 MySQL 到 OceanBase 的 MySQL 租户的离线同步。
1.mysqldump 迁移 MySQL 表到 OceanBase
1.导出指定数据库的表结构(不包括数据)
mysqldump -h 127.0.0.1 -uadmin -P3358 -p -d cctest --compact > cctest_ddl.sql
会有一些特别的语法 OceanBase MYSQL 会不支持,但是不影响,需要替换掉其中部分。比如说变量 SQL_NOTES,DEFINER 语句等。本次导出的测试表无相关语句因此忽略。
2.导出指定数据库的表数据(不包括结构)
mysqldump -h 127.0.0.1 -uadmin -P3358 -p -t cctest > cctest_data.sql
3.在 obclient 客户端里通过 source 命令可以执行外部 SQL 脚本文件。
[root@test2 export]# obclient -h127.0.0.1 -uroot@obmysql -P13881
Welcome to the OceanBase. Commands end with ; or \g.
Your MySQL connection id is 3221556938
Server version: 5.7.25 OceanBase 3.1.2 (r10000392021123010-d4ace121deae5b81d8f0b40afbc4c02705b7fc1d) (Built Dec 30 2021 02:47:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> use cctest
Database changed
MySQL [cctest]> source cctest_ddl.sql
2.使用datax (离线)从 MySQL 同步表数据到 OceanBase
测试环境无法连接外网,先将安装包下载好上传至服务器
http://datax-opensource.oss-cn-hangzhou.aliyuncs.com/datax.tar.gz
tar -zvxf datax.tar.gz
cd datax
find ./datax/plugin -name ".*" | xargs rm -f
python ./datax/bin/datax.py -r mysqlreader -w oceanbasev10writer
vi myobce.json
{
"job": {
"setting": {
"speed": {
"channel": 4
},
"errorLimit": {
"record": 0,
"percentage": 0.1
}
},
"content": [
{
"reader": {
"name": "mysqlreader",
"parameter": {
"username": "admin",
"password": "admin",
"column": [
"*"
],
"connection": [
{
"table": [
"cctest"
],
"jdbcUrl": ["jdbc:mysql://10.0.0.1:3358/cctest?useUnicode=true&characterEncoding=utf8&useSSL=false"]
}
]
}
},
"writer": {
"name": "oceanbasev10writer",
"parameter": {
"obWriteMode": "insert",
"column": [
"*"
],
"preSql": [
"truncate table cctest"
],
"connection": [
{
"jdbcUrl": "||_dsc_ob10_dsc_||obdemo:obmysql||_dsc_ob10_dsc_||jdbc:oceanbase://127.0.0.1:13881/cctest?useLocalSessionState=true&allowBatch=true&allowMultiQueries=true&rewriteBatchedStatements=true",
"table": [
"cctest"
]
}
],
"username": "root",
"password":"",
"writerThreadCount":10,
"batchSize": 1000,
"memstoreThreshold": "0.9"
}
}
}
]
}
}
python datax/bin/datax.py myobce.json