时光闹钟app开发者,请关注我,后续分享更精彩!
坚持原创,共同进步!
概述
MongoDB是一个可扩展,高性能,开源,面向文档的,基于键/值类型的数据库。非常适合保存大对象及json格式的数据。本文将介绍MongoDB的单机和集群副本安装,记录下来以备后用。希望对有需要的朋友有所帮助和参考。
下载地址
# 历史产品和版本选择
https://www.mongodb.com/docs/legacy/
# 3.6版本文档地址
https://www.mongodb.com/docs/v3.6/
安装方式
- StandAlone:单机环境,一般开发测试的时候用。
- Replication:主从结构,一个Primary,多个Secondary,可能会有Arbitry。
- Primary挂掉之后,会选举出一个Secondary作为Primary,与zookeeper类似。
- Arbitry(仲裁节点)上面不存数据,只是为了凑数。选举算法要求节点数必须是奇数个,如果Primary+Secondary不是奇数个,就要用Arbitry凑数。
- 写数据只能在Primary,读数据默认也在Primary,可以配置成从Secondary读,可以选最近的节点。
- 数据在Primary上写成功之后,会将操作记录在oplog中,Secondary将oplog拷贝过去,然后照着操作一遍,就有数据了。
- Primary和Secondary上面的数据保证最终一致性,可以为写操作配置write concern,有几个级别:在Primary上写完就认为写成功;写到oplog后认为写成功;写到一个/多个/某个/某几个Secondary之后认为写成功,等等。
- Sharding:share nothing的结构,每台机器只存一部分数据。mongod服务器存数据,mongos服务器负责路由读写请求,元数据存在config数据库中。
安装&准备
centos 系统 yum源创建
cat >/etc/yum.repos.d/mongodb-org-3.6.repo<<eof
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/6Server/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
eof
根据具体情况,选择版本安装
# 安装最近版本
# sudo yum install -y mongodb-org
# 安装指定版本
sudo yum install --disablerepo=kubernetes -y mongodb-org-3.6.8 mongodb-org-server-3.6.8 mongodb-org-shell-3.6.8 mongodb-org-mongos-3.6.8 mongodb-org-tools-3.6.8
StandAlone安装配置
默认配置文件/etc/mongod.conf
- storage.dbPath 数据存储目录
- systemLog.path 系统日志目录
- net.bindIp 访问ip绑定,默认127.0.0.1。需要外部机器连接时,需调整成新人的访问源ip,或者测试所有ip放开0.0.0.0
启动MongoDB
sudo systemctl start mongod
# 如果启动报如下报错
# Failed to start mongod.service: Unit mongod.service not found.
# 启动以下命令
# sudo systemctl daemon-reload
查看MongoDB状态
sudo systemctl status mongod
# 如果想要重启系统MongoDB服务自动启动,可设置以下命令
# sudo systemctl enable mongod
停止MongoDB
sudo systemctl stop mongod
重启MongoDB
sudo systemctl restart mongod
连接MongoDB
mongo --host 127.0.0.1:27017
Replication安装
参考文档
官方文档:https://www.mongodb.com/docs/v3.6/administration/replica-set-deployment/
环境
3个节点分别安装MongoDB实例
修改/etc/mongod.conf以下配置项:
#replica set的名字
replication:
replSetName: "rs0"
#client访问ip绑定,开发测试0.0.0.0所有ip
net:
bindIp: localhost,<ip address>
分别在3个节点上启动MongoDB服务
sudo systemctl start mongod
# 如果启动报如下报错
# Failed to start mongod.service: Unit mongod.service not found.
# 启动以下命令
# sudo systemctl daemon-reload
Initiate the replica set(初始化副本集)
任意一个节点控制台登录MongoDB
[[email protected] ~]# mongo
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.8
Server has startup warnings:
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten]
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten]
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten]
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten]
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten]
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
2022-05-11T09:36:02.035+0800 I CONTROL [initandlisten]
>
初始化副本集。 注:只需在集群一台机器上执行初始化命令。
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "dev2:27017" },
{ _id: 1, host: "dev3:27017" },
{ _id: 2, host: "dev4:27017" }
]
})
执行结果如下:
> rs.initiate( {
... _id : "rs0",
... members: [
... { _id: 0, host: "dev2:27017" },
... { _id: 1, host: "dev3:27017" },
... { _id: 2, host: "dev4:27017" }
... ]
... })
{
"ok" : 1,
"operationTime" : Timestamp(1653010782, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1653010782, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
查看配置结果:控制台执行以下指令
rs.conf()
附录
# 历史产品和版本选择
https://www.mongodb.com/docs/legacy/
# 3.6版本文档地址
https://www.mongodb.com/docs/v3.6/
#官方文档:
https://www.mongodb.com/docs/v3.6/administration/replica-set-deployment/
# 3.6社区版centos安装文档
https://www.mongodb.com/docs/v3.6/tutorial/install-mongodb-on-red-hat/
# MongoDB集群安装
https://www.cnblogs.com/seasonzone/p/3820762.html