且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mongo(五)副本集

更新时间:2022-07-02 07:20:38

配置文件

1-3为三个存储节点,其实一个为Primary,两个secondary作为备份,4为仲裁节点
mongo(五)副本集
mongo(五)副本集
# mongod.conf

#where to log
logpath=/home/data/db/mongodb2.log

logappend=true

# fork and run in background
fork=true

port=33197

dbpath=/home/yuxianda/data/db/db2

# location of pidfile
pidfilepath=/home/yuxianda/data/db/mongod2.pid

# Listen to local interface only. Comment out to listen on all interfaces. 
bind_ip=127.0.0.1

#datebase in diffrent file
directoryperdb=true

# fsync time
#syncdelay=5

# Disables write-ahead journaling
# nojournal=true

# Enables periodic logging of CPU utilization 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 on receipt (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 to port 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=<size>

# Replication Options

# in replicated mongo databases, specify the replica set name here
replSet=android-test
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile
mongo(五)副本集

执行配置

mongod -f conf.conf
 

设定副本集合:

mongo --port 33198
use admin
mongo(五)副本集
db.runCommand({"replSetInitiate":{
    "_id":"android-test",
    "members":[
    {
          "_id":1,
          "host":"127.0.0.1:33198"
    },
     {
          "_id":2,
          "host":"127.0.0.1:33197"
    },
     {
          "_id":3,
          "host":"127.0.0.1:33196"
    }
    ]}})
mongo(五)副本集

增加仲裁节点

rs.addArb("127.0.0.1:33195")
 
mongo(五)副本集
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

from pymongo import MongoClient, MongoReplicaSetClient

dbList = '127.0.0.1:33198,127.0.0.1:33197,127.0.0.1:33196'


def init(dbname):
    try:
        client = MongoReplicaSetClient(dbList, replicaSet='android-    test')
        db = client[dbname]
        return db
    except Exception as e:
        print 'error:%s'%e
        return False

if __name__ == '__main__':
    db = init('test')
print db.appinfo.find_one()    
mongo(五)副本集

 

 



mongo(五)副本集
本文 由 cococo点点 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 *** 许可协议进行许可。欢迎转载,请注明出处:
转载自:cococo点点 http://www.cnblogs.com/coder2012