wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.9.tgz
cd /
mkdir soft
tar zxvf mongodb-linux-x86_64-rhel62-3.2.9.tgz
cp -r mongodb-linux-x86_64-rhel62-3.2.9 /soft/mongodb-master
cp -r mongodb-linux-x86_64-rhel62-3.2.9 /soft/mongodb-slave
cp -r mongodb-linux-x86_64-rhel62-3.2.9 /soft/mongodb-arbiter
rm -rf mongodb-linux-x86_64-rhel62-3.2.9
主节点操作
cd /soft/mongodb-master/
mkdir db
mkdir logs
mkdir conf
cd conf
vi master.conf
输入一下内容:
#master.conf
dbpath=/soft/mongodb-master/db/
logpath=/soft/mongodb-master/logs/master.log
pidfilepath=/soft/mongodb-master/master.pid
logappend=true
replSet=testrs
bind_ip=172.16.1.21 ##本机ip
port=27060 ##端口
fork=true
noprealloc=true
auth=false
启动
/soft/mongodb-master/bin/mongod -f /soft/mongodb-master/conf/master.conf
设置开机自动启动mongodb
vi /etc/rc.d/rc.local
/soft/mongodb-master/bin/mongod -f /soft/mongodb-master/conf/master.conf
从节点操作
cd mongodb-slave/
mkdir db
mkdir logs
mkdir conf
cd conf
vi slaver.conf
输入一下内容:
#slaver.conf
dbpath=/soft/mongodb-slave/db/
logpath=/soft/mongodb-slave/logs/slaver.log
pidfilepath=/soft/mongodb-slave/slaver.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=172.16.1.22 ##本机ip
port=27070
fork=true
noprealloc=true
auth=false
启动
/soft/mongodb-slave/bin/mongod -f /soft/mongodb-slave/conf/slaver.conf
iptables -I INPUT -p tcp --dport 27070 -j ACCEPT
设置开机自动启动mongodb
vi /etc/rc.d/rc.local
/soft/mongodb-slave/bin/mongod -f /soft/mongodb-slave/conf/slaver.conf
裁判节点操作
cd mongodb-arbiter/
mkdir db
mkdir logs
mkdir conf
cd conf
vi arbiter.conf
输入以下内容:
#arbiter.conf
dbpath=/soft/mongodb-arbiter/db/
logpath=/soft/mongodb-arbiter/logs/arbiter.log
pidfilepath=/soft/mongodb-arbiter/arbiter.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=172.16.1.22 ##本机ip
port=27080
fork=true
noprealloc=true
auth=false
启动
/soft/mongodb-arbiter/bin/mongod -f /soft/mongodb-arbiter/conf/arbiter.conf
iptables -I INPUT -p tcp --dport 27080 -j ACCEPT
设置开机自动启动mongodb
vi /etc/rc.d/rc.local
/soft/mongodb-arbiter/bin/mongod -f /soft/mongodb-arbiter/conf/arbiter.conf
进入数据库(主库)---bin目录下执行
cd /soft/mongodb-master/bin
./mongo 172.16.1.21:27060
> use admin
> cfg={ _id:"testrs", members:[ {_id:0,host:'172.16.1.21:27060',priority:10}, {_id:1,host:'172.16.1.22:27070',priority:9}, {_id:2,host:'172.16.1.22:27080',arbiterOnly:true}] };
> rs.initiate(cfg)
testrs:OTHER> rs.status()
创建用户
use admin;
db.createUser( { user: "root", pwd: "gzyr123",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
db.createUser( { user: "admin", pwd: "admin", roles: [ { role: "root", db: "admin" } ] });
创建数据库和用户
use test-youren;
db.createUser( { user: "test-youren", pwd: "test-youren", roles: [ { role: "readWrite", db: "test-youren" } ] });
db.createUser( { user: "youren_r", pwd: "rhnn9iBPzj8PwzAD", roles: [ { role: "read", db: "test-youren"} ] });
db.test.insert({'id':1});
use dev-youren;
db.createUser( { user: "dev-youren", pwd: "dev-youren", roles: [ { role: "readWrite", db: "dev-youren" } ] });
db.createUser( { user: "youren_r", pwd: "rhnn9iBPzj8PwzAD", roles: [ { role: "read", db: "dev-youren" } ] });
db.test.insert({'id':1});
进入从库 --bin目录下执行
cd /soft/mongodb-slave/bin
./mongo 172.16.1.22:27070
testrs:SECONDARY> use test-youren
testrs:SECONDARY> db.getMongo().setSlaveOk()
testrs:SECONDARY> db.test.find()
{ "_id" : ObjectId("57eb19e242ff167f086d55ca"), "id" : 1 }
开启验证--修改配置文件
auth=true
验证权限
cd /soft/mongodb-master/bin
./mongo 172.16.1.21:27060
use test-youren;
db.auth("test-youren", "test-youren") ;