点击查看原文
对于mongodb的相关介绍,在此不多累赘,如需了解请见:
http://www.mongodb.org/display/DOCS/Home
mongod1 : deploy ip : 10.12.7.107 port : 27031 $ mkdir -p /data/db/0 ./mongod --dbpath /data/db/0 --port 27031 --replSet myset
mongod2 : doc ip : 10.12.7.108 port : 27032 $ mkdir -p /data/db/1 ./mongod --dbpath /data/db/1 --port 27032 --replSet myset
mongod3 : deploy ip : 10.12.7.107 port : 27033 $ mkdir -p /data/db/2 ./mongod --dbpath /data/db/2 --port 27033 --replSet myset
> config = {_id: 'myset', members: [ {_id: 0, host: '10.12.7.107:27031'}, {_id: 1, host: '10.12.7.108:27032'}, {_id: 2, host: '10.12.7.107:27033', arbiterOnly: true}]} > rs.initiate(config) > rs.conf() #查看配置信息 > rs.staus()
> use test switched to db test > db.users.insert({name:"terrylc"})
#27031 Tue Oct 18 08:44:07 [FileAllocator] allocating new datafile /data/db/0/test.ns, filling with zeroes... Tue Oct 18 08:44:07 [FileAllocator] done allocating datafile /data/db/0/test.ns, size: 16MB, took 0.054 secs Tue Oct 18 08:44:07 [FileAllocator] allocating new datafile /data/db/0/test.0, filling with zeroes... Tue Oct 18 08:44:07 [FileAllocator] done allocating datafile /data/db/0/test.0, size: 64MB, took 0.153 secs Tue Oct 18 08:44:07 [FileAllocator] allocating new datafile /data/db/0/test.1, filling with zeroes... Tue Oct 18 08:44:07 [conn3] building new index on { _id: 1 } for test.users Tue Oct 18 08:44:07 [conn3] done for 0 records 0secs Tue Oct 18 08:44:07 [conn3] insert test.users 213ms Tue Oct 18 08:44:09 [FileAllocator] done allocating datafile /data/db/0/test.1, size: 128MB, took 1.527 secs #27032 Tue Oct 18 23:43:02 [FileAllocator] allocating new datafile /data/db/1/test.ns, filling with zeroes... Tue Oct 18 23:43:02 [FileAllocator] done allocating datafile /data/db/1/test.ns, size: 16MB, took 0.054 secs Tue Oct 18 23:43:02 [FileAllocator] allocating new datafile /data/db/1/test.0, filling with zeroes... Tue Oct 18 23:43:03 [FileAllocator] done allocating datafile /data/db/1/test.0, size: 64MB, took 0.556 secs Tue Oct 18 23:43:03 [FileAllocator] allocating new datafile /data/db/1/test.1, filling with zeroes... Tue Oct 18 23:43:03 [replica set sync] building new index on { _id: 1 } for test.users Tue Oct 18 23:43:03 [replica set sync] done for 0 records 0secs Tue Oct 18 23:43:03 [FileAllocator] done allocating datafile /data/db/1/test.1, size: 128MB, took 0.166 secs #27033 Tue Oct 18 08:42:22 [ReplSetHealthPollTask] replSet info 10.12.7.107:27031 is up Tue Oct 18 08:42:22 [ReplSetHealthPollTask] replSet member 10.12.7.107:27031 PRIMARY Tue Oct 18 08:42:22 [ReplSetHealthPollTask] replSet info 10.12.7.108:27032 is up Tue Oct 18 08:42:22 [ReplSetHealthPollTask] replSet member 10.12.7.108:27032 RECOVERING Tue Oct 18 08:42:34 [ReplSetHealthPollTask] replSet member 10.12.7.108:27032 SECONDARY
#27032 Tue Oct 18 23:47:47 [conn2] end connection 10.12.7.107:58587 Tue Oct 18 23:47:47 [replica set sync] replSet syncThread: 10278 dbclient error communicating with server: 10.12.7.107:27031 Tue Oct 18 23:47:48 [ReplSetHealthPollTask] DBClientCursor::init call() failed Tue Oct 18 23:47:48 [ReplSetHealthPollTask] replSet info 10.12.7.107:27031 is down (or slow to respond): DBClientBase::findOne: transport error: 10.12.7.107:27031 query: { replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "10.12.7.108:27032" } Tue Oct 18 23:47:48 [rs Manager] replSet info electSelf 1 Tue Oct 18 23:47:48 [rs Manager] replSet couldn't elect self, only received -9999 votes Tue Oct 18 23:47:54 [rs Manager] replSet info electSelf 1 Tue Oct 18 23:47:54 [rs Manager] replSet PRIMARY #27033 Tue Oct 18 08:48:52 [conn2] end connection 10.12.7.107:43768 Tue Oct 18 08:48:53 [conn3] 10.12.7.108:27032 is trying to elect itself but 10.12.7.107:27031 is already primary and more up-to-date Tue Oct 18 08:48:54 [ReplSetHealthPollTask] DBClientCursor::init call() failed Tue Oct 18 08:48:54 [ReplSetHealthPollTask] replSet info 10.12.7.107:27031 is down (or slow to respond): DBClientBase::findOne: transport error: 10.12.7.107:27031 query: { replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "10.12.7.107:27033" } Tue Oct 18 08:48:59 [conn3] replSet info voting yea for 1 Tue Oct 18 08:49:00 [ReplSetHealthPollTask] replSet member 10.12.7.108:27032 PRIMARY }}}
$ ./mongo localhost:27032 > rs.status() { "set" : "myset", "date" : ISODate("2011-10-18T15:53:01Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "10.12.7.107:27031", "health" : 0, "state" : 1, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "t" : 1318952647000, "i" : 1 }, "optimeDate" : ISODate("2011-10-18T15:44:07Z"), "lastHeartbeat" : ISODate("2011-10-18T15:47:46Z"), "errmsg" : "socket exception" }, { "_id" : 1, "name" : "10.12.7.108:27032", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "optime" : { "t" : 1318952647000, "i" : 1 }, "optimeDate" : ISODate("2011-10-18T15:44:07Z"), "self" : true }, { "_id" : 2, "name" : "10.12.7.107:27033", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 705, "optime" : { "t" : 0, "i" : 0 }, "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2011-10-18T15:53:00Z") } ], "ok" : 1 } }}}
myset:PRIMARY> db.users.findOne() { "_id" : ObjectId("4e9d9ec76df8f2f72bd9c45a"), "name" : "terrylc" }
Tue Oct 18 23:56:46 [ReplSetHealthPollTask] replSet info 10.12.7.107:27031 is up Tue Oct 18 23:56:46 [ReplSetHealthPollTask] replSet member 10.12.7.107:27031 SECONDARY Tue Oct 18 23:56:47 [conn6] I am already primary, 10.12.7.107:27031 can try again once I've stepped down Tue Oct 18 23:56:48 [initandlisten] connection accepted from 10.12.7.107:44777 #7 Tue Oct 18 23:56:49 [slaveTracking] building new index on { _id: 1 } for local.slaves Tue Oct 18 23:56:49 [slaveTracking] done for 0 records 0secs Tue Oct 18 23:57:03 [conn5] end connection 127.0.0.1:58555
import pymongo import sys def testInsert(): flag = 0 while True: if flag == 4: print "try to connect server 4 times but failed,so give up" break try: conn = pymongo.Connection(host=["10.12.7.107:27031", "10.12.7.108:27032", "10.12.7.107:27033"]) db = conn.test post = {"name1": "terrylc"} db.users.insert(post) conn.disconnect() return sys.exit(0) except Exception, e: print e flag += 1 if __name__ == '__main__': testInsert() }}}
def testInsert2(index): try: conn = pymongo.Connection(host=["10.12.7.107:27031", "10.12.7.108:27032", "10.12.7.107:27033"]) db = conn.test while index <= 5000: post = {"testTAG": index} time.sleep(1) objectId = db.users.save(post, safe=True,w=2) if objectId is not None: index += 1 conn.disconnect() except Exception, e: print e print "you index is :" + str(index) time.sleep(2) testInsert2(index) if __name__ == '__main__': testInsert2(1) }}}
conn = pymongo.Connection(host=["10.12.7.107:27031", "10.12.7.108:27032", "10.12.7.107:27033"], slave_okay=True) slave_okay=True # 这个是关键,可以将读写分离
$ echo "this is my super secret key" > mykey $ chmod 600 mykey $ mongod --keyFile mykey # other options... $ echo "this is my super secret key" > mykey $ chmod 600 mykey $ mongod --keyFile mykey # other options... $ echo "this is my super secret key" > mykey $ chmod 600 mykey $ mongod --keyFile mykey # other options...