admin

mycat接oracle和mysql多个实例

admin PHP 2023-02-26 1044浏览 0

Mycat 后面接一个Oracle实例与一个MySQL实例,假设用户表,订单表,转账记录表,Oracle字符集为GBK的,MySQL字符集则要求UTF8的
完成用户表与订单表到MySQL的迁移过程,要求导数据是通过命令行连接Mycat来完成的

oracle
scott/scott103@10.0.1.103/grs
select userenv(‘language’) from dual;
SIMPLIFIED CHINESE_CHINA.AL32UTF8
–oracle tables
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);

INSERT INTO UC_USER(ID,UC_NAME,CREATE_TIME)VALUES(1,’张三’,SYSDATE);
INSERT INTO UC_USER(ID,UC_NAME,CREATE_TIME)VALUES(2,’李四’,SYSDATE);
INSERT INTO UC_ORDER(ID,UC_ID,SHOP_NAME,CREATE_TIME)VALUES(1,1,’mycat技术权威指南书籍’,SYSDATE);
INSERT INTO UC_ORDER(ID,UC_ID,SHOP_NAME,CREATE_TIME)VALUES(2,2,’mysql高性能第三版’,SYSDATE);
INSERT INTO TRADERS(ID,UC_ID,ORDER_ID,FEE,TRADE_STATUS,CREATE_TIME)VALUES(1,1,1,59,1,SYSDATE);
INSERT INTO TRADERS(ID,UC_ID,ORDER_ID,FEE,TRADE_STATUS,CREATE_TIME)VALUES(2,2,2,119,1,SYSDATE);

mycat中配置oracle、mysql数据源
<table name=”UC_USER” primaryKey=”ID” type=”global” dataNode=”dn_oracle_1″ />
<table name=”UC_ORDER” primaryKey=”ID” type=”global” dataNode=”dn_oracle_1″ />
<table name=”TRADERS” primaryKey=”ID” type=”global” dataNode=”dn_oracle_1″ />

<dataNode name=”dn_oracle_1″ dataHost=”dn_oracle_1″ database=”grs” />

<dataHost name=”dn_oracle_1″ maxCon=”1000″ minCon=”10″ balance=”0″
writeType=”0″ dbType=”oracle” dbDriver=”jdbc” switchType=”1″ slaveThreshold=”100″>
<heartbeat>select 1 from dual </heartbeat>
<connectionInitSql>alter session set nls_date_format=’yyyy-mm-dd hh24:mi:ss'</connectionInitSql>
<!– can have multi write hosts –>
<writeHost host=”hostM1″ url=”jdbc:oracle:thin:@10.0.1.103:1521:grs” user=”scott” password=”scott103″ >
</writeHost>
</dataHost>
–mysql tables

mysql> CREATE TABLE UC_USER_2(ID int, UC_NAME VARCHAR(64),CREATE_TIME DATETIME);
mysql> CREATE TABLE UC_ORDER_2(ID int,UC_ID int,SHOP_NAME VARCHAR(64),CREATE_TIME DATETIME);
mysql> CREATE TABLE TRADERS_2(ID int,UC_ID int,ORDER_ID int, FEE int,TRADE_STATUS char(1),CREATE_TIME DATETIME);
mycat中配置oracle、mysql数据源
<table name=”UC_USER_2″ primaryKey=”ID” type=”global” dataNode=”dn1,dn2,dn3″ />
<table name=”UC_ORDER_2″ primaryKey=”ID” type=”global” dataNode=”dn1,dn2,dn3″ />
<table name=”TRADERS_2″ primaryKey=”ID” type=”global” dataNode=”dn1,dn2,dn3″ />

