使用的版本:
$ ./mongo --version
MongoDB shell version: 2.6.12
下载地址:
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.12.tgz
创建用户mongo
解压后拷贝到用户目录: /usr/local/mongodb27017
$ id
uid=1010(mongo) gid=1010(mongo) groups=1010(mongo)
$ pwd
/usr/local/mongodb27017
$ ls
bin GNU-AGPL-3.0 mongodb-keyfile mongod.conf mongod.log mongod.pid README THIRD-PARTY-NOTICES
配置文件:/usr/local/mongodb27017/mongod.conf
maxConns=655360
logpath=/usr/local/mongodb27017/mongod.log
logappend=true
fork=true
dbpath=/data/mongodata27017
pidfilepath=/usr/local/mongodb27017/mongod.pid
bind_ip=192.168.137.131
auth=true
replSet=rs1
keyFile=/usr/local/mongodb27017/mongodb-keyfile
启动服务
/usr/local/mongodb27017/bin/mongod -f /usr/local/mongodb27017/mongod.conf
以超级用户管理员登陆
bin/mongo admin --host 192.168.137.132 -u "siteRootAdmin" -p "asdfasdfasdf"
查看连接数:
rs1:PRIMARY> db.serverStatus().connections
{
"current" : 417,
"available" : 523871,
"totalCreated" : NumberLong(177746)
}
==============================================================================================
参考命令:
1:集群环境说明:mongodb1:192.168.43.10、mongodb2:192.168.43.11、mongodb3:192.168.43.12。且基于主机名相互通信/etc/hosts文件(为了集群信息同步不出现问题,先关闭认证功能anth)
2:编辑mongodb1的/etc/mongod.conf文件,注释auth指令,添加如下内容
# 指定副本集的名称
replSet=uplooking
# 指定预拷贝的索引,这里只拷贝id那个系统自动生成的索引
replIndexPrefetch=_id_only
noauth=true
3:将mongodb1的配置文件拷贝到mongodb2、mongodb3
scp /etc/mongod.conf 192.168.43.11:/etc/
scp /etc/mongod.conf 192.168.43.12:/etc/
3:启动mongodb1、mongodb2、mongodb3
4:登入mongodb1的shell接口
> rs.status(); (查看集群中的节点状态)
> rs.initiate();(初始化副本集)
> rs.status();(再次查看集群中的节点状态)
uplooking:PRIMARY> (此时的提示符已经变了)
5:当初始化了主节点以后,需要添加其他的从节点到mongodb集群中来
uplooking:PRIMARY> rs.add("192.168.43.11")
uplooking:PRIMARY> rs.add("192.168.43.12")
6:添加了其他节点以后,就可以查看配置信息和副本集的状态信息
uplooking:PRIMARY> rs.conf()
{
"_id" : "uplooking",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017"
},
{
"_id" : 1,
"host" : "192.168.43.11:27017"
},
{
"_id" : 2,
"host" : "192.168.43.12:27017"
}
]
}
7:再次查看状态副本集的状态信息
uplooking:PRIMARY> rs.status()
{
"set" : "uplooking",
"date" : ISODate("2017-10-29T16:05:55Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "mongodb1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 914,
"optime" : Timestamp(1509292923, 1),
"optimeDate" : ISODate("2017-10-29T16:02:03Z"),
"electionTime" : Timestamp(1509292619, 1),
"electionDate" : ISODate("2017-10-29T15:56:59Z"),
"self" : true
},
{
"_id" : 1,
"name" : "192.168.43.11:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 237,
"optime" : Timestamp(1509292923, 1),
"optimeDate" : ISODate("2017-10-29T16:02:03Z"),
"lastHeartbeat" : ISODate("2017-10-29T16:05:54Z"),
"lastHeartbeatRecv" : ISODate("2017-10-29T16:05:54Z"),
"pingMs" : 0,
"syncingTo" : "mongodb1:27017"
},
{
"_id" : 2,
"name" : "192.168.43.12:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 232,
"optime" : Timestamp(1509292923, 1),
"optimeDate" : ISODate("2017-10-29T16:02:03Z"),
"lastHeartbeat" : ISODate("2017-10-29T16:05:55Z"),
"lastHeartbeatRecv" : ISODate("2017-10-29T16:05:55Z"),
"pingMs" : 0,
"syncingTo" : "mongodb1:27017"
}
],
"ok" : 1
}
8:此时从节点可以在shell接口查看到在主节点复制过来的数据库和表,但是不能查询表的内容,因此需要通过命令让从节点变为可用节点,在查询就可以了
uplooking:SECONDARY> rs.slaveOk()
9:主节点也可以通过命令变为从节点, 此时其他的节点通过投票将会变为主节点
uplooking:SECONDARY> rs.stepDown()
10:在新的主节点(mongodb2)中可以设置从节点的优先级让从节点变为主节点,让第一个从节点优先级为10,那么第一个从节点就会变为主节点
uplooking:PRIMARY> config=rs.conf() # 先使用一个config临时变量保存rs.conf()的值,也就是当前的副本集配置
uplooking:PRIMARY> config.members[0].priority=10 # 让第一个节点,也即是mongodb1的优先级为10
uplooking:PRIMARY> rs.reconfig(config) # 让副本集获得新的配置,此时,mongodb1将会成为主节点
11:在主节点中,从副本集中移除一个节点,
rs.conf() # 先查看需要删除的host
rs.remove("192.168.43.12:27017")
12:也可以将一个从节点变为arbiter仲裁节点,这里将mongodb3变为arbiter节点,必须先让mongodb3下线,然后删除数据目录,在重启,再设置为arbiter仲裁节点
rs.addArb("192.168.43.12")