下载MongoDB安装包
官网地址:https://www.mongodb.com/zh-cn
下载MongoDB shell(后面连接MongoDB会用到)
上传安装文件到服务器
我上传到了/root/pkg目录
安装及配置MongoDB
##解压文件到指定目录
tar -zxvf /root/pkg/mongodb-linux-x86_64-rhel80-6.0.8.tgz -C /opt/
##修改mongodb的安装目录名为mongodb(方便后面操作)
cd /opt/
mv mongodb-linux-x86_64-rhel80-6.0.8 mongodb
##切换到mongodb目录
cd mongodb/
##创建相关的目录(配置文件、数据库路径[副本集1,2,3]、keyFile、日志问价、pid文件)
mkdir config data1 data2 data3 key logs pid
##创建keyFile及修改权限(所有副本集节点都必须要用同一份keyfile,权限必须是只读)
openssl rand -base64 90 > key/mongo.keyfile
chmod 400 key/mongo.keyfile
##创建副本集1,2,3的配置文件(内容在下面)
touch config/mongodb1.conf config/mongodb2.conf config/mongodb3.conf
副本集1的配置文件内容
#指定数据库路径
dbpath=/opt/mongodb/data1
#指定MongoDB日志文件
logpath=/opt/mongodb/logs/mongodb1.log
#使用追加的方式写日志
logappend=true
#端口号
port=27017
#方便外网访问,绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定则默认本地所有IP
bind_ip=0.0.0.0
#以守护进程的方式运行MongoDB,创建服务器进程
#fork=true
#启用用户验证
auth=true
#指定KEYFILE
keyFile=/opt/mongodb/key/mongo.keyfile
#启用日志文件
journal=true
#副本集名称
replSet=rs1
#pid文件
pidfilepath=/opt/mongodb/pid/pid1.pid
#设置oplogsize大小为1024M
oplogSize=1024
副本集2的配置文件内容
#指定数据库路径
dbpath=/opt/mongodb/data2
#指定MongoDB日志文件
logpath=/opt/mongodb/logs/mongodb2.log
#使用追加的方式写日志
logappend=true
#端口号
port=27018
#方便外网访问,绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定则默认本地所有IP
bind_ip=0.0.0.0
#以守护进程的方式运行MongoDB,创建服务器进程
#fork=true
#启用用户验证
auth=true
#指定KEYFILE
keyFile=/opt/mongodb/key/mongo.keyfile
#启用日志文件
journal=true
#副本集名称
replSet=rs1
#pid文件
pidfilepath=/opt/mongodb/pid/pid2.pid
#设置oplogsize大小为1024M
oplogSize=1024
副本集3的配置文件内容
#指定数据库路径
dbpath=/opt/mongodb/data3
#指定MongoDB日志文件
logpath=/opt/mongodb/logs/mongodb3.log
#使用追加的方式写日志
logappend=true
#端口号
port=27019
#方便外网访问,绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定则默认本地所有IP
bind_ip=0.0.0.0
#以守护进程的方式运行MongoDB,创建服务器进程
#fork=true
#启用用户验证
auth=true
#指定KEYFILE
keyFile=/opt/mongodb/key/mongo.keyfile
#启用日志文件
journal=true
#副本集名称
replSet=rs1
#pid文件
pidfilepath=/opt/mongodb/pid/pid3.pid
#设置oplogsize大小为1024M
oplogSize=1024
编写副本集启停脚本
启动脚本
vi /opt/mongodb/bin/start.sh
#!/usr/bin/bash
nohup ./mongod -f /opt/mongodb/config/mongodb1.conf &
nohup ./mongod -f /opt/mongodb/config/mongodb2.conf &
nohup ./mongod -f /opt/mongodb/config/mongodb3.conf &
停止脚本
vi /opt/mongodb/bin/stop.sh
#!/usr/bin/bash
./mongod --shutdown -f /opt/mongodb/config/mongodb1.conf
./mongod --shutdown -f /opt/mongodb/config/mongodb2.conf
./mongod --shutdown -f /opt/mongodb/config/mongodb3.conf
修改启停脚本权限
chmod 755 /opt/mongodb/bin/*.sh
安装MongoDB shell
cd /root/pkg/
yum -y localinstall mongodb-mongosh-shared-openssl11-1.10.1.x86_64.rpm
启动MongoDB6.0.8副本集
##启动
/opt/mongodb/bin/start.sh
##查看端口看服务是否运行
ss -na | grep 2701
初始化副本集
登录任意一节点,执行 rs.initiate
mongosh --port 27017
rs.initiate({_id:"rs1",members:[
{_id:0,host:"127.0.0.1:27017" ,priority:2},
{_id:1,host:"127.0.0.1:27018",priority:1},
{_id:2,host:"127.0.0.1:27019", arbiterOnly:true}]});
可以看到过了一会,节点显示都变了
查看rs信息
rs.status();
{
set: 'rs1',
date: ISODate("2023-07-25T07:34:54.283Z"),
myState: 1,
term: Long("1"),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long("2000"),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 2,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1690270486, i: 1 }), t: Long("1") },
lastCommittedWallTime: ISODate("2023-07-25T07:34:46.401Z"),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1690270486, i: 1 }), t: Long("1") },
appliedOpTime: { ts: Timestamp({ t: 1690270486, i: 1 }), t: Long("1") },
durableOpTime: { ts: Timestamp({ t: 1690270486, i: 1 }), t: Long("1") },
lastAppliedWallTime: ISODate("2023-07-25T07:34:46.401Z"),
lastDurableWallTime: ISODate("2023-07-25T07:34:46.401Z")
},
lastStableRecoveryTimestamp: Timestamp({ t: 1690270436, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate("2023-07-25T07:26:16.182Z"),
electionTerm: Long("1"),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1690269965, i: 1 }), t: Long("-1") },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1690269965, i: 1 }), t: Long("-1") },
numVotesNeeded: 2,
priorityAtElection: 2,
electionTimeoutMillis: Long("10000"),
numCatchUpOps: Long("0"),
newTermStartDate: ISODate("2023-07-25T07:26:16.321Z"),
wMajorityWriteAvailabilityDate: ISODate("2023-07-25T07:26:17.075Z")
},
members: [
{
_id: 0,
name: '127.0.0.1:27017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 1570,
optime: { ts: Timestamp({ t: 1690270486, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-07-25T07:34:46.000Z"),
lastAppliedWallTime: ISODate("2023-07-25T07:34:46.401Z"),
lastDurableWallTime: ISODate("2023-07-25T07:34:46.401Z"),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1690269976, i: 1 }),
electionDate: ISODate("2023-07-25T07:26:16.000Z"),
configVersion: 1,
configTerm: 1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: '127.0.0.1:27018',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 528,
optime: { ts: Timestamp({ t: 1690270486, i: 1 }), t: Long("1") },
optimeDurable: { ts: Timestamp({ t: 1690270486, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-07-25T07:34:46.000Z"),
optimeDurableDate: ISODate("2023-07-25T07:34:46.000Z"),
lastAppliedWallTime: ISODate("2023-07-25T07:34:46.401Z"),
lastDurableWallTime: ISODate("2023-07-25T07:34:46.401Z"),
lastHeartbeat: ISODate("2023-07-25T07:34:52.828Z"),
lastHeartbeatRecv: ISODate("2023-07-25T07:34:53.768Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: '127.0.0.1:27017',
syncSourceId: 0,
infoMessage: '',
configVersion: 1,
configTerm: 1
},
{
_id: 2,
name: '127.0.0.1:27019',
health: 1,
state: 7,
stateStr: 'ARBITER',
uptime: 528,
lastHeartbeat: ISODate("2023-07-25T07:34:52.815Z"),
lastHeartbeatRecv: ISODate("2023-07-25T07:34:52.865Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
configVersion: 1,
configTerm: 1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1690270486, i: 1 }),
signature: {
hash: Binary(Buffer.from("d8b71dce14636bdfc5d2101f4b2085f96f3871a3", "hex"), 0),
keyId: Long("7259654268330704902")
}
},
operationTime: Timestamp({ t: 1690270486, i: 1 })
}
创建管理用户
[root@192 ~]# mongosh --port 27017
Current Mongosh Log ID: 64bf7bf8a2532dba4bea63e0
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
Using MongoDB: 6.0.8
Using Mongosh: 1.10.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
rs1 [direct: primary] test> use admin;
switched to db admin
rs1 [direct: primary] admin> db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1690270740, i: 4 }),
signature: {
hash: Binary(Buffer.from("c51f59125d56755bd53e78d346518ec29c5d1ddc", "hex"), 0),
keyId: Long("7259654268330704902")
}
},
operationTime: Timestamp({ t: 1690270740, i: 4 })
}
rs1 [direct: primary] admin> db.auth('admin','admin');
{ ok: 1 }
rs1 [direct: primary] admin> db.createUser({user:"root",pwd:"root",roles:["root"]});
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1690270771, i: 1 }),
signature: {
hash: Binary(Buffer.from("f8e1d1633ff2a24af3b054758728f18b35e4d97c", "hex"), 0),
keyId: Long("7259654268330704902")
}
},
operationTime: Timestamp({ t: 1690270771, i: 1 })
}
rs1 [direct: primary] admin> db.auth('root','root');
{ ok: 1 }
rs1 [direct: primary] admin> exit
创建普通用户
mongosh --port 27017
use testdb1;
db.createUser({user:"testu1",pwd:"testpwd",roles:[{role:"readWrite",db:"testdb1"}]});
db.auth('testu1','testpwd');
指定用户登录
[root@192 ~]# mongosh -u admin -p admin --host 192.168.80.10 --port 27017
Current Mongosh Log ID: 64bf7cb80ae8203df7cacab9
Connecting to: mongodb://<credentials>@192.168.80.10:27017/?directConnection=true&appName=mongosh+1.10.1
Using MongoDB: 6.0.8
Using Mongosh: 1.10.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
rs1 [direct: primary] test>
参考连接
1.MongoDB6.0.6 副本集搭建(CentOs8)http://www.dtmao.cc/php/101089.html
2.MongoDB 6.0.3 用户管理 http://blog.itpub.net/29785807/viewspace-2928763/
3.Linux下MongoDB的安装与配置 — 2023-04-26 https://www.jianshu.com/p/95ee7d49073b