<dataNode name=”dn1″ dataHost=”localhost0″ database=”db1″ />
<dataNode name=”dn2″ dataHost=”localhost0″ database=”db2″ />
<dataNode name=”dn3″ dataHost=”localhost0″ database=”db3″ />
<dataHost name=”localhost0″ maxCon=”1000″ minCon=”10″ balance=”0″
writeType=”0″ dbType=”mysql” dbDriver=”native” switchType=”1″ slaveThreshold=”100″>
<heartbeat>show slave status </heartbeat>
<!– can have multi write hosts –>
<writeHost host=”hostM1″ url=”10.0.1.134:3306″ user=”system”
password=”mysql”>
<!– can have multi read hosts –>
<readHost host=”hostS1″ url=”192.168.3.110:3306″ user=”system” password=”mysql” />
</writeHost>
<!–writeHost host=”hostM2″ url=”192.168.3.110:3306″ user=”system”
password=”mysql” > –>
<!–readHost host=”hostS2″ url=”10.0.1.134:3306″ user=”system” password=”mysql” /> –>
<!–/writeHost> –>
<!– <writeHost host=”hostM2″ url=”localhost:3316″ user=”root” password=”123456″/> –>
</dataHost>
<dataHost name=”135_host” maxCon=”1000″ minCon=”10″ balance=”0″
writeType=”0″ dbType=”mysql” dbDriver=”native” switchType=”1″ slaveThreshold=”100″>
<heartbeat>select user() </heartbeat>
<writeHost host=”hostM2″ url=”10.0.1.135:3307″ user=”system”
password=”mysql”>
</writeHost>
</dataHost>
加载oracle的lib包
需要把oracle的jar包放到mycat安装目录下,一般是用这个ojdbc14.jar,放到/usr/mysql/mycat/lib目录下面,然后重新加载下mycat生效。
[root@hongquan lib]# ls ojdbc14.jar
ojdbc14.jar
[root@hongquan lib]# pwd
/usr/local/mycat/mycat/lib
配置并重启
mysql> show @@datanode;
+————-+—————–+——-+——–+——–+——+——+———+————+———-+———+—————+
| NAME | DATHOST | INDEX | TYPE | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+————-+—————–+——-+——–+——–+——+——+———+————+———-+———+—————+
| dn1 | localhost0/db1 | 0 | mysql | 0 | 2 | 1000 | 5 | 0 | 0 | 0 | -1 |
| dn10 | 135_host/db10 | 0 | mysql | 0 | 5 | 1000 | 8 | 0 | 0 | 0 | -1 |
| dn2 | localhost0/db2 | 0 | mysql | 0 | 1 | 1000 | 1 | 0 | 0 | 0 | -1 |
| dn3 | localhost0/db3 | 0 | mysql | 0 | 2 | 1000 | 2 | 0 | 0 | 0 | -1 |
| dn4 | localhost0/db4 | 0 | mysql | 0 | 1 | 1000 | 1 | 0 | 0 | 0 | -1 |
| dn5 | localhost0/db5 | 0 | mysql | 0 | 1 | 1000 | 1 | 0 | 0 | 0 | -1 |
| dn6 | localhost0/db6 | 0 | mysql | 0 | 1 | 1000 | 1 | 0 | 0 | 0 | -1 |
| dn7 | localhost0/db7 | 0 | mysql | 0 | 1 | 1000 | 1 | 0 | 0 | 0 | -1 |
| dn8 | localhost0/db8 | 0 | mysql | 0 | 1 | 1000 | 1 | 0 | 0 | 0 | -1 |
| dn9 | 135_host/db9 | 0 | mysql | 0 | 5 | 1000 | 5 | 0 | 0 | 0 | -1 |
| dn_oracle_1 | dn_oracle_1/grs | 0 | oracle | 0 | 10 | 1000 | 10 | 0 | 0 | 0 | -1 |
+————-+—————–+——-+——–+——–+——+——+———+————+———-+———+—————+

防止乱码配置,在server.xml里面配置:
<property name=”charset”>utf8</property>
从oracle迁移出来
在mycat命令行迁移出来
mysql -h127.0.0.1 -P8066 -usystem -pmysql -DTESTDB -s -N -e “select * from UC_USER” > /tmp/UC_USER.txt
导入mysql
在mycat命令行执行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;

 

