使用docker-compose 搭建mongo单节点与副本集
注:确认已经已经关闭防火墙和selinux和已安装docker
1、单节点安装
1.1 安装docker-compose
1.安装docker-cpmpose
curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-uname -s-uname -m > /usr/local/bin/docker-compose
chmod +x/usr/local/bin/docker-compose
或者去直接下载安装包 https://github.com/docker/compose/releases
1.2 创建安装yml
mkdir -p /data/mongodb
vim /data/mongodb/mongo.yml
version: '3'
services:
mongo:
image: mongo
container_name: mongodb
ports:
- 27017:27017
volumes:
- /data/mongodb/data/db:/data/db
- /data/mongodb/configdb:/data/configdb
- /data/mongodb/backup:/data/backup
command:
['--auth']
tty:
true
1.3 启动mongodb脚本
vim /data/mongo/start_mongodb.sh
docker-compose -f mongo.yml up -d
# 延迟登录,解决可能出现的连接失败问题
sleep 3
# 进入docker容器,以admin用户身份进入mongo
docker exec -it mongodb mongo admin
1.4 mongo常用命令
常用命令使用文档: https://www.runoob.com/mongodb/mongodb-tutorial.html文章来源:https://www.toymoban.com/news/detail-813700.html
# 创建用户
db.createUser({user:'zhd',pwd:'123456',roles:[{role:'root',db:'admin'}]})
# 验证用户
db.auth('zhd','123456')
# 创建/切换数据库
use t_zhd
# 创建集合
db.createCollection('zhd')
# 查看集合
show tables
# 插入文档
db.hcy.insert({"zhd":"123"})
# 查询文档
db.hcy.find()
# 查看数据库
show dbs
2、MongoDB副本集集群
2.1.1 生成keyfile
mkdir /data/mongodb/{mongo-master,mongo-slave1,mongo-slave2} -pv
cd /data/mongodb/
# 400权限是要保证安全性,否则mongod启动会报错
openssl rand -base64 756 > mongodb.key
chmod 400 mongodb.key
cp mongodb.key /data/mongodb/mongo-master/
cp mongodb.key /data/mongodb/mongo-slave1/
cp mongodb.key /data/mongodb/mongo-slave2/
2.1.2 生成yml文件
vim /data/mongodb/mongodb.yml文章来源地址https://www.toymoban.com/news/detail-813700.html
version: "3"
services:
#主节点
mongodb1:
image: mongo
container_name: mongo-master
restart: always
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=mongodb@evescn
command: mongod --replSet rs0 --keyFile /mongodb.key
volumes:
- /etc/localtime:/etc/localtime
- /data/mongodb/mongo-master/data:/data/db
- /data/mongodb/mongo-master/configdb:/data/configdb
- /data/mongodb/mongo-master/mongodb.key:/mongodb.key
networks:
- mongoNet
entrypoint:
- bash
- -c
- |
chmod 400 /mongodb.key
chown 999:999 /mongodb.key
exec docker-entrypoint.sh $$@
# 副节点
mongodb2:
image: mongo
container_name: mongo-slave1
restart: always
ports:
- 27018:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=mongodb@evescn
command: mongod --replSet rs0 --keyFile /mongodb.key
volumes:
- /etc/localtime:/etc/localtime
- /data/mongodb/mongo-slave1/data:/data/db
- /data/mongodb/mongo-slave1/configdb:/data/configdb
- /data/mongodb/mongo-slave1/mongodb.key:/mongodb.key
networks:
- mongoNet
entrypoint:
- bash
- -c
- |
chmod 400 /mongodb.key
chown 999:999 /mongodb.key
exec docker-entrypoint.sh $$@
# 副节点
mongodb3:
image: mongo
container_name: mongo-slave2
restart: always
ports:
- 27019:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=mongodb@evescn
command: mongod --replSet rs0 --keyFile /mongodb.key
volumes:
- /etc/localtime:/etc/localtime
- /data/mongodb/mongo-slave2/data:/data/db
- /data/mongodb/mongo-slave2/configdb:/data/configdb
- /data/mongodb/mongo-slave2/mongodb.key:/mongodb.key
networks:
- mongoNet
entrypoint:
- bash
- -c
- |
chmod 400 /mongodb.key
chown 999:999 /mongodb.key
exec docker-entrypoint.sh $$@
networks:
mongoNet:
driver: bridge
2.1.3 启动和停止服务
docker-compose -f /data/mongodb/mongodb.yml up -d #启动服务
docker-compose -f /data/mongodb/mongodb.yml down #停止服务
2.2配置集群
2.2.1 登录容器
docker exec -it mongo-master mongo #进入容器
# 认证
> use admin
> db.auth('root', 'mongodb@evescn') #账户密码是配置文件配置的
2.2.2 初始化集群
2.2.2.1添加配置文件
> config={_id:"rs0",members:[
{_id:0,host:"192.168.2.11:27017"},
{_id:1,host:"192.168.2.11:27018"},
{_id:2,host:"192.168.2.11:27019"}]
}
#rs0 为集群名称,可以任意
#ip填写自己对应的ip
2.2.2.2初始化副本集mongodb-cluster集群
> rs.initiate(config)
2.2.2.5 查看副本集配置信息和运行状态
## 查看副本集配置信息 > rs.conf() ## 查看副本集运行状态: > rs.status() ............ "members" : [ { "_id" : 0, "name" : "192.168.2.11:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 316, "optime" : { "ts" : Timestamp(1680589697, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2023-04-04T06:28:17Z"), "lastAppliedWallTime" : ISODate("2023-04-04T06:28:17.929Z"), "lastDurableWallTime" : ISODate("2023-04-04T06:28:17.929Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1680589607, 1), "electionDate" : ISODate("2023-04-04T06:26:47Z"), "configVersion" : 1, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.2.11:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 102, "optime" : { "ts" : Timestamp(1680589697, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1680589697, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2023-04-04T06:28:17Z"), "optimeDurableDate" : ISODate("2023-04-04T06:28:17Z"), "lastAppliedWallTime" : ISODate("2023-04-04T06:28:17.929Z"), "lastDurableWallTime" : ISODate("2023-04-04T06:28:17.929Z"), "lastHeartbeat" : ISODate("2023-04-04T06:28:19.957Z"), "lastHeartbeatRecv" : ISODate("2023-04-04T06:28:18.991Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "192.168.2.11:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "configTerm" : 1 }, { "_id" : 2, "name" : "192.168.2.11:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 102, "optime" : { "ts" : Timestamp(1680589697, 1), "t" : NumberLong(1) }, .................
到了这里,关于使用docker-compose 搭建mongo单节点与副本集的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!