mongodb副本集

教程发布:风哥 教程分类:ITPUX技术网 更新日期:2022-02-12 浏览学习:341

本帖最后由 love19791125 于 2015-12-30 13:36 编辑

[color=rgb(46, 46, 46)][size=7.5pt]准备3台台机器[color=rgb(46, 46, 46)][size=7.5pt] 192.168.10.92[color=rgb(46, 46, 46)][size=7.5pt]、192.168.10.93、192.168.10.94
[color=rgb(46, 46, 46)][size=7.5pt]-------------------------------------------------[color=rgb(46, 46, 46)][size=7.5pt]1.[color=rgb(46, 46, 46)][size=7.5pt]分别在每台机器上建立mongodb[color=rgb(46, 46, 46)][size=7.5pt]解压mongodb[size=7.5pt]tar zxvf mongodb-linux-x86_64-2.6.0.tgz[size=7.5pt]mv mongodb-linux-x86_64-2.6.0/usr/local/mongodb[color=rgb(46, 46, 46)][size=7.5pt]2.[color=rgb(46, 46, 46)][size=7.5pt]配置环境变量[color=rgb(46, 46, 46)][size=7.5pt]echo 'exportPATH=$PATH:/usr/local/mongodb/bin' >> /etc/profile[color=rgb(46, 46, 46)][size=7.5pt]source /etc/profile[color=rgb(46, 46, 46)][size=7.5pt]ldconfig[size=7.5pt]3.[size=7.5pt]存放mongodb数据日志文件[color=rgb(46, 46, 46)][size=7.5pt]mkdir -p /usr/local/mongodb19999/{etc,date,log}[color=rgb(46, 46, 46)][size=7.5pt]cd /usr/local/mongodb/log[color=rgb(46, 46, 46)][size=7.5pt]touch mongodb.log[size=7.5pt]4.[size=7.5pt]分别在每台机器上启动mongodb(1).命令启动:/usr/local/mongodb/bin/mongod--fork --dbpath=/usr/local/mongodb/date--logpath=/usr/local/mongodb/log/mongodb.log --logappend --replSet repset(2).开机启动[color=rgb(46, 46, 46)][size=7.5pt]建立在/etc/init.d/mongodb(启动文件)#!/bin/sh
### BEGIN INIT INFO# Provides: mongodb# Required-Start:# Required-Stop:# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: mongodb# Description: mongo db server### END INIT INFO
. /etc/init.d/functions
PROGRAM=/usr/local/mongodb/bin/mongodMONGOPID=`ps -ef | grep 'mongod' | grep -vgrep | awk '{print $2}'`
test -x $PROGRAM || exit 0
case "$1" in start) ulimit -n 65535 echo "Starting MongoDB server" $PROGRAM -f /usr/local/mongodb/etc/mongod.conf --replSet repset ;; stop) echo "Stopping MongoDB server" if [ ! -z "$MONGOPID" ]; then kill -15 $MONGOPID fi ;; status) ;; *) log_success_msg "Usage: /etc/init.d/mongodb{start|stop|status}" exit 1esac
exit 0[color=rgb(46, 46, 46)][size=7.5pt]chmod 777 /etc/init.d/mongodb[color=rgb(46, 46, 46)][size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]建立/usr/local/mongodb/etc/mongodb.conf# mongod.conf
#where to loglogpath=/usr/local/mongodb/log/mongodb.log
logappend=true
# fork and run in backgroundfork=true
port=27017
dbpath=/usr/local/mongodb/date
# location of pidfilepidfilepath=/usr/local/mongodb/mongod.pid
# Listen to local interface only. Commentout to listen on all interfaces. #bind_ip=xx
# Disables write-ahead journaling# nojournal=true
# Enables periodic logging of CPUutilization and I/O wait#cpu=true
# Turn on/off security. Off is currently the default#noauth=true#auth=true
# Verbose logging output.#verbose=true
# Inspect all client data for validity onreceipt (useful for# developing drivers)#objcheck=true
# Enable db quota management#quota=true
# Set oplogging level where n is# 0=off (default)# 1=W# 2=R# 3=both# 7=W+some reads#diaglog=0
# Ignore query hints#nohints=true
# Enable the HTTP interface (Defaults toport 28017).#httpinterface=true
# Turns off server-side scripting. This will result in greatly limited# functionality#noscripting=true
# Turns off table scans. Any query that would do a table scan fails.#notablescan=true
# Disable data file preallocation.#noprealloc=true
# Specify .ns file size for new databases.# nssize=
# Replication Options
# in replicated mongo databases, specifythe replica set name here#replSet=setname# maximum size in megabytes for replicationoperation log#oplogSize=1024# path to a key file storing authenticationinfo for connections# between replica set members#keyFile=/path/to/keyfile[color=rgb(46, 46, 46)][size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]chkconfig mongodb on[color=rgb(46, 46, 46)][size=7.5pt]chkconfig --level 2345 mongodb on[color=rgb(46, 46, 46)][size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]5.[color=rgb(46, 46, 46)][size=7.5pt]服务启动[color=rgb(46, 46, 46)][size=7.5pt]service mongodb start[color=rgb(46, 46, 46)][size=7.5pt]service mongodb stop[color=rgb(46, 46, 46)][size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]6.[color=rgb(46, 46, 46)][size=7.5pt]初始化副本集[color=rgb(46, 46, 46)][size=7.5pt]在三台机器上任意一台机器登陆mongodb[color=rgb(46, 46, 46)][size=7.5pt]运行mongo
1613
[color=#2e2e2e][size=7.5pt]使用[color=#2e2e2e][size=7.5pt]admin[color=#2e2e2e][size=7.5pt]数据库[color=#2e2e2e][size=7.5pt]1614
[size=7.5pt]#[size=7.5pt]定义副本集配置变量,这里的 _id:”[size=7.5pt]repset[size=7.5pt]”[size=7.5pt]和上面命令参数“ –replSet [size=7.5pt]repset[size=7.5pt]”[size=7.5pt]要保持一样[size=7.5pt]输入一下内容:注意IP和端口换成你自己的config = { _id:"repset",members:[{_id:0,host:"192.168.10.92:27017"},{_id:1,host:"192.168.10.93:27017"},}[color=rgb(46, 46, 46)][size=7.5pt] [size=7.5pt]#[size=7.5pt]输出{ "_id" : "repset", "members" : [ { "_id" : 0, "host" :"192.168.10.92:27017" }, { "_id" : 1, "host" :"192.168.10.93:27017" }, { "_id" : 2, "host" :"192.168.10.94:27017" }}[size=7.5pt] [size=7.5pt]#[size=7.5pt]初始化副本集配置rs.initiate(config);[size=7.5pt] [size=7.5pt]#[size=7.5pt]输出成功{ "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1}[color=rgb(46, 46, 46)][size=7.5pt] [size=7.5pt]#[size=7.5pt]查看日志,副本集启动成功[size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]查看集群节点的状态 rs.status();[size=7.5pt]#[size=7.5pt]输出
[size=7.5pt]1611
[size=7.5pt]整个副本集已经搭建成功了。[color=rgb(46, 46, 46)][size=7.5pt]#[color=rgb(46, 46, 46)][size=7.5pt]在主节点PRIMARY机器上连接到终端:mongo 127.0.0.1[color=rgb(46, 46, 46)][size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]#[color=rgb(46, 46, 46)][size=7.5pt]建立test数据库。use test;[color=rgb(46, 46, 46)][size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]往testdb表插入数据。> db.testdb.insert({"test1":"testval1"})[color=rgb(46, 46, 46)][size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]#[color=rgb(46, 46, 46)][size=7.5pt]在副本节点SECONDARY上连接到mongodb查看数据是否复制过来。[color=rgb(46, 46, 46)][size=7.5pt]mongo 192.168.10.94:27017 [color=rgb(46, 46, 46)][size=7.5pt]或mongo 192.168.10.94:27017[color=rgb(46, 46, 46)][size=7.5pt] [color=rgb(46, 46, 46)][size=7.5pt]#[color=rgb(46, 46, 46)][size=7.5pt]使用test数据库。repset:SECONDARY> use test;[color=rgb(46, 46, 46)][size=7.5pt] repset:SECONDARY> show tables;[color=rgb(46, 46, 46)][size=7.5pt]输出Sun Dec 29 21:50:48.590 error: { "$err" : "not master and slaveOk=false","code" :13435 } at src/mongo/shell/query.js:128#mongodb默认是从主节点读写数据的,副本节点上不允许读,需要设置副本节点可以读。repset:SECONDARY>db.getMongo().setSlaveOk();
#可以看到数据已经复制到了副本集。repset:SECONDARY> db.testdb.find();12#输出{ "_id" :ObjectId("52c028460c7505626a93944f"), "test1" :"testval1" }
先停掉主节点,查看从节点的日志可以看到经过一系列的投票选择操作,其中1台当选主节点,其他节点会同步数据过来。查看整个集群的状态,可以看到停掉的服务器为状态不可达。rs.status();
1612
再启动原来的主节点,原来的主节点变为 SECONDARY。java程序连接副本集测试。三个节点有一个节点挂掉也不会影响应用程序客户端对整个副本集的读写!public class TestMongoDBReplSet {
public static void main(String[]args) {
try { Listaddresses = new ArrayList(); ServerAddressaddress1 = new ServerAddress("192.168.10.92" , 27017); ServerAddressaddress2 = new ServerAddress("192.168.10.93" , 27017); ServerAddressaddress3 = new ServerAddress("192.168.10.94" , 27017); addresses.add(address1); addresses.add(address2); addresses.add(address3); MongoClientclient = new MongoClient(addresses); DBdb = client.getDB( "test"); DBCollectioncoll = db.getCollection( "testdb"); // 插入 BasicDBObjectobject = new BasicDBObject(); object.append("test2", "testval2" ); coll.insert(object);DBCursor dbCursor = coll.find();while (dbCursor.hasNext()) {DBObject dbObject = dbCursor.next();System. out.println(dbObject.toString()); } } catch (Exception e) { e.printStackTrace(); } }}

常规写操作来说并没有读操作多,所以一台主节点负责写,两台副本节点负责读。1、设置读写分离需要先在副本节点SECONDARY 设置 setSlaveOk。
2、在程序中设置副本节点负责读操作,如下代码:
public class TestMongoDBReplSetReadSplit {
public static void main(String[]args) { try { Listaddresses = new ArrayList(); ServerAddress address1 =new ServerAddress("192.168.10.92" , 27017); ServerAddress address2 =new ServerAddress("192.168.10.93" , 27017); ServerAddress address3 =new ServerAddress("192.168.10.94" , 27017); addresses.add(address1); addresses.add(address2); addresses.add(address3); MongoClient client= new MongoClient(addresses); DB db = client.getDB("test" ); DBCollection coll =db.getCollection( "testdb" ); BasicDBObjectobject = new BasicDBObject(); object.append("test2" , "testval2" ); coll.insert(object); //读操作从副本节点读取 ReadPreference preference =ReadPreference. secondary(); DBObject dbObject =coll.findOne(object, null , preference); System. out .println(dbObject); } catch (Exceptione) { e.printStackTrace(); }}}读参数除了secondary一共还有五个参数:primary、primaryPreferred、secondary、secondaryPreferred、nearest。[color=rgb(46, 46, 46)][size=7.5pt]primary:[color=rgb(46, 46, 46)][size=7.5pt]默认参数,只从主节点上进行读取操作;[color=#2E2E2E][size=7.5pt]
primaryPreferred:[color=rgb(46, 46, 46)][size=7.5pt]大部分从主节点上读取数据,只有主节点不可用时从secondary节点读取数据。[color=#2E2E2E][size=7.5pt]
secondary:[color=rgb(46, 46, 46)][size=7.5pt]只从secondary节点上进行读取操作,存在的问题是secondary节点的数据会比primary节点数据“旧”。[color=#2E2E2E][size=7.5pt]
secondaryPreferred:[color=rgb(46, 46, 46)][size=7.5pt]优先从secondary节点进行读取操作,secondary节点不可用时从主节点读取数据;[color=#2E2E2E][size=7.5pt]
nearest:[color=rgb(46, 46, 46)][size=7.5pt]不管是主节点、secondary节点,从网络延迟最低的节点上读取数据。

本文标签:
本文标题:mongodb副本集
网站声明:本文由风哥整理发布,转载请保留此段声明,本站所有内容将不对其使用后果做任何承诺,请读者谨慎使用!
【上一篇】
【下一篇】