在mycat平台上从oracle迁移到mysql中文正常
指导思想:
Mycat对于数据库而言就是客户端,一般来讲客户端字符编码与数据库服务端一致就不会有什么问题,或者是严格超集,
保证插入到数据库不因转码而丢失。客户端、系统终端、数据库三者字符庥得协调,否则不一致的话,会有字符集转换的问题,
有时候,显示正确的,但不一定在数据库就保存正确,或者保存到数据库的数据编码正确,没准显示为乱码。这就意味着,
在oracle里面是gbk或者latin1啥都没有关系,只要做好以下3点:
(1),在迁移到mysql里面,在mycat平台上从oracle导出时候字符集设置为utf8
(2),在mycat平台上导入到mysql字符集也要设置为utf8
(3), 然后在mycat平台上查看数据也要设置字符集为utf8
做好了以上3点,三而合一,这样就能正常显示中文了。
设定好utf8,–default-character-set=utf8:
mysql -h127.0.0.1 -P8066 -usystem -pmysql -DTESTDB -s -N –default-character-set=utf8 -e “select * from UC_USER” > /tmp/UC_USER_2.txt
导入的时候,在命令行也设定好utf8,set names utf8;
mysql> set names utf8;

2016-12-23 00:01:35.648 DEBUG [Timer0] (io.mycat.backend.jdbc.JDBCHeartbeat.heartbeat(JDBCHeartbeat.java:109)) – JDBCHeartBeat con query sql: select 1 from dual
2016-12-23 00:01:36.461 INFO [$_NIOREACTOR-0-RW] (io.mycat.net.handler.FrontendAuthenticator.success(FrontendAuthenticator.java:194)) – [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=4,host=10.0.1.134,port=9066,schema=TESTDB]’system’ login success
2016-12-23 00:01:36.465 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.net.FrontendConnection.query(FrontendConnection.java:288)) – [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=4,host=10.0.1.134,port=9066,schema=TESTDB] show databases
2016-12-23 00:01:36.465 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.manager.ManagerQueryHandler.query(ManagerQueryHandler.java:58)) – [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=4,host=10.0.1.134,port=9066,schema=TESTDB]show databases
2016-12-23 00:01:36.468 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.net.FrontendConnection.query(FrontendConnection.java:288)) – [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=4,host=10.0.1.134,port=9066,schema=TESTDB] show tables
2016-12-23 00:01:36.468 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.manager.ManagerQueryHandler.query(ManagerQueryHandler.java:58)) – [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=4,host=10.0.1.134,port=9066,schema=TESTDB]show tables
2016-12-23 00:01:36.469 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.net.FrontendConnection.query(FrontendConnection.java:288)) – [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=4,host=10.0.1.134,port=9066,schema=TESTDB] show @@heartbeat
2016-12-23 00:01:36.469 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.manager.ManagerQueryHandler.query(ManagerQueryHandler.java:58)) – [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=4,host=10.0.1.134,port=9066,schema=TESTDB]show @@heartbeat
2016-12-23 00:01:36.474 WARN [$_NIOREACTOR-0-RW] (io.mycat.net.NIOReactor$RW.run(NIOReactor.java:107)) – caught err:
java.lang.NullPointerException
at io.mycat.manager.response.ShowHeartbeat.getRows(ShowHeartbeat.java:138) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.manager.response.ShowHeartbeat.response(ShowHeartbeat.java:112) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.manager.handler.ShowHandler.handle(ShowHandler.java:135) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.manager.ManagerQueryHandler.query(ManagerQueryHandler.java:69) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.net.FrontendConnection.query(FrontendConnection.java:317) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.net.FrontendConnection.query(FrontendConnection.java:337) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.net.handler.FrontendCommandHandler.handle(FrontendCommandHandler.java:71) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.manager.ManagerConnection.handle(ManagerConnection.java:54) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.net.AbstractConnection.onReadData(AbstractConnection.java:321) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.net.NIOSocketWR.asynRead(NIOSocketWR.java:190) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.net.AbstractConnection.asynRead(AbstractConnection.java:273) ~[Mycat-server-1.6-RELEASE.jar:?]
at io.mycat.net.NIOReactor$RW.run(NIOReactor.java:102) ~[Mycat-server-1.6-RELEASE.jar:?]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
2016-12-23 00:01:36.477 INFO [$_NIOREACTOR-0-RW] (io.mycat.net.AbstractConnection.close(AbstractConnection.java:508)) – close connection,reason:program err:java.lang.NullPointerException ,[thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=4,host=10.0.1.134,port=9066,schema=TESTDB]

—-
INFO | jvm 1 | 2016/12/23 00:13:48 | Caused by: io.mycat.config.util.ConfigException: SelfCheck### schema scott refered by user scott is not exist!

Caused by: io.mycat.config.util.ConfigException: org.xml.sax.SAXParseException; lineNumber: 63; columnNumber: 16; The value of attribute “name” associated with an element type “schema” must not contain the ‘<‘ character.

INFO | jvm 1 | 2016/12/23 00:48:25 | 2016-12-23 00:48:25,238 [DEBUG][Timer0] JDBCHeartBeat con query sql: select 1 from dual (io.mycat.backend.jdbc.JDBCHeartbeat:JDBCHeartbeat.java:109)
INFO | jvm 1 | 2016/12/23 00:48:31 | 2016-12-23 00:48:31,482 [INFO ][$_NIOREACTOR-0-RW] [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=1,host=10.0.1.134,port=9066,schema=TESTDB]’system’ login success (io.mycat.net.handler.FrontendAuthenticator:FrontendAuthenticator.java:194)
INFO | jvm 1 | 2016/12/23 00:48:31 | 2016-12-23 00:48:31,485 [DEBUG][$_NIOREACTOR-0-RW] [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=1,host=10.0.1.134,port=9066,schema=TESTDB] show databases (io.mycat.net.FrontendConnection:FrontendConnection.java:288)
INFO | jvm 1 | 2016/12/23 00:48:31 | 2016-12-23 00:48:31,486 [DEBUG][$_NIOREACTOR-0-RW] [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=1,host=10.0.1.134,port=9066,schema=TESTDB]show databases (io.mycat.manager.ManagerQueryHandler:ManagerQueryHandler.java:58)
INFO | jvm 1 | 2016/12/23 00:48:31 | 2016-12-23 00:48:31,501 [DEBUG][$_NIOREACTOR-0-RW] [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=1,host=10.0.1.134,port=9066,schema=TESTDB] show tables (io.mycat.net.FrontendConnection:FrontendConnection.java:288)
INFO | jvm 1 | 2016/12/23 00:48:31 | 2016-12-23 00:48:31,502 [DEBUG][$_NIOREACTOR-0-RW] [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=1,host=10.0.1.134,port=9066,schema=TESTDB]show tables (io.mycat.manager.ManagerQueryHandler:ManagerQueryHandler.java:58)
INFO | jvm 1 | 2016/12/23 00:48:31 | 2016-12-23 00:48:31,504 [DEBUG][$_NIOREACTOR-0-RW] [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=1,host=10.0.1.134,port=9066,schema=TESTDB] select @@version_comment limit 1 (io.mycat.net.FrontendConnection:FrontendConnection.java:288)
INFO | jvm 1 | 2016/12/23 00:48:31 | 2016-12-23 00:48:31,504 [DEBUG][$_NIOREACTOR-0-RW] [thread=$_NIOREACTOR-0-RW,class=ManagerConnection,id=1,host=10.0.1.134,port=9066,schema=TESTDB]select @@version_comment limit 1 (io.mycat.manager.ManagerQueryHandler:ManagerQueryHandler.java:58)

INFO | jvm 1 | 2016/12/23 00:54:03 | 2016-12-23 00:54:03,688 [ERROR][Timer1] JDBCHeartBeat error java.sql.SQLException: Listener refused the connection with the following error:
INFO | jvm 1 | 2016/12/23 00:54:03 | ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
INFO | jvm 1 | 2016/12/23 00:54:03 | The Connection descriptor used by the client was:
INFO | jvm 1 | 2016/12/23 00:54:03 | 10.0.1.103:1521:ORACLEDB

[mysql@hongquan ~]$ mysql -uroot -proot -P9066 -h10.0.1.134 -Dgrs
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6-RELEASE-20161010173036 MyCat Server (monitor)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> show @@databases;
+———-+
| DATABASE |
+———-+
| TESTDB |
| grs |
+———-+
2 rows in set (0.00 sec)

继续浏览有关 数据库技术文章/教程 的文章
发表评论