Mycat 后面接一个Oracle实例与一个MySQL实例,假设用户表,订单表,转账记录表,Oracle字符集为GBK的,MySQL字符集则要求UTF8的
完成用户表与订单表到MySQL的迁移过程,要求导数据是通过命令行连接Mycat来完成的
同时操作如下过程:
在Mycat里查询转账记录表,查询OK
在Mycat里查询用户表,查询OK
Linux部署安装oracle详细全过程:http://blog.csdn.net/mchdba/article/details/43086037
创建oracle用户:
创建表空间: create tablespace mycat logging datafile '/data/oracle/powerdes/mycat.dbf' size 50m autoextend on next 50m extent management local; 创建用户并赋予权限: CREATE USER mycat PROFILE "DEFAULT" IDENTIFIED BY "mycat123456" DEFAULT TABLESPACE "MYCAT" ACCOUNT UNLOCK; grant create session,connect,resource to mycat;
执行过程如下: SQL> create tablespace mycat logging datafile '/data/oracle/powerdes/mycat.dbf' size 50m autoextend on next 50m 7 extent management local;
Tablespace created.
SQL> SQL> CREATE USER mycat PROFILE "DEFAULT" IDENTIFIED BY "mycat123456" DEFAULT TABLESPACE "MYCAT" ACCOUNT UNLOCK;
User created.
SQL> grant create session,connect,resource to mycat;
Grant succeeded.
SQL>
确认oracle的字符集为GBK: SQL> select userenv('language') from dual;
USERENV('LANGUAGE') ---------------------------------------------------- AMERICAN_AMERICA.ZHS16GBK
SQL> 在mycat平台上建oracle表,执行过程,当然也可以在后续直接在oracle实例上建表: mysql> CREATE TABLE UC_USER(ID number,UC_NAME VARCHAR(64),CREATE_TIME DATE); Query OK, 0 rows affected (0.16 sec) OK!
mysql> CREATE TABLE UC_ORDER(IDnumber,UC_ID number,SHOP_NAME VARCHAR(64),CREATE_TIME DATE); Query OK, 0 rows affected (0.05 sec) OK!
mysql> CREATE TABLE TRADERS(IDnumber,UC_ID number,ORDER_ID number, FEE number,TRADE_STATUSchar(1),CREATE_TIME DATE); Query OK, 0 rows affected (0.02 sec) OK! mysql> |
文章原地址:http://blog.csdn.net/mchdba/article/details/50995059,作者mchdba(黄杉),谢绝转载。
Linux下源码部署安装mysql实例详细全过程:http://blog.csdn.net/mchdba/article/details/35994251
建库,这里为了好提现对比,所以将mysql实例下的表加了后缀_2来区分:
去mysql实例上建库: mysql> create database powerdes; Query OK, 1 row affected (0.00 sec)
mysql> 在myacat命令行上建表,mysql相对于oracle,比如数字int、日期datetime等都描述不一样: mysql> CREATE TABLE UC_USER_2(ID int, UC_NAME VARCHAR(64),CREATE_TIME DATETIME); Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE UC_ORDER_2(ID int,UC_ID int,SHOP_NAME VARCHAR(64),CREATE_TIME DATETIME); Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE TRADERS_2(ID int,UC_ID int,ORDER_ID int, FEE int,TRADE_STATUS char(1),CREATE_TIME DATETIME); Query OK, 0 rows affected (0.01 sec)
mysql> |
<table name="UC_USER" primaryKey="_ID" dataNode="dn_oracle_1" needAddLimit="false"/> <table name="ORDER" primaryKey="_ID" dataNode="dn_oracle_1" needAddLimit="false"/> <table name="TRADERS" primaryKey="_ID" dataNode="dn_oracle_1" needAddLimit="false"/>
<dataNode name="dn_oracle_1" dataHost="dh_oracle_1" database="powerdes" />
<dataHost name="dh_oracle_1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="oracle" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select 1 from dual</heartbeat> <connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql> <writeHost host="hostM1" url="jdbc:oracle:thin:@192.168.121.90:1521:powerdes" user="mycat" password="mycat123456" > </writeHost> </dataHost> |
Schema.xml中mysql配置:
Schema.xml中
<table name="UC_USER_2"primaryKey="_ID" dataNode="dn_uc"needAddLimit="false"/> <table name="UC_ORDER_2"primaryKey="_ID" dataNode="dn_oracle_1"needAddLimit="false"/> <table name="TRADERS_2" primaryKey="_ID"dataNode="dn_uc" needAddLimit="false"/> <dataNode name="dn_uc"dataHost="dh1" database="powerdes" /> |
需要把oracle的jar包放到mycat安装目录下,一般是用这个ojdbc14.jar,放到/usr/mysql/mycat/lib目录下面,然后重新加载下mycat生效。
进入oracle实例窗口建表(如果前面没有建,这可以建下表):
oracle实例建表SQL语句如下: CREATE TABLE UC_USER(ID number, UC_NAME VARCHAR(64),CREATE_TIME DATE); CREATE TABLE UC_ORDER(ID number,UC_ID number,SHOP_NAME VARCHAR(64),CREATE_TIME DATE); CREATE TABLE TRADERS(ID number,UC_ID number,ORDER_ID number, FEE number,TRADE_STATUS char(1),CREATE_TIME DATE);
执行建表过程: [oracle@hch_test_121_90 admin]$ rlwrap sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Sun Mar 27 22:24:01 2016
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> CREATE TABLE mycat.UC_USER(ID number, UC_NAME VARCHAR(64),CREATE_TIME DATE);
Table created.
SQL> CREATE TABLE mycat.UC_ORDER(ID number,UC_ID number,SHOP_NAME VARCHAR(64),CREATE_TIME DATE);
Table created.
SQL> CREATE TABLE mycat.TRADERS(ID number,UC_ID number,ORDER_ID number, FEE number,TRADE_STATUS char(1),CREATE_TIME DATE);
Table created.
SQL> |
如下图01.png
开始在oracle实例窗口录入数据:
录入用户数据: INSERT INTO mycat.UC_USER(ID,UC_NAME,CREATE_TIME)VALUES(1,'张三',SYSDATE); INSERT INTO mycat.UC_USER(ID,UC_NAME,CREATE_TIME)VALUES(2,'李四',SYSDATE); 录入订单数据: INSERT INTO mycat.UC_ORDER(ID,UC_ID,SHOP_NAME,CREATE_TIME)VALUES(1,1,'mycat技术权威指南书籍',SYSDATE); INSERT INTO mycat.UC_ORDER(ID,UC_ID,SHOP_NAME,CREATE_TIME)VALUES(2,2,'mysql高性能第三版',SYSDATE); 录入转账数据: INSERT INTO mycat.TRADERS(ID,UC_ID,ORDER_ID,FEE,TRADE_STATUS,CREATE_TIME)VALUES(1,1,1,59,1,SYSDATE); INSERT INTO mycat.TRADERS(ID,UC_ID,ORDER_ID,FEE,TRADE_STATUS,CREATE_TIME)VALUES(2,2,2,119,1,SYSDATE); |
如下图02.png:
防止乱码配置,在server.xml里面配置:
<property name="charset">utf8</property> |
在mycat命令行迁移出来:
mysql -h127.0.0.1 -P8066 -utest -ptest -DTESTDB -s -N -e "select * from UC_USER" > /tmp/UC_USER.txt |
执行图下图03.png:
在mycat命令行执行load data infile '/tmp/UC_USER_2.txt' into table UC_USER_2;:
load data infile '/tmp/UC_USER_2.txt' into table UC_USER_2; mysql> load data infile '/tmp/UC_USER_2.txt' into table UC_USER_2; Query OK, 2 rows affected (0.03 sec) Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from UC_USER_2; +------+---------+---------------------+ | ID | UC_NAME | CREATE_TIME | +------+---------+---------------------+ | 1 | ?? | 2016-03-27 22:33:12 | | 2 | ?? | 2016-03-27 22:33:17 | +------+---------+---------------------+ 2 rows in set (0.00 sec)
mysql> exit |
Gbk导入到mysql中utf8,乱码了,怎么破?
指导思想:
Mycat对于数据库而言就是客户端,一般来讲客户端字符编码与数据库服务端一致就不会有什么问题,或者是严格超集,保证插入到数据库不因转码而丢失。客户端、系统终端、数据库三者字符庥得协调,否则不一致的话,会有字符集转换的问题,有时候,显示正确的,但不一定在数据库就保存正确,或者保存到数据库的数据编码正确,没准显示为乱码。这就意味着,在oracle里面是gbk或者latin1啥都没有关系,只要做好以下3点:
(1),在迁移到mysql里面,在mycat平台上从oracle导出时候字符集设置为utf8
(2),在mycat平台上导入到mysql字符集也要设置为utf8
(3), 然后在mycat平台上查看数据也要设置字符集为utf8
做好了以上3点,三而合一,这样就能正常显示中文了。
设定好utf8,--default-character-set=utf8:
[root@oracle_standby conf]# mysql -h127.0.0.1 -P8066 -utest -ptest -DTESTDB -s -N --default-character-set=utf8 -e "select * from UC_USER" > /tmp/UC_USER_2.txt [root@oracle_standby conf]# mysql -h127.0.0.1 -P8066 -utest -ptest -DTESTDB -s -N --default-character-set=utf8 -e "select * from UC_ORDER" > /tmp/UC_ORDER_2.txt [root@oracle_standby conf]# mysql -h127.0.0.1 -P8066 -utest -ptest -DTESTDB -s -N --default-character-set=utf8 -e "select * from TRADERS" > /tmp/TRADERS_2.txt |
mysql> set names utf8; Query OK, 0 rows affected (0.00 sec)
mysql> load data infile '/tmp/UC_USER_2.txt' into table UC_USER_2; Query OK, 2 rows affected (0.03 sec) Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> load data infile '/tmp/UC_ORDER_2.txt' into table UC_ORDER_2; Query OK, 2 rows affected (0.02 sec) Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> load data infile '/tmp/TRADERS_2.txt' into table TRADERS_2; Query OK, 2 rows affected (0.02 sec) Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from UC_USER_2; +------+---------+---------------------+ | ID | UC_NAME | CREATE_TIME | +------+---------+---------------------+ | 1 | 张三 | 2016-03-27 22:33:12 | | 2 | 李四 | 2016-03-27 22:33:17 | +------+---------+---------------------+ 2 rows in set (0.00 sec)
mysql> select * from UC_ORDER_2; +------+-------+-------------------------------+---------------------+ | ID | UC_ID | SHOP_NAME | CREATE_TIME | +------+-------+-------------------------------+---------------------+ | 1 | 1 | mycat技术权威指南书籍 | 2016-03-27 22:33:22 | | 2 | 2 | mysql高性能第三版 | 2016-03-27 22:33:26 | +------+-------+-------------------------------+---------------------+ 2 rows in set (0.00 sec)
mysql> |
08_1.png,08_2.png:
INFO | jvm 1 | 2016/03/27 21:40:30 |WrapperSimpleApp: Encountered an error running main:java.lang.ExceptionInInitializerError
INFO | jvm 1 | 2016/03/27 21:40:30 |java.lang.ExceptionInInitializerError
INFO | jvm 1 | 2016/03/27 21:40:30 | atorg.opencloudb.MycatStartup.main(MycatStartup.java:50)
INFO | jvm 1 | 2016/03/27 21:40:30 | atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
INFO | jvm 1 | 2016/03/27 21:40:30 | atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
INFO | jvm 1 | 2016/03/27 21:40:30 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
INFO | jvm 1 | 2016/03/27 21:40:30 | atjava.lang.reflect.Method.invoke(Method.java:497)
INFO | jvm 1 | 2016/03/27 21:40:30 | atorg.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:240)
INFO | jvm 1 | 2016/03/27 21:40:30 | atjava.lang.Thread.run(Thread.java:745)
INFO | jvm 1 | 2016/03/27 21:40:30 |Caused by: org.opencloudb.config.util.ConfigException:java.lang.NumberFormatException: For input string:"oracle:thin:@192.168.121.90:1521:powerdes"
INFO | jvm 1 | 2016/03/27 21:40:30 | at org.opencloudb.config.loader.xml.XMLSchemaLoader.load(XMLSchemaLoader.java:111)
INFO | jvm 1 | 2016/03/27 21:40:30 | atorg.opencloudb.config.loader.xml.XMLSchemaLoader.<init>(XMLSchemaLoader.java:71)
INFO | jvm 1 | 2016/03/27 21:40:30 | at org.opencloudb.config.loader.xml.XMLSchemaLoader.<init>(XMLSchemaLoader.java:75)
INFO | jvm 1 | 2016/03/27 21:40:30 | atorg.opencloudb.ConfigInitializer.<init>(ConfigInitializer.java:63)
INFO | jvm 1 | 2016/03/27 21:40:30 | atorg.opencloudb.MycatConfig.<init>(MycatConfig.java:69)
INFO | jvm 1 | 2016/03/27 21:40:30 | atorg.opencloudb.MycatServer.<init>(MycatServer.java:104)
INFO | jvm 1 | 2016/03/27 21:40:30 | atorg.opencloudb.MycatServer.<clinit>(MycatServer.java:73)
INFO | jvm 1 | 2016/03/27 21:40:30 | ... 7 more
解决,配置问题,如下配置dbDriver写错了,dbDriver要写成jdbc:
<dataHost name="oracle1"maxCon="1000" minCon="1" balance="0"writeType="0" dbType="oracle" dbDriver="jdbc">