主从复制对照简单,指定master、slave即可,个中master可写可读、slave只能读不克不及写。向master插入数据时,mongodb会主动将数据复制到slave节点。如许做的优点是读写星散,也便于节制一些权限。方式:
启动master:
像正常启动一样,只用加一个–master即可将此节点符号为主。
mongod --dbpath "F:/mongo/dbs/master" --port 10000 --master --logpath "F:/mongo/logs/master/MongoDB.log" --rest
启动slave:
与正常启动比拟多了两个参数,一个是–slave(把这个机械标识为slave),另一个是–source 127.0.0.1:10000(透露表现此slave的master是本机的10000号端口)
mongod --dbpath "D:/mongo/dbs/slave" --port 10001 --slave --source 127.0.0.1:10000 --logpath "D:/mongo/logs/slave/MongoDB.log" --rest
然后可以测试,向master插入数据,会被主动复制到slave。向slave插入数据会提醒mot master异常。
Replica Set复制集:
replica Set的特点是多个节点互相备份,个中一个节点担任primary,其余节点担任secondary。当primary节点挂失落之后,会经由过程选举机制在secondary中从新发生一个primary节点,可以提高mongoDB的健壮性。特殊是在分片中,可以将每个分片利用replica set作为复制集如许更不乱。
操作步调:
第一个节点启动时:
C:/Users/tree>mongod --dbpath "D:/Program Files/MongoDB/data/db" --logpath "D:/Program Files/MongoDB/data/log/MongoDB.log" --logappend --rest --journal --replSet CIF/127.0.0.1:10000
如许就启动了一个在27017的节点。
个中利用–replSet CIF/127.0.0.1:10000来声明这个节点是一个复制集,复制集的名字是“CIF”,别的一个节点在127.0.0.1的10000端口上。
第二个节点:
E:/MongoDB_CIF/bin>mongod --dbpath "E:/MongoDB_CIF/data/db" --port 10000 --logpath "E:/MongoDB_CIF/data/log/MongoDB.log" --logappend --rest --journal --replSet CIF/127.0.0.1:27017
同样利用–replSet CIF/127.0.0.1:27017声明这个节点和本机27017的节点同为一个CIF复制集。
第三个节点:
我们在linux虚拟机上到场第三个节点是,随便指定上面的一个节点就行,mongoDB会主动将所有节点毗邻起来,如:
[[email protected] log]# mongod -dbpath /usr/local/mongodb/data/db --logpath /usr/local/mongodb/data/log/MongoDB.log --logappend --fork --rest --journal --replSet CIF/192.168.43.104:27017 about to fork child process, waiting until server is ready for connections. all output going to: /usr/local/mongodb/data/log/MongoDB.log forked process: 3753 child process started successfully, parent exiting
OK,三个节点都竖立好了,如今最先初始化:
初始化:
随便在一个终端进行,这个终端会被当成primary节点。要求secondary节点没稀有据,不然会报一个already has data……must be empty什么的错。
config={"_id":"CIF","members":[{"_id":1,"host":"192.168.43.104:27017"},{"_id":2,"host":"192.168.43.104:10000"},{"_id":3,"host":"192.168.43.101:27017"}]} rs.initiate(config)
客户端打印如下信息,申明复制集竖立成功:
> rs.initiate(config) { "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 }
然后进入节制页面看看:
可以看见一个primary节点,两个secondary节点。
到场一个仲裁节点:
下面测试在2000端口启动一个仲裁节点:
mongod -dbpath "E:/MongoDB_Arbiter/data/db" --port 20000 --logpath "E:/MongoDB_Arbiter/data/log/MongoDB.log" --logappend --rest --journal --replSet CIF/127.0.0.1:27017
在primary节点上从新设置装备摆设:
config={"_id":"CIF","members":[{"_id":1,"host":"192.168.43.104:27017",priority:2},{"_id":2,"host":"192.168.43.104:10000"},{"_id":3,"host":"192.168.43.101:27017"},{"_id":4,"host":"192.168.43.104:20000",arbiterOnly:true}]} CIF:PRIMARY> rs.reconfig(config)
以上变设置装备摆设好了复制集。复制集主节点操作没有任何问题,然则对从节点进行操作时:
CIF:SECONDARY> show collections Mon Sep 30 09:07:36.128 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:128
会报一个我们熟悉的错,not master~需要设置slaveOK属性才可以在secondary节点查询:
CIF:SECONDARY> db.getMongo().setSlaveOk()
然后再查询就OK了。
如需点窜每个节点的权重:
//登录primary节点,显示当前复制集的设置装备摆设: CIF:PRIMARY> rs.conf() { "_id" : "CIF", "version" : 2, "members" : [ { "_id" : 1, "host" : "192.168.43.104:27017", "priority" : 2 }, { "_id" : 2, "host" : "192.168.43.104:10000" }, { "_id" : 3, "host" : "192.168.43.101:27017" }, { "_id" : 4, "host" : "192.168.43.104:20000", "arbiterOnly" : true } ] } //把这个设置装备摆设付给一个变量config CIF:PRIMARY> config=rs.conf() //从新点窜config变量的值 CIF:PRIMARY> config.members[0].priority=3 3 CIF:PRIMARY> config.members[1].priority=2 2 //从新把config变量设置装备摆设到mongo集群中: CIF:PRIMARY> rs.reconfig(config) Mon Sep 30 11:00:29.808 DBClientCursor::init call() failed Mon Sep 30 11:00:29.811 trying reconnect to 127.0.0.1:27017 Mon Sep 30 11:00:29.818 reconnect 127.0.0.1:27017 ok reconnected to server after rs command (which is normal)
附:linux下可以经由过程conf设置装备摆设文件启动
设置装备摆设文件全文:
# mongo.conf #where to log logpath=/var/log/mongo/mongod.log logappend=true # fork and run in background fork = true #port = 27017 dbpath=/var/lib/mongo # 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 #oplog = 0 # Diagnostic/debugging option #nocursors = true # Ignore query hints #nohints = true # Disable the HTTP interface (Defaults to localhost:27018). #nohttpinterface = 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> # Accout token for Mongo monitoring server. #mms-token = <token> # Server name for Mongo monitoring server. #mms-name = <server-name> # Ping interval for Mongo monitoring server. #mms-interval = <seconds> # Replication Options # in replicated mongo databases, specify here whether this is a slave or master #slave = true #source = master.example.com # Slave only: specify a single database to replicate #only = master.example.com # or #master = true #source = slave.example.com # Address of a server to pair with. #pairwith = <server:port> # Address of arbiter server. #arbiter = <server:port> # Automatically resync if slave data is stale #autoresync # Custom size for replication operation log. #oplogSize = <MB> # Size limit for in-memory storage of op ids. #opIdMem = <bytes>
故我们上面的启动第三个节点的如下剧本可以改成设置装备摆设文件形式:
之前的设置装备摆设:
[[email protected] log]# mongod -dbpath /usr/local/mongodb/data/db --logpath /usr/local/mongodb/data/log/MongoDB.log --logappend --fork --rest --journal --replSet CIF/192.168.43.104:27017
之后的设置装备摆设:【待续】
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/9205.html