前提
这里使用的是 4.4.19 版本
1. 下载
$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.19.tgz
2. 解压
$ tar -zxvf mongodb-linux-x86_64-rhel70-4.4.19.tgz
3. 创建存储数据的文件文件夹 data
4. 创建存储配置文件的文件夹 conf
5. 创建存放日志文件的文件夹 log
$ mkdir data
$ mkdir conf
$ mkdir log
6. 创建配置文件:mongodb.conf
# 数据存放的目录
dbpath=/usr/local/mongodb/mongodb-linux-x86_64-rhel70-4.4.19/data
# 后台启动需要配置日志输出
logpath=/usr/local/mongodb/mongodb-linux-x86_64-rhel70-4.4.19/log/mongodb.log
# 端口
port=27017
# 0.0.0.0 任何网址都可以进行访问,客户端工具链接需要进行此操作
bind_ip = 127.0.0.1
# 设置成后台启动
fork = true
journal = false
7. 启动
$ ./mongod --config /usr/local/mls/mongoDB/mongodb-linux-x86_64-rhel70-4.4.19/conf/mongodb.conf
$ ./mongod --config /usr/local/mls/mongoDB/mongodb-linux-x86_64-rhel70-4.4.19/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1248
child process started successfully, parent exiting
$
8. 访问客户端的方式
# 指定端口访问
$ ./mongo --port 8000
# 如果是默认端口 27017 则不用指定路径启动
$ ./mongo
9. 添加用户信息
$ ./mongo
> use admin
switched to db admin
# 添加用户
> db.createUser({user:"admin", pwd:"123456", roles:[{role:"userAdminAnyDatabase", db:"admin"}]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
# 添加超级用户
> db.createUser({user:"root", pwd:"123456", roles:[{role:"root", db:"admin"}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
# 查看已经添加的用户
> show users
{
"_id" : "admin.admin",
"userId" : UUID("11c80ba9-f906-4141-98ca-d6f9cbe04f3a"),
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
{
"_id" : "admin.root",
"userId" : UUID("8ed55dbf-777c-4a61-8eb8-4df781add491"),
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
# 删除单个用户 admin1 用户是为了测试命令而添加上的
> db.system.users.remove({user:"admin1"})
WriteResult({ "nRemoved" : 1 })
# 关闭服务
> db.shutdownServer()
server should be down...
10. 关闭服务
$ ./mongod --config /usr/local/mls/mongoDB/mongodb-linux-x86_64-rhel70-4.4.19/conf/mongodb.conf --shutdown
$ ./mongod --config /usr/local/mls/mongoDB/mongodb-linux-x86_64-rhel70-4.4.19/conf/mongodb.conf --shutdown
{"t":{"$date":"2023-04-04T07:08:58.184Z"},"s":"I", "c":"CONTROL", "id":20697, "ctx":"main","msg":"Renamed existing log file","attr":{"oldLogPath":"/usr/local/mls/mongoDB/mongodb-linux-x86_64-rhel70-4.4.19/log/mongodb.log","newLogPath":"/usr/local/mls/mongoDB/mongodb-linux-x86_64-rhel70-4.4.19/log/mongodb.log.2023-04-04T07-08-58"}}
There doesn't seem to be a server running with dbpath: /usr/local/mls/mongoDB/mongodb-linux-x86_64-rhel70-4.4.19/data'
$ ps aux|grep mongodb
root 28241 0.0 0.0 112824 988 pts/0 S+ 15:09 0:00 grep --color=auto mongodb
11. 开启登录验证
配置文件 mongodb.conf
... ...
# 登录验证
auth=true
12. 重启项目,重复第 7 步的命令
13. 测试登录验证
直接启动是可以启动起来,但是查询等的功能会提示命令需要认证
$ ./mongo
MongoDB shell version v4.4.19
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6f3c2dab-4f59-48dd-a9ac-54f40f05442d") }
MongoDB server version: 4.4.19
> use admin
switched to db admin
> show users
uncaught exception: Error: command usersInfo requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1659:15
shellHelper.show@src/mongo/shell/utils.js:914:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
14. 账号密码登录
$ ./mongo -u root p 123456
参考的文章:
https://blog.csdn.net/mouday/article/details/90670432
若文章不小心侵犯了您的合法权益,联系作者删除
若本文未能成功解决您的问题,欢迎在评论区讨论
若本文有错,请在评论区或私聊作者指正
如若转载,保留文章原出处