MongDB主从复制、复制集
主从复制比较简单,指定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会自动将所有节点连接起来,如:
[root@localhost 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>
故我们上面的启动第三个节点的如下脚本可以改成配置文件形式:
之前的配置:
[root@localhost 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
之后的配置:【待续】