为了账号安全,请及时绑定邮箱和手机立即绑定

docker部署mongo 4.*集群

标签:
MongoDB

前言

​ 为了在测试环境部署新项目,决定使用docker搭建一条mongo集群数据。

准备

​ 三台测试环境服务器

ip 操作系统
192.168.188.7 centos7
192.168.188.129 centos7
192.168.188.144 centos7

部署

以下每台都执行==============

=安装过程=======

镜像

docker pull mongo:4.0.10

查看版本:

地址如下:https://hub.docker.com

网络

docker network create --subnet=10.20.0.0/24 mongodbnet

文件夹

mkdir -p /usr/local/mongo/configsvr
mkdir -p /usr/local/mongo/shard1
mkdir -p /usr/local/mongo/shard2
mkdir -p /usr/local/mongo/shard3
mkdir -p /usr/local/mongo/mongos

Config-Server 配置文件

vim /usr/local/mongo/configsvr/mongod.conf
####内容:
storage:
  dbPath: /data/db
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
net:
  bindIp: 0.0.0.0
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
replication:
  replSetName: cfg
sharding:
  clusterRole: configsvr

Mongos 配置文件

vim /usr/local/mongo/mongos/mongos.conf
####内容:
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongos.log
net:
  port: 27020
  bindIp: 0.0.0.0
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
sharding:
  configDB: cfg/192.168.188.7:27019,192.168.188.129:27019,192.168.188.144:27019

Shard-Server 配置文件1

vim /usr/local/mongo/shard1/mongod.conf
####内容
storage:
  dbPath: /data/db
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
net:
  bindIp: 0.0.0.0
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr

Shard-Server 配置文件2

vim /usr/local/mongo/shard2/mongod.conf
#### 内容
storage:
  dbPath: /data/db
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
net:
  bindIp: 0.0.0.0
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr

Shard-Server 配置文件3

vim /usr/local/mongo/shard3/mongod.conf
#### 内容
storage:
  dbPath: /data/db
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
net:
  bindIp: 0.0.0.0
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr

启动Docker容器 Config-Server容器(每台服务器执行一次):

docker run -d --restart=always --name=cfg_1 -p 27019:27019 --network=mongodbnet -v /usr/local/mongo/configsvr:/etc/mongodb mongo:4.0.10 -f /etc/mongodb/mongod.conf

启动3*3个Shard-Server容器:说明:分片服务器启动后默认是以27018作为端口。

启动第一个分片 - shard1

docker run -d --restart=always --name=shard1_1 -p 27018:27018 --network=mongodbnet -v /usr/local/mongo/shard1:/etc/mongodb mongo:4.0.10 -f /etc/mongodb/mongod.conf

启动第二个分片 - shard2

docker run -d --restart=always --name=shard2_1 -p 27028:27018 --network=mongodbnet -v /usr/local/mongo/shard2:/etc/mongodb mongo:4.0.10 -f /etc/mongodb/mongod.conf

启动第三个分片 - shard3

docker run -d --restart=always --name=shard3_1 -p 27038:27018 --network=mongodbnet -v /usr/local/mongo/shard3:/etc/mongodb mongo:4.0.10 -f /etc/mongodb/mongod.conf

启动3个mongos服务器 说明:这里也使用了mongo镜像,但是需要开启mongos进程,mongod进程并不需要用到


docker run -d --restart=always --name=mongos_1 -p 27017:27017 -p 27020:27020 --network=mongodbnet -v /usr/local/mongo/mongos:/etc/mongodb mongo:4.0.10

集群

进入其中一个容器配置Config-Server副本集:

docker exec -it cfg_1 /bin/bash
# 容器中
mongo --port 27019
# Mongo Shell中
rs.initiate({
	"_id":"cfg",
	"members":[
 	 {
  		"_id":0,
  		"host":"192.168.188.7:27019"
 	 },
 	 {
  		"_id":1,
  		"host":"192.168.188.129:27019"
 	 },
 	 {
  		"_id":2,
  		"host":"192.168.188.144:27019"
  	 }
 ]
})

进入其中一个容器配置Shard-Server1副本集:

# 宿主机
docker exec -it shard1_1 /bin/bash
# 容器中
mongo --port 27018
# Mongo Shell中
rs.initiate({
	"_id":"shard1",
	"members":[
	{
		"_id":0,
		"host":"192.168.188.7:27018"
	},
	{
		"_id":1,
	"host":"192.168.188.129:27018"
	},
	{
		"_id":2,
		"host":"192.168.188.144:27018"
	}
  ]
})

进入其中一个容器配置Shard-Server2副本集:

# 宿主机
docker exec -it shard2_1 /bin/bash
# 容器中
mongo --port 27018
# Mongo Shell中
rs.initiate({
	"_id":"shard2",
	"members":[
	{
		"_id":0,
		"host":"192.168.188.7:27028"
	},
	{
		"_id":1,
		"host":"192.168.188.129:27028"
	},
	{
		"_id":2,
		"host":"192.168.188.144:27028"
	}
  ]
})

进入其中一个容器配置Shard-Server3副本集:

# 宿主机
docker exec -it shard3_1 /bin/bash
# 容器中
mongo --port 27018
# Mongo Shell中
rs.initiate({
	"_id":"shard3",
	"members":[
	{
		"_id":0,
		"host":"192.168.188.7:27038"
	},
	{
		"_id":1,
		"host":"192.168.188.129:27038"
	},
	{
		"_id":2,
		"host":"192.168.188.144:27038"
	}
  ]
})

进入mongos容器中,启动mongos进程(此处可以改进一下,自动运行mongos进程)

# 宿主机
docker exec -it mongos_1 /bin/bash
# 容器中
mongos -f /etc/mongodb/mongos.conf
#可以就在其中一个mongos容器中使用mongo shell连接mongos进程配置分片集群
# 连接mongos,端口号与mongos配置文件中设定一致
mongo -port 27020
# 将分片加入集群
sh.addShard("shard1/192.168.188.7:27018,192.168.188.129:27018,192.168.188.144:27018")
sh.addShard("shard2/192.168.188.7:27028,192.168.188.129:27028,192.168.188.144:27028")
sh.addShard("shard3/192.168.188.7:27038,192.168.188.129:27038,192.168.188.144:27038")

# 对数据库开启分片功能
sh.enableSharding("cvmc")

#切换数据库并建立一张测试表才会真的创建数据库
use cvmc
db.message.insert({"mid":"test"})
#创建一个索引才能开启集合分片
db.message.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
use admin
# 对数据库中集合开启分片,并指定片键
sh.shardCollection("cmvc.message",{"mid":1})
# sh.shardCollection("[dbName.collectionName]",{[keyName]:1})
# 查看分片状态
sh.status()

mongo一些常用命令

索引

1.基础索引

在字段age 上创建索引,1(升序);-1(降序):

db.users.ensureIndex({age:1})

_id 是创建表的时候自动创建的索引,此索引是不能够删除的。当系统已有大量数据时,创建索引就是个非常耗时的活,我们可以在后台执行,只需指定“backgroud:true”即可。

db.t3.ensureIndex({age:1} , {backgroud:true})

2.文档索引

索引可以任何类型的字段,甚至文档:

db.factories.insert( { name: "wwl", addr: { city: "Beijing", state: "BJ" } } );

//在addr 列上创建索引

db.factories.ensureIndex( { addr : 1 } );

//下面这个查询将会用到我们刚刚建立的索引

db.factories.find( { addr: { city: "Beijing", state: "BJ" } } );

//但是下面这个查询将不会用到索引,因为查询的顺序跟索引建立的顺序不一样

db.factories.find( { addr: { state: "BJ" , city: "Beijing"} } );

3. 组合索引

跟其它数据库产品一样,MongoDB 也是有组合索引的,下面我们将在addr.city 和addr.state上建立组合索引。当创建组合索引时,字段后面的1 表示升序,-1 表示降序,是用1 还是用-1 主要是跟排序的时候或指定范围内查询 的时候有关的。

db.factories.ensureIndex( { "addr.city" : 1, "addr.state" : 1 } );

// 下面的查询都用到了这个索引

db.factories.find( { "addr.city" : "Beijing", "addr.state" : "BJ" } );

db.factories.find( { "addr.city" : "Beijing" } );

db.factories.find().sort( { "addr.city" : 1, "addr.state" : 1 } );

db.factories.find().sort( { "addr.city" : 1 } );

4. 唯一索引

只需在ensureIndex 命令中指定”unique:true”即可创建唯一索引。例如,往表t4 中插入2 条记录时候报错。

db.t4.ensureIndex({firstname: 1, lastname: 1}, {unique: true});

5.强制使用索引

hint 命令可以强制使用某个索引。

db.t5.find({age:{$lt:30}}).hint({name:1, age:1}).explain()

6.删除索引

//删除t3 表中的所有索引

db.t3.dropIndexes()

//删除t4 表中的firstname 索引

db.t4.dropIndex({firstname: 1})
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消