RockMongo是个web版的MongoDB管理工具,相比原始的命令行操作,RockMongo的图形化界面大大的提升的我们的工作效率,本章我们在Docker下搭建一个Mongodb集群环境,再装一个RockMongo,用来操作新建的数据库;
有关安装Mongodb集群环境的详情,请参照《Docker下,实战mongodb副本集(Replication)》一文;
Mongodb镜像选用bolingcavalry/ubuntu16-mongodb349:0.0.1,这是我自己制作的mongodb的镜像,已经传到hub.docker.com网站,可以通过docker pull bolingcavalry/ubuntu16-mongodb349:0.0.1命令下载使用,关于此镜像的详情请看《制作mongodb的Docker镜像文件》;
Rockmongo的镜像我选用了javierjeronimo/rockmongo:latest,这是hub.docker.com上被pull次数较多的镜像,作者公开了Dockfile脚本,有兴趣的读者可以自己动手构建一下,脚本地址:https://hub.docker.com/r/javierjeronimo/rockmongo/~/dockerfile/
我们要启动四个容器:Mongodb集群和Rockmongo Server:
容器名 | ip | 备注 |
---|---|---|
ma | 172.19.0.2 | Primary |
mb | 172.19.0.3 | Secondary1 |
mc | 172.19.0.4 | Secondary2 |
rockmongo | 172.19.0.5 | 管理server |
网络情况如下图所示:
从上图得知我们要启动四个容器:Mongodb集群和Rockmongo Server,所以用docker-compose批量管理比较方便,docker-compose.yml内容如下:
version: '2'
services:
ma:
image: bolingcavalry/ubuntu16-mongodb349:0.0.1
container_name: ma
command: /bin/sh -c 'mongod --replSet replset0'
restart: always
mb:
image: bolingcavalry/ubuntu16-mongodb349:0.0.1
container_name: mb
depends_on:
- ma
command: /bin/sh -c 'mongod --replSet replset0'
restart: always
mc:
image: bolingcavalry/ubuntu16-mongodb349:0.0.1
container_name: mc
depends_on:
- mb
command: /bin/sh -c 'mongod --replSet replset0'
restart: always
rockmongo:
image: javierjeronimo/rockmongo:latest
container_name: rockmongo
depends_on:
- mc
ports:
- "8088:80"
links:
- ma:mongodba
restart: always
如上述yml脚本所示,ma、mb、mc这三个容器组成了副本集集群,rockmongo容器启动后,/etc/hosts文件中有了ma的ip配置(links属性);
在docker-compose.yml文件所在目录下,执行以下命令即可创建所有容器:
docker-compose up -d
启动效果如图所示:
config = { _id:"replset0", members:[{_id:0,host:"172.19.0.2:27017"},{_id:1,host:"172.19.0.3:27017"},{_id:2,host:"172.19.0.4:27017"}]}
控制台输出如下:
> use admin
switched to db admin
> config = { _id:"replset0", members:[{_id:0,host:"172.19.0.2:27017"},{_id:1,host:"172.19.0.3:27017"},{_id:2,host:"172.19.0.4:27017"}]}
{
"_id" : "replset0",
"members" : [
{
"_id" : 0,
"host" : "172.19.0.2:27017"
},
{
"_id" : 1,
"host" : "172.19.0.3:27017"
},
{
"_id" : 2,
"host" : "172.19.0.4:27017"
}
]
}
>
可见三个机器都已经加入集群;
4. 执行rs.initiate(config)初始化配置;
5. 执行rs.status()查看状态,得到输出如下:
replset0:PRIMARY> rs.status()
{
"set" : "replset0",
"date" : ISODate("2017-10-14T15:00:54.417Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1507993251, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1507993251, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1507993251, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "172.19.0.2:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1855,
"optime" : {
"ts" : Timestamp(1507993251, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-10-14T15:00:51Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1507993190, 1),
"electionDate" : ISODate("2017-10-14T14:59:50Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "172.19.0.3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 74,
"optime" : {
"ts" : Timestamp(1507993251, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1507993251, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-10-14T15:00:51Z"),
"optimeDurableDate" : ISODate("2017-10-14T15:00:51Z"),
"lastHeartbeat" : ISODate("2017-10-14T15:00:52.494Z"),
"lastHeartbeatRecv" : ISODate("2017-10-14T15:00:53.380Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.19.0.4:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.19.0.4:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 74,
"optime" : {
"ts" : Timestamp(1507993251, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1507993251, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-10-14T15:00:51Z"),
"optimeDurableDate" : ISODate("2017-10-14T15:00:51Z"),
"lastHeartbeat" : ISODate("2017-10-14T15:00:52.494Z"),
"lastHeartbeatRecv" : ISODate("2017-10-14T15:00:53.374Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.19.0.2:27017",
"configVersion" : 1
}
],
"ok" : 1
}
replset0:PRIMARY>
可以看到三个容器都已经加入集群,并且ma是Primary;
在ma的mongodb控制台执行以下命令,创建数据库school并往集合student中新增三条记录:
use school
db.student.insert({name:"Tom", age:16})
db.student.insert({name:"Jerry", age:15})
db.student.insert({name:"Mary", age:9})
到这里,mongodb的集群环境已经配置好了,并且数据库school中也添加了一个集合student,里面存了三个文档,接下来我们该配置Rockmongo了;
退出ma容器,执行docker exec -it rockmongo /bin/bash进入rockmongo容器;
进入rockmongo容器后,用vi打开/app/config.php文件,找到下面红框中的代码:
上图红框中的两个配置分别是数据库名称和mongodb的IP,分别改成”school”和”mongodba”(因为在docker-compose.yml中配置了link,所以此处用link的配置就行,不用写ma容器的IP了),改动后的部分如下图所示:
如上图红框所示,默认选中的数据库就是我们刚刚改的”school”,用户名密码都是admin,输入后可以进入RockMongo系统;
2. 进入系统后看到的页面如下图所示:
如上图,数据库school的集合student中的文档数据全部可以看见了;
3. 我们来试试在RonckMongo系统中执行查询,如下图,在输入框输入font color=”blue”>{age:{$lt:10}},点击下面的“执行”,就能看到查询结果:
至此,Docker下安装和体验Rockmongo就实战完毕了,希望读者们多尝试和体验这个工具,用来在实际工作中提升效率;