tungsten
#########################
Tungsten Replicator的基本支持
一、系统要求
ruby 1.8.5 or later (ruby --version)
Ruby openssl libraries ( echo "p 'OK'" | ruby -ropenssl )
Java 1.6 or later (java -version)
GNU tar (tar --version)
rsync 并启动
二、用户权限要求
1、新建用户tungsten,且属于mysql组,这样tugsten才有权限读取MYSQL binlog目录
useradd tungsten -g mysql
2、用户必须能对所有服务器有SSH权限,且无需要密码。可通过 ssh-agent来完成
使用tungsten实现mysql-->mongodb的同步
搭建tungsten-replicator
master端(mysql安装略):
修改mysql的配置文件(/home/work/mysql_3306/conf/my.cnf)
log-bin=mysql-bin server-id=1 #(注意这里一定要server-id,而不是server_id) open_files_limit = 65535 max_allowed_packet=52m innodb_flush_lot_at_trx_commit=2 sync_binlog=1 binlog_checksum=none character-set-server=utf8 collation-server=utf8_general_ci innodb_flush_method=O_DIRECT
创建用于replicator连接mysql的用户:
grant all privileges on *.* to tungsten@'%' identified by '123456' with grant option;
创建replicator:
tar -zxvf tungsten-replicator-3.0.0-524.tar.gz
cd tungsten-replicator-3.0.0-524
./tools/tpm install alpha \
--master=192.168.1.111 \
--datasource-mysql-conf=/home/work/mysql_3306/conf/my.cnf \
--install-directory=/home/work/tungsten \
--replication-user=tungsten \
--replication-password=123456 \
--replication-port=3306 \
--enable-heterogenous-master=true \
--property=replicator.filter.pkey.addColumnsToDeletes=true \
--property=replicator.filter.pkey.addPkeyToInserts=true \
--start
slave端(mongodb)
安装mongodb
创建replicator
./tools/tpm install alpha \
--datasource-type=mongodb \
--install-directory=/home/work/tungsten \
--master=192.168.1.111 \
--members=192.168.1.222 \
--enable-heterogenous-slave=true \
--topology=master-slave \
--start=true
ssh等效配置:
主:
ssh-keygen -t rsa
ssh-keygen -t dsa
备:
ssh-keygen -t rsa
ssh-keygen -t dsa
在主上work用户执行以下操作
cat ~/.ssh/id_rsa.pub >> ./.ssh/authorized_keys
cat ~/.ssh/id_dsa.pub >> ./.ssh/authorized_keys
ssh 192.168.1.111 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
ssh 192.168.1.222 cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys 192.168.1.222:~/.ssh/authorized_keys
有时会出现有一个节点能不使用密码连上第二个节点,但另一个节点却不行:
chmod -R 700 ~/.ssh
三、网络要求
1、所有用户可以通过hostname
hostname --ip-address必须返回一个IP(如果用IP方式可以不用配置host)
2、程序应用对外开放接口
3306 (MySQL database)
2112 (Tungsten THL)
10000 (Tungsten RMI)
10001 (JMX management)
四、数据库要求
1、必须支持bin-log,且不同server有不同的server-id
设置MYSQL支持bin-log
修改配置文件my.cnf
点击(此处)折叠或打开
- [mysqld]
- # Master replication settings.
- log-bin=mysql-bin
- server-id=1
- max_allowed_packet=48m
- innodb_flush_lot_at_trx_commit=2
- sync_binlog=1
注:
default-storage-engine 必须为innodb引擎
innodb_buffer_pool_size 512M为最低设置
max_allowed_packet最低设置为16M,建议48M。如果设置的较小,复制过程中将报错 "Packet for query is too large"
innodb_flush_lot_at_trx_commit=2 and sync_binlog=1 ,此设置是为防止BinLog崩溃,如未设置,install过程中会报warn
之后重新启动MYSQL
binlog-ignore-db = tungsten_tungsten
执行tungsten会往数据库里写数据,所所加上这句。不让这句写入binlog ;tungsten_服务名
2、数据库必须支持innodb
默认搜索引擎要设置为innodb
MYSQL5.1设置为,修改配置文件my.cnf
default-table-type=innodb
MYSQL5.5默认引擎为innodb,无需设置
3、必须有能所有SERVER都有权限的用户,可通过mysql -u USER -pPASSWORD -P PORT -h HOST 连接
创建Tungsten Replicator数据用户与数据库,
点击(此处)折叠或打开
- mysql> grant all on *.* to root@'hostIP' identified by 'password' with grant option;
- mysql> grant all on *.* to tungsten@'hostIP' identified by 'password' with grant option;
注:此处除了master与slave要相互有权限之外,还要对自己的localhost有权限;
Tungsten Replicator的安装(Tungsten Replicator 2.0.4 及2.0.6亦可)
一、下载Tungsten Replicator
点击(此处)折叠或打开
wget http://tungsten-replicator.googlecode.com/files/tungsten-replicator-2.0.4.tar.gz
tar zxvf tungsten-replicator-2.0.4.tar.gz
mv tungsten-replicator-2.0.4 /usr/local/tungsten
chown -R tungsten /usr/local/tungsten
二、用tungsten-installer安装master-slave模式
可通过./tools/tungsten-installer --help-all 查看所有参数
datasource-user与password 要有权限均可以master与slave中执行MYSQL权限
service-name可以随意取名
home-directory为tungsten目录
start-and-report 为配置并启动,用这个参数执行后会自动启动replicator,并且conf中写入services等相关配置
点击(此处)折叠或打开
--master-host=masterhost
--datasource-user=tungsten
--datasource-password=badpwd
--datasource-port=3306
--cluster-hosts=masterhost ,slavehost
--service-name=tungsten
--home-directory=/usr/local/tungsten
--start-and-report
执行过程中,可能会出现SSH权限、MYSQL权限报错、mysqldump路径报错等;
如果两个HOST的SSH端口不是默认的22,要加入参数--net-ssh-option=port=19000
执行中会输出 大概如下内容
点击(此处)折叠或打开
- ...
- Processing services command...
- NAME VALUE
- ---- -----
- appliedLastSeqno: -1
- appliedLatency : -1.0
- role : slave
- serviceName : tungsten
- serviceType : local
- started : true
- state : ONLINE
- Finished services command...
只要SSH互通,slave暂时不需设置。
执行完成后,会同时在masterslave两台上出现tungsten_tungsten数据库、同时有replicator进程,且打开10000端口;
查看tungsten_tungsten数据库
点击(此处)折叠或打开
+-----------------------------+
| Tables_in_tungsten_tungsten |
+-----------------------------+
| consistency |
| heartbeat |
| trep_commit_seqno |
| trep_shard |
+-----------------------------+
4 rows in set (0.00 sec)
其中trep_commit_seqno 表会一直记录最新的binlog和最新的posttion
点击(此处)折叠或打开
- mysql> select * from trep_commit_seqnoG;
- *************************** 1. row ***************************
- task_id: 0
- seqno: 0
- fragno: 0
- last_frag: 1
- source_id: 192.168.0.12
- epoch_number: 0
- eventid: mysql-bin.000039:0000000000000427;140
- applied_latency: 0
- update_timestamp: 2013-02-18 15:28:12
- shard_id: tungsten_tungsten
- extract_timestamp: 2013-02-18 15:28:12
- 1 row in set (0.00 sec)
更新数据内容后重新执行,查看数据ID已发生变化 ,且slave端数据库已和master数据同步;
Tungsten Replicator的备份与恢复功能
Tungsten Replicator提供了简单的备份与恢复的功能 ,使用了两个简单的plugins(mysqldump and xtrabackup)
配置文件中已经默认配置了BackUp相关功能,可做相应修改
配置文件位置为/usr/local/tungsten/tungsten-replicator/conf/,列出如下几个重要参数
点击(此处)折叠或打开
- less tungsten-replicator/conf/static-tungsten.properties |grep backup
- replicator.backup.default=mysqldump
- ...
- replicator.backup.agent.mysqldump.mysqldumpOptions=--opt --all-databases --add-drop-database
- replicator.storage.agent.fs.directory=/usr/local/tungsten/backups/tungsten
- replicator.backup.agent.mysqldump.hotBackupEnabled=true
- replicator.backup.agent.xtrabackup.hotBackupEnabled=true
- replicator.backup.agent.xtrabackup.options=user=${replicator.global.db.user}&password=${replicator.global.db.password}&host=${replicator.global.db.host}&port=${replicator.global.db.port}&directory=/tmp/innobackup&archive=/tmp/innobackup.tar&mysqldatadir=/var/lib/mysql/&mysql_service_command=/etc/init.d/mysql
可在配置文件中加入replicator.auto_backup=true,设置自动备份
注:备份与恢复 都要在offline的情况下执行,状态为“OFFLINE:NORMAL”,如不正常情况会出现“OFFLINE:ERROR”,则会恢复失败
查看Replicator当前状态
- ./tungsten-replicator/bin/trepctl status
- ...
- state : ONLINE
设置为offline状态,并备份与恢复
- ./tungsten-replicator/bin/trepctl status
- ...
- state : OFFLINE:NORMAL
- Finished status command...
- # ./tungsten-replicator/bin/trepctl backup
- Backup completed successfully; URI=storage://file-system/store-0000000003.properties
- # ./tungsten-replicator/bin/trepctl restore -uri storage://file-system/store-0000000003.properties
- Restore is pending; check log for status
查看恢复后的文件,根据conf/static-tungsten.properties配置下的备份目录查看
- # ls /usr/local/tungsten/backups/tungsten
- storage.index store-0000000002-mysqldump-1871676336962858846.sql store-0000000003.properties
- store-0000000001-mysqldump-341047084963524507.sql store-0000000002.properties
- store-0000000001.properties store-0000000003-mysqldump-4915067303150219872.sql
在Master/Slave模式下,新加Slave
在正常Master/Slave模式下,如果有其中一台Slave坏掉,不影响正常的Tungsten-Replicator运行,但如果 Master失败,则要重新都offline,然后保证数据一致性;
正常要加Slave,要同时offline 数据一致后,重新online,但因为已经有一台Slave,则可以避免Master的OFFLINE;思路为用一台Slave做一台新Slave的供体,保证两台数据一致;
1、设置slave1 为offline状态 ,然后备份数据,然后再online,online后,会自动同步offline的过程中产生的master的数据
- trepctl -host slave1 offline
- trepctl -host slave1 backup
- trepctl -host slave1 online
2、在新的Slave上恢复数据,然后online
- trepstart
- trepctl -host newslave restore
- trepctl -host newslave online
###########################