mongo(五)副本集

时间:2022-06-01 21:41:30

mongo(五)副本集

配置文件

1-3为三个存储节点,其实一个为Primary,两个secondary作为备份,4为仲裁节点
# 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.conf

执行配置

mongod -f conf.conf

设定副本集合:

mongo --port 33198
use admin
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"
}
]}})

增加仲裁节点

rs.addArb("127.0.0.1:33195")
#!/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()