一 下载mongodb
https://www.mongodb.com/try/download/community?tck=docs_server
选择下载tar.gz包
- 直接解压
tar -zxvf mongodb-linux-x86_64-rhel70-5.0.5.tgz
- 重命名文件夹
mv mongodb-linux-x86_64-rhel70-5.0.5 mongodb
- 创建mongo 用户
adduser mongodb
- 设置密码
passwd mongodb
- 将mongodb 添加到mongodb分组
usermod -G mongodb mongodb
- 可查看monongdb 的分组
id mongodb
- 将mongo文件夹改为mongodb用户所有
chown -R mongodb:mongodb ./mongo
- 设置环境变量
vim /etc/profile
9.在最底部输入如下(/opt/mongo/mongodb 为解压的路径)
MONGODB_HOME=/opt/mongo/mongodb
PATH=$PATH:$MONGODB_HOME/bin
- 使环境变量生效
source /etc/profile
- 查看环境变量是否生效
mongo -version
显示如下
MongoDB shell version v5.0.5
Build Info: {
"version": "5.0.5",
"gitVersion": "d65fd89df3fc039b5c55933c0f71d647a54510ae",
"openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "rhel70",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
- 切换mongodb用户
su mongodb
二. 后面通过配置三个端口和不同文件夹模拟台电脑运行三个mongo
启动配置文档https://docs.mongodb.com/manual/administration/configuration/
13. 创建三个文件夹,使用三个不同端口
mkdir mongodb_27017 mongodb_27018 mongodb_27019
14. 每个文件夹创建 data logs 目录和mongo.yml配置文件
cd /opt/mongo/mongodb_27017
mkdir data logs
vi mongo.yml
输入
processManagement:
fork: true
net:
bindIp: 192.168.75.129
port: 27017
storage:
dbPath: /opt/mongo/mongodb_27017/data
systemLog:
destination: file
path: "/opt/mongo/mongodb_27017/logs/mongod.log"
logAppend: true
storage:
journal:
enabled: true
replication:
replSetName: mymongodb
启动 mongod -f mongo.yml
第二个文件夹
cd /opt/mongo/mongodb_27018
mkdir data logs
vi mongo.yml
输入
processManagement:
fork: true
net:
bindIp: 192.168.75.129
port: 27018
storage:
dbPath: /opt/mongo/mongodb_27018/data
systemLog:
destination: file
path: "/opt/mongo/mongodb_27018/logs/mongod.log"
logAppend: true
storage:
journal:
enabled: true
replication:
replSetName: mymongodb
启动 mongod -f mongo.yml
第三文件夹
cd /opt/mongo/mongodb_27019
mkdir data logs
vi mongo.yml
输入
processManagement:
fork: true
net:
bindIp: 192.168.75.129
port: 27019
storage:
dbPath: /opt/mongo/mongodb_27019/data
systemLog:
destination: file
path: "/opt/mongo/mongodb_27019/logs/mongod.log"
logAppend: true
storage:
journal:
enabled: true
replication:
replSetName: mymongodb
启动 mongod -f mongo.yml
15. 连接mongo
mongo --host=192.168.75.129:27017
输入初始化命令
rs.initiate()
输出
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "192.168.75.129:27017",
"ok" : 1
}
mymongodb:SECONDARY>
mymongodb:PRIMARY>
16.添加从节点节点
mongo --host=192.168.75.129:27017
rs.add("192.168.75.129:27018")
17.添加仲裁节点(我的本地这种方式添加失败,可使用cf配置)
rs.addArb("192.168.75.129:27019")
18.配置从节点可以读取数据
mongo --host=192.168.75.129:27018
rs.slaveOk();
19. 也可以直接配置初始化副本集群
mongo --host=192.168.75.129:27018
通过上面命令连接后输入下面两条命令初始化副本集群
cfg={"_id":"mymongodb","members":[{"_id":1,"host":"192.168.75.129:27017"},{"_id":2,"host":"192.168.75.129:27018"},{"_id":3,"host":"192.168.75.129:27019","arbiterOnly":true}]}
rs.initiate(cfg)
显示如下标识成功
> rs.initiate(cfg)
{ "ok" : 1 }
19.修改设置优先级(先获取配置,将配置的第二个节点的优先权重改为2,再通过reconfig重新使配置生效)
var config = rs.config()
config.members[1].priority=2
rs.reconfig(config)