[App] MongoDB Cluster 配置

Mongodb集群有三种模式:  Replica Set 、 Sharding、Master-Slaver

现在演示 Replica Set 模式。

 

主节点

172.16.237.128

备节点

172.16.237.129

仲裁

172.16.237.129

os

rhel 7.2

 

1mongodb 安装 (所有节点)

# cd/usr/local/src

# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.4.tgz

# tar -zxf mongodb-linux-x86_64-rhel70-3.2.4.tgz

# mv mongodb-linux-x86_64-rhel70-3.2.4 /usr/local/mongodb

# chmod +x /usr/local/mongodb/bin/*

# vi /etc/profile

export PATH=/usr/local/mongodb/bin:$PATH

# source /etc/profile

# cd /usr/local/mongodb/

# mkdir -p data/mongodb

# mkdir -p data/logs

 

2、各节点配置

# vi data/mongodb.conf     # 主节点 配置

port=27017

dbpath=/usr/local/mongodb/data/mongodb/

logpath=/usr/local/mongodb/data/logs/mongodb.log

fork=true

logappend=true

replSet=mzh

bind_ip=172.16.237.128

noprealloc=true # 是否禁用数据文件预分配

oplogSize=100     # 设置oplog的大小(MB

 

# vi data/mongodb.conf     # 备节点 配置

port=27017

dbpath=/usr/local/mongodb/data/mongodb/

logpath=/usr/local/mongodb/data/logs/mongodb.log

fork=true

logappend=true

replSet=mzh

bind_ip=172.16.237.128

noprealloc=true

oplogSize=100

 

# vi data/mongodb1.conf     # 仲裁 配置

port=27018

dbpath=/usr/local/mongodb/data/mongodb1/

logpath=/usr/local/mongodb/data/logs/mongodb1.log

fork=true

logappend=true

replSet=mzh

bind_ip=172.16.237.128

noprealloc=true

oplogSize=100

 

3、启动

# mongod --config /usr/local/mongodb/data/mongodb.conf    #

about to fork child process, waiting until server is ready for connections.

forked process: 17802

child process started successfully, parent exiting

 

# mongod --config /usr/local/mongodb/data/mongodb.conf    #

# mongod --config /usr/local/mongodb/data/mongodb1.conf   # 仲裁

 

4、加入自启

# vi /etc/rc.local

mongod --config /usr/local/mongodb/data/mongodb.conf

 

5、主节点配置集群

# mongo 172.16.237.128

> use admin

> cfg={_id:"mzh",members:[{_id:0,host:"172.16.237.128:27017",priority:2},{_id:1,host:"172.16.237.129:27017",priority:1},{_id:2,host:"172.16.237.129:27018",arbiterOnly:true}]}

> rs.initiate(cfg)

> rs.status()

 

# priority 优先级高的为主

# arbiterOnly:true  仲裁点,主备模式生效

 

2、测试

# 主节点 新增记录

# mongo

> use test

>db.createCollection("test_table")

> showcollections

>db.test_table.insert({uid:1,"name":"test",url:"www.ceshi1.com"})

 

# 备节点 查看刚才的记录

# mongo

> use test

>db.test_table.find()

你可能感兴趣的:(【开源服务】,----,【数据库】,--------,MongoDB)