文章目錄
- 一、系統(tǒng)環(huán)境
- 二、MongoDb安裝
- 添加MongoDB官方庫
- 安裝MongoDB
- 配置MongoDB
- 三、MongoDB常見操作
- 四、MongoDB用戶管理
-
- 五、啟用安全控制
- 六、備份與還原
-
- 七、外部工具連接MongoDB
一、系統(tǒng)環(huán)境
- CentOS Stream 9 64bit

二、MongoDb安裝
添加MongoDB官方庫
- 執(zhí)行以下命令
sudo vim /etc/yum.repos.d/mongodb-org-4.4.repo
- 在文件中添加以下內(nèi)容
[mongodb-org-4.4]name=MongoDB Repositorybaseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
- 如不熟悉vim編輯器操作,可手動在
/etc/yum.repos.d/
目錄下創(chuàng)建mongodb-org-4.4.repo
文件,打開文件后,在文件內(nèi)添加以上文件內(nèi)容,保存
安裝MongoDB
sudo yum install mongodb-org -y
配置MongoDB
- 打開配置文件
sudo vim /etc/mongod.conf
- 以下為mongodb常見配置及說明
# mongod.conf# 所有配置項文檔:
# http://docs.mongodb.org/manual/reference/configuration-options/# 配置與日志相關(guān)信息
# 是否追加方式寫入日志,默認True
# 日志文件的路徑
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod.log# 配置與存儲相關(guān)信息
# 數(shù)據(jù)庫文件位置
storage:dbPath: /var/lib/mongojournal:enabled: true
# engine:
# wiredTiger:# 配置與網(wǎng)絡(luò)相關(guān)信息
# 默認127.0.0.1 只能通過本地連接
# 0.0.0.0 任意地址遠程連接
net:port: 27017bindIp: 0.0.0.0# 配置流程管理相關(guān)信息
# 是否以守護進程方式運行,默認false
processManagement:fork: truetimeZoneInfo: /usr/share/zoneinfo# 啟用安全控制
# security:
# authorization: enabled
- 按需修改配置后,保存關(guān)閉
三、MongoDB常見操作
- 啟動MongoDB
sudo systemctl start mongod
- 關(guān)閉MongoDB
sudo systemctl stop mongod
- 重啟MongoDB
sudo systemctl restart mongod
- 查看當(dāng)前MongoDB狀態(tài)
sudo systemctl status mongod
- 將MongoDB設(shè)置為系統(tǒng)服務(wù)
sudo systemctl enable mongod
- 登錄MongoDB
mongo
四、MongoDB用戶管理
創(chuàng)建用戶
- 切換到admin數(shù)據(jù)庫:
use admin
- 創(chuàng)建管理員
admin
:
db.createUser({ user: "admin", pwd: "123456", roles: [{ role: "readWriteAnyDatabase", db: "admin" },{ role: "userAdminAnyDatabase", db: "admin" },{ role: "dbAdminAnyDatabase", db: "admin" }
]});
- 參數(shù)釋義:* roles:數(shù)組形式,配置具體權(quán)限+ `role: "readWriteAnyDatabase"`表示有讀寫任意數(shù)據(jù)庫的權(quán)限+ `db: "admin"`,表示:當(dāng)前權(quán)限只對`admin`數(shù)據(jù)庫生效
- `admin`數(shù)據(jù)庫,是 MongoDB 的管理級的特殊數(shù)據(jù)庫,是有特殊意義的。
- 所以`readWriteAnyDatabase`這個權(quán)限雖然設(shè)置給了`admin`數(shù)據(jù)庫,但實際上,`admin`數(shù)據(jù)庫里還擁有`readWriteAnyDatabase`這個權(quán)限的`admin`用戶,所以,該用戶對任意數(shù)據(jù)庫都具有讀寫的功能,不只局限在`admin`數(shù)據(jù)庫。
- **注意**:只有`admin`數(shù)據(jù)庫,才有`readWriteAnyDatabase`權(quán)限,如果是自己創(chuàng)建的數(shù)據(jù)庫,無法為用戶設(shè)置`readWriteAnyDatabase`權(quán)限,會報錯該數(shù)據(jù)庫沒有這個權(quán)限的用戶。
- 創(chuàng)建超級管理員
root
:
db.createUser({ user: "root", pwd: "123456", roles: [{ role: "root", db: "admin" }
]});
- `root`權(quán)限也只能放在`admin`數(shù)據(jù)庫內(nèi)才能生效
- `root`是最高權(quán)限,可以做任何事情
- 創(chuàng)建普通用戶
zhangsan
:
db.createUser({ user: "zhangsan", pwd: "123456", roles: [{ role: "readWrite", db: "school" },{ role: "userAdmin", db: "school" },{ role: "readWrite", db: "myBlog" },{ role: "userAdmin", db: "myBlog" }
]});
- MongoDB常用權(quán)限
read
:允許讀取指定數(shù)據(jù)庫中數(shù)據(jù)的權(quán)限。readWrite
:允許讀、寫指定數(shù)據(jù)庫中數(shù)據(jù)的權(quán)限。dbAdmin
:允許對指定數(shù)據(jù)庫中執(zhí)行管理函數(shù)的權(quán)限,如索引創(chuàng)建、刪除,查看統(tǒng)計或訪問 system.profile。userAdmin
:允許對指定數(shù)據(jù)庫執(zhí)行用戶管理的權(quán)限,比如創(chuàng)建、刪除和修改用戶。dbOwner
:允許對指定數(shù)據(jù)庫執(zhí)行任何管理操作。該角色結(jié)合了readWrite 、 dbAdmin和userAdmin角色授予的權(quán)限。<font style="color:#E4495B;">readAnyDatabase</font>
:只對admin數(shù)據(jù)庫可用,授予用戶對所有數(shù)據(jù)庫的read權(quán)限。<font style="color:#E4495B;">readWriteAnyDatabase</font>
:只對admin數(shù)據(jù)庫可用,授予用戶對所有數(shù)據(jù)庫的readWrite權(quán)限。<font style="color:#E4495B;">userAdminAnyDatabase</font>
:只對admin數(shù)據(jù)庫可用,授予用戶對所有數(shù)據(jù)庫的userAdmin權(quán)限。<font style="color:#E4495B;">dbAdminAnyDatabase</font>
:只對admin數(shù)據(jù)庫可用,授予用戶對所有數(shù)據(jù)庫的dbAdmin權(quán)限。<font style="color:#AD1A2B;">root</font>
:只對admin數(shù)據(jù)庫可用。超級賬號,超級權(quán)限。
修改密碼
db.updateUser("用戶名", {pwd: "新密碼"})
刪除用戶
五、啟用安全控制
- 修改
mongodb.conf
配置文件:
# 啟用安全控制
security:authorization: enabled
- 重啟 MongoDB 服務(wù):
sudo systemctl restart mongod
- 重新進入MongoDB sheel:
mongo
- 選擇要驗證的數(shù)據(jù)庫:
use admin
- 保存了需要驗證的用戶信息的數(shù)據(jù)庫
- 驗證用戶信息:
db.auth("用戶名", "密碼")
- 返回值為
1
,表示驗證通過。驗證失敗有error提示
六、備份與還原
1. 備份
- 語法:
mongodump --host 服務(wù)器地址 --port 端口 --db 要備份的數(shù)據(jù)庫名 --out 備份文件存儲目錄
mongodump --host localhost --port 27017 --db my_DB_name --out C:\Users\Administrator\Desktop\mydb.dump
2. 恢復(fù)
- 語法:
mongorestore --host localhost:27017 -u用戶名 -p密碼 --authenticationDatabase=驗證數(shù)據(jù)庫 備份文件目錄 --drop
mongorestore --host localhost:27017 -uroot -p123456 --authenticationDatabase=admin C:\Users\Administrator\Desktop\mydb.dump --drop
-
mongorestore
參數(shù)詳解:
-h
:--host=<hostname>
:連接地址--port=<port>
:端口號 -u
:--username=<username>
:用戶名-p
:--password=<password>
:密碼--authenticationDatabase=<db-name>
:驗證數(shù)據(jù)庫名--authenticationMechanism=<mechanism>
:驗證機制-d
:--db=<db-name>
:指定恢復(fù)的數(shù)據(jù)庫,如果不指定-d,會從備份目錄中獲取數(shù)據(jù)庫名-c
:--collection<collection-name>
:指定恢復(fù)的集合,如果不指定-c,會從備份目錄中獲取集合名--drop
:導(dǎo)入集合前先刪掉集合,不會刪除不會備份中的集合--gzip
:從壓縮文件中進行恢復(fù)
七、外部工具連接MongoDB
- 可視化管理工具(此處以Navicat示例):

- NodeJs(以mongoose驅(qū)動為例):
{"name": "mongodb_test","version": "0.0.0",..."dependencies": {..."mongoose": "^8.4.0",...}...
}
const mongoose = require('mongoose');
const host = '數(shù)據(jù)庫地址';
const database = 'dbName';
const port = 27017;const username = '用戶名';
const password = '密碼';
const authSource = 'admin';
const authMechanism = 'DEFAULT'; const connectionString = `mongodb://${username}:${password}@${host}:${port}/${database}?authSource=${authSource}&authMechanism=${authMechanism}`;
mongoose.connect(connectionString)
.then(() => console.log('數(shù)據(jù)庫連接成功'))
.catch(err => console.error('數(shù)據(jù)庫連接失敗', err));module.exports = mongoose;