本文为一个动手实验,配置传统复制模式中的单表复制(非复制整库),配置2个数据库, master和一个subscriber。拓扑如下:
为简化,master和subscriber位于同一主机。同时,为和上一个实验保持一致,master和subscriber的DSN分别为master1和subscriber1。
创建DSN
[ODBC Data Sources]
master1=TimesTen 11.2.2 Driver
subscriber1=TimesTen 11.2.2 Driver
[master1]
Driver=/home/oracle/TimesTen/tt1122/lib/libtten.so
DataStore=/tmp/master1
DatabaseCharacterSet=AL32UTF8
ConnectionCharacterSet=AL32UTF8
[subscriber1]
Driver=/home/oracle/TimesTen/tt1122/lib/libtten.so
DataStore=/tmp/subscriber1
DatabaseCharacterSet=AL32UTF8
ConnectionCharacterSet=AL32UTF8
在master中创建表和复制
$ ttisql master1
Command>
CREATE TABLE employees
( employee_id NUMBER(6) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25) NOT NULL UNIQUE,
phone_number VARCHAR2(20),
hire_date DATE NOT NULL,
job_id VARCHAR2(10) NOT NULL,
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
manager_id NUMBER(6),
department_id NUMBER(4)
) ;
CREATE REPLICATION tablerep
ELEMENT e TABLE employees
MASTER master1
SUBSCRIBER subscriber1;
Command> repschemes;
Replication Scheme ORACLE.TABLEREP:
Element: E
Type: Table ORACLE.EMPLOYEES
Master Store: MASTER1 on TIMESTEN-HOL Transmit Durable
Subscriber Store: SUBSCRIBER1 on TIMESTEN-HOL
Store: MASTER1 on TIMESTEN-HOL
Port: (auto)
Log Fail Threshold: (none)
Retry Timeout: 120 seconds
Compress Traffic: Disabled
Store: SUBSCRIBER1 on TIMESTEN-HOL
Port: (auto)
Log Fail Threshold: (none)
Retry Timeout: 120 seconds
Compress Traffic: Disabled
1 replication scheme found.
CREATE REPLICATION创建一个classic replication scheme。
ELEMENT可以是整库,也可以是一张表
在subscriber上创建表和复制
执行的步骤与上一步完全相同
$ ttisql subscriber1
Command>
CREATE TABLE employees
( employee_id NUMBER(6) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25) NOT NULL UNIQUE,
phone_number VARCHAR2(20),
hire_date DATE NOT NULL,
job_id VARCHAR2(10) NOT NULL,
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
manager_id NUMBER(6),
department_id NUMBER(4)
) ;
CREATE REPLICATION tablerep
ELEMENT e TABLE employees
MASTER master1
SUBSCRIBER subscriber1;
```
## 在主库和复制库中都启动复制代理
Command> call ttrepstart;
Command> call ttrepstateget;
< IDLE, NO GRID >
1 row found.
两个数据库的状态都是IDLE,因为其不是ASP
## 在master库中插入数据
注意,由于我们定义的是单向复制,因此subscriber是只读的,如果插入数据会报错:
8151: ORACLE.EMPLOYEES’s replication role disallows the requested operation
The command failed.
我们仍回到主库中插入数据, 并确认数据已成功复制到subscriber:
$ ttisql master1
Command>
INSERT INTO employees VALUES
( 202,
‘Pat’,
‘Fay’,
‘PFAY’,
‘603-123-7777’,
TO_DATE(‘17-AUG-1997’, ‘dd-MON-yyyy’),
‘MK_REP’,
6000,
NULL,
201,
20
);
$ ttisql -v1 subscriber1
Command> select * from employees;
< 202, Pat, Fay, PFAY, 603-123-7777, 1997-08-17 00:00:00, MK_REP, 6000, , 201, 20 >
## 监控复制环境
Command> repschemes;
Replication Scheme ORACLE.TABLEREP:
Element: E
Type: Table ORACLE.EMPLOYEES
Master Store: MASTER1 on TIMESTEN-HOL Transmit Durable
Subscriber Store: SUBSCRIBER1 on TIMESTEN-HOL
Store: MASTER1 on TIMESTEN-HOL
Port: (auto)
Log Fail Threshold: (none)
Retry Timeout: 120 seconds
Compress Traffic: Disabled
Store: SUBSCRIBER1 on TIMESTEN-HOL
Port: (auto)
Log Fail Threshold: (none)
Retry Timeout: 120 seconds
Compress Traffic: Disabled
使用ttstatus监控数据库状态, 可以看到复制代理已经启动
$ ttstatus
TimesTen status report as of Mon May 30 17:49:26 2016
Daemon pid 2652 port 53392 instance tt1122
TimesTen server pid 2661 started on port 53393
Data store /tmp/subscriber1
There are 15 connections to the data store
Shared Memory KEY 0x0200c902 ID 2392080
PL/SQL Memory KEY 0x0300c902 ID 2424849 Address 0x7fa0000000
Type PID Context Connection Name ConnID
Replication 4006 0x0000000004017480 LOGFORCE:1089546560 129
Replication 4006 0x0000000004080e60 REPHOLD:1104329024 130
Replication 4006 0x00000000040d5a90 REPLISTENER:1085208896 131
Replication 4006 0x0000000004161f20 RECEIVER:1098897728 128
Subdaemon 2656 0x000000000157f360 Manager 142
Subdaemon 2656 0x00000000015f6430 Rollback 141
Subdaemon 2656 0x00000000016d5c80 Flusher 140
Subdaemon 2656 0x000000000175d610 HistGC 139
Subdaemon 2656 0x00000000017b2240 Deadlock Detector 138
Subdaemon 2656 0x0000000001806e70 AsyncMV 137
Subdaemon 2656 0x000000000185baa0 IndexGC 136
Subdaemon 2656 0x00000000018b06d0 Aging 135
Subdaemon 2656 0x0000000001905300 Log Marker 134
Subdaemon 2656 0x0000000001959f30 Checkpoint 133
Subdaemon 2656 0x00000000019aeb60 Monitor 132
Replication policy : Manual
Replication agent is running.
Cache Agent policy : Manual
PL/SQL enabled.
Data store /tmp/master1
There are 15 connections to the data store
Shared Memory KEY 0x0600ce42 ID 2457618
PL/SQL Memory KEY 0x0700ce42 ID 2490387 Address 0x7fa0000000
Type PID Context Connection Name ConnID
Replication 4034 0x000000000334d430 LOGFORCE:1101445440 129
Replication 4034 0x0000000003393530 REPLISTENER:1107417408 131
Replication 4034 0x00000000033b6ea0 REPHOLD:1093810496 130
Replication 4034 0x00000000034acb10 TRANSMITTER(M):1110567232 128
Subdaemon 2658 0x00000000006fe360 Manager 142
Subdaemon 2658 0x0000000000775430 Rollback 141
Subdaemon 2658 0x000000000083ce50 Flusher 140
Subdaemon 2658 0x0000000000851b60 Monitor 138
Subdaemon 2658 0x00000000008a69b0 Checkpoint 134
Subdaemon 2658 0x00000000008bb5a0 Aging 137
Subdaemon 2658 0x00000000008d0190 Log Marker 136
Subdaemon 2658 0x00000000008e4ea0 AsyncMV 135
Subdaemon 2658 0x000000000097a070 IndexGC 133
Subdaemon 2658 0x00007f7114021670 HistGC 132
Subdaemon 2658 0x00007f711c0008c0 Deadlock Detector 139
Replication policy : Manual
Replication agent is running.
Cache Agent policy : Manual
PL/SQL enabled.
Accessible by group oracle
End of report
$ ttrepadmin -showconfig master1
Self host “TIMESTEN-HOL”, port auto, name “MASTER1”, LSN 0/16414984, timeout 120, threshold 0
List of subscribers
Peer name Host name Port State Proto Track
SUBSCRIBER1 TIMESTEN-HOL Auto Start 36 0
Last Msg Sent Last Msg Recv Latency TPS RecordsPS
00:00:02 - -1.00 -1 -1
List of objects and subscriptions
Table details
Table : ORACLE.EMPLOYEES Timestamp updates : -
Master Name Subscriber name
———– —————
MASTER1 SUBSCRIBER1
$ ttrepadmin -showstatus master1
Replication Agent Status as of: 2016-05-30 18:01:18
DSN : master1
Process ID : 4034 (Started)
Replication Agent Policy : manual
Host : TIMESTEN-HOL
RepListener Port : 44907 (AUTO)
Last write LSN : 0.16462088
Last LSN forced to disk : 0.16461824
Replication hold LSN : 0.16455944
Replication Peers:
Name : SUBSCRIBER1
Host : TIMESTEN-HOL
Port : 50197 (AUTO) (Connected)
Replication State : STARTED
Communication Protocol : 36
TRANSMITTER thread(s):
For : SUBSCRIBER1 (track 0)
Start/Restart count : 1
Send LSN : 0.16460040
Transactions sent : 1
Total packets sent : 183
Tick packets sent : 176
MIN sent packet size : 64
MAX sent packet size : 1691
AVG sent packet size : 73
Last packet sent at : 18:01:13
Total Packets received: 182
MIN rcvd packet size : 64
MAX rcvd packet size : 120
AVG rcvd packet size : 118
Last packet rcvd’d at : 18:01:13
TXNs Allocated : 2
TXNs In Use : 0
ACTs Allocated : 1
ACTs In Use : 0
ACTs Data Allocated : 0
Most recent errors (max 5):
TT16025 in repagent.c (line 1227) at 17:47:40 on 05-30-2016
TT16285 in transmitter.c (line 1020) at 17:47:40 on 05-30-2016
TT16999 in transmitter.c (line 1340) at 17:47:40 on 05-30-2016
$ ttRepAdmin -log master1
1 log file retained by replication
[[email protected] ~]$ ttRepAdmin -self -list master1
Self host “TIMESTEN-HOL”, port auto, name “MASTER1”, LSN 0/16507144
Operation successful
[[email protected] ~]$ ttRepAdmin -self -list subscriber1
Self host “TIMESTEN-HOL”, port auto, name “SUBSCRIBER1”, LSN -1/-1
Operation successful
[[email protected] ~]$ ttrepadmin -receiver -list master1
Peer name Host name Port State Proto Track
SUBSCRIBER1 TIMESTEN-HOL Auto Start 36 0
Last Msg Sent Last Msg Recv Latency TPS RecordsPS Logs
00:00:04 - -1.00 -1 -1 1
$ ttRepAdmin -wait -name master1 subscriber1
Replication has caught up after 0 seconds
$ ttRepAdmin -wait -name subscriber1 master1
Timed out after 0 seconds waiting for replication to catch up
[[email protected] ~]ttRepAdmin?bookmarkmaster1ReplicationholdLSN……0/16855304LastwrittenLSN……….0/16865544LastLSNforcedtodisk…0/16865280[oracle@timesten?hol ] ttRepAdmin -bookmark master1
Replication hold LSN …… 0/16865544
Last written LSN ………. 0/16869640
Last LSN forced to disk … 0/16869376
[[email protected] ~]$ ttRepAdmin -bookmark master1
Replication hold LSN …… 0/16865544
Last written LSN ………. 0/16871688
Last LSN forced to disk … 0/16871424
## 清理复制环境
包括停止复制代理,删除复制关系和删除表。
在所有数据库中执行:
CALL ttRepStop;
DROP REPLICATION tablerep;
DROP TABLE employees;
最后删除数据库:
ttdestroymasterds ttdestroy subscriberds
“`
总结
- TimesTen的Classic Replication(传统复制)是以element为基础复制单元的,element既可以是单个对象如表,也可以是整个库,如datastore。
- 传统复制可以是单向的,也可以是双向的。单向复制时,subscriber端只读;双向复制时,两个库都可以写
- 传统复制需要在两个库中建立相同的schema。而在ASP复制中,只需在master中建schema,然后克隆出standby和subscriber。
- 复制的表需要主键和唯一索引,这和缓存组的要求是一样的。
- 复制中每一个库都需要启动一个对应的复制代理