Skip to content

mongoDB配置副本集

--> -->

内容提供者: 海涛同学

配置副本集:

1.启动主服务器(110.161.51.30)

mongod --replSet tulingdao -f /etc/mongodb.conf --fork

2.启动副本服务器(110.163.177.72)

mongod --replSet tulingdao -f /etc/mongodb.conf --fork

3.在monogo shell中创建配置文件,并连接到主服务器

javascript
var config = {
  "_id": "tulingdao",
  "members": [
    {"_id":0, "host" : "110.161.51.30:27017"},
    {"_id":1, "host" : "110.163.177.72:27017", "votes":0 ,"priority": 0, slaveDelay:360 }
  ]
}
javascript
//连接到主服务器
db = (new Mongo("110.161.51.30:27017")).getDB("test")

//初始化副本集
rs.initiate(config)

node.js 调用mongodb的副本集环境. 通过多个连接 url来实现,如下代码实现

mongoose.connect('mongodb://username:password[@host](/user/host):port/database,mongodb://username:password[@host](/user/host):port,mongodb://username:password[@host](/user/host):port?options...' [, options]);

主/副 服务器连接字符串中间用逗号隔开即可. 一介布衣