当前位置:网站首页>MongoDB permission verification is turned on and mongoose database configuration
MongoDB permission verification is turned on and mongoose database configuration
2022-08-04 23:42:00 【@weida】
ExpressThe most commonly used databases for projects aremongodb,and with modulesmongoose完成对数据库的访问.
This article assumes that you have done it correctlymongodb
数据库的安装,下面是针对mongodb
The setup and project configuration work.
一、为mongodbDatabase settings permission verification
在mongodb
安装完成之后,Permission verification is turned off by default,That is to say, you can access the data in the database without using any password.,This practice is very dangerous in a production environment.
如何查看我们的mongodb
Does the database have permission verification enabled??非常简单,我们只需要打开mongodb
的安装目录,found in it named/bin/mongod.cfg
的配置文件,查看其中security
选项下的authroization
配置,If the option is commented out or empty,Indicates that permission verification is not enabled.
#security:
#authorization: enabled
配置文件中的
#
表示注释,这里虽然authorization
被设置为了enabled
,But because it is commented out, it still means that permission verification is not turned on.
如果您的mongodb
Permission verification has not been turned on,Follow the steps below to complete the setup.
1.1 创建超级管理员
mongodb
在完成安装之后,Three databases are created by default,分别为admin
、config
、local
.其中,admin
The database administrator ismongodb
的超级管理员.
Creating a super administrator requires two commands:
use admin
db.createUser({
user:'admin',
pwd:'123456',
roles:[{
role:'root',db:'admin'}]
})
The above command will beadmin
数据库创建一个名为admin
的超级管理员(角色为root
),其密码为123456
(Be sure to use a complex password for production environments,to ensure database security).
After the super administrator is created,我们可以使用命令show users
查看当前数据库中的用户,The process of creating a super administrator is as follows:
> use admin
switched to db admin
> db.createUser({
... user:'admin',
... pwd:'123456',
... roles:[{
role:'root',db:'admin'}]
... })
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> show users
{
"_id" : "admin.admin",
"userId" : UUID("c17aa709-a1d8-4a76-8d5b-0b61bb589d49"),
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
1.2 修改mongod.cfg
配置
修改/bin/mongod.cfg
文件中的security
选项如下:
security:
authorization: enabled
1.3 重启mongodb服务
- 使用快捷键
win + R
,并输入services.msc
,然后回车
- 在服务列表中找到
MongoDB Server (MongoDB)
- 右键选择
重新启动
- Waiting for the service is restarted
1.4 使用密码登录mongodb
使用如下命令,连接数据库,If you can successfully connect to the database,Indicates that we have turned on permission verification,and created a super administrator.
> mongo admin -u admin -p 123456
After enabling permission verification,使用
mongo
command can still connectmongodb
数据库,但是,如果我们使用show dbs
Command to view the database will encounter a permission error,or can't see the database list.
After completing the creation of the super administrator,We also need to create one more user for the project database,Used to access the project database.
这就和使用Linux
Do not use the super administrator directly in the systemroot
The same is true for direct operating system resources.
1.5 Create a user for the project database
Suppose now that a database for the project has been createddb_ahoh
,You can use the following command to create an access-onlyad_ahoh
的用户ahohAdmin
.
use db_ahoh
db.createUser({
user:"ahohAdmin",
pwd:"123456",
roles:[{
role:"dbOwner",db:"db_ahoh"}]
})
The above command is databasedb_ahoh
创建了一个名为ahohAdmin
的用户,This is the role of the userdbOwner
,只能访问db_ahoh
一个数据库.
在
mongodb
中,To create a database, you only need to insert a piece of data into the database.,例如,The following two commands can create a databasedb_ahoh
:
use db_ahoh
db.tb_users.insert({username:"xiaoming"})
The above command when creating the database,Created a table by the way
tb_users
,并插入了一条数据.
1.6 Log in with the project database user
在完成ahohAdmin
用户创建之后,You can use the following command to connect to the database:
mongo db_ahoh -u ahohAdmin -p 123456
此时,如果我们执行show dbs
指令,only see one database:
> show dbs
db_ahoh 0.000GB
二、使用mongoose连接数据库
mongoosemodules are operationsmongodb
的 不二之选,我们需要先安装mongoose
包之后,Re-connect to the database.
2.1 安装mongoose模块
使用以下命令,安装mongoose
模块:
npm i mongoose
2.2 编辑数据库连接URI
Similar to other relational databases,在连接数据库的时候,Requires a string similar to a web page connection,Used to indicate the name of the database to connect to、端口、IP、用户名和密码等信息,格式如下:
mongodb://<username>:<password>@<IP>:<port>/<database>
If you and the database configured in this article are exactly the same,那么您可以使用下面的URI连接mongodb
数据库:
mongodb://ahohAdmin:123456@localhost:27017/db_ahoh
这个重要的链接Often placed in a separate configuration file,Rather than hard coded in the code.
2.3 创建配置文件config.js
在node
项目中,We usually use aconfig.js
File to store some important configuration information.
在项目根目录下创建文件夹config/
,然后在其中创建一个名为config.js
的配置文件.
Edit the content in the configuration file as follows:
/* config/config.js */
module.exports = {
mongoURI: "mongodb://ahohAdmin:[email protected]:27017/db_ahoh",
}
2.4 使用mongoose连接数据库
in the main file of the projectapp.js
中执行以下步骤,连接mongodb
数据库.
- 引入
mongoose
模块
const mongoose = require('mongoose')
- 使用
uri
连接数据库
const dbUrl = require('./config/config').mongoURI
mongoose.connect(dbUrl)
.then(()=>{
console.log('MongoDB connected!') }) //连接成功输出 MongoDB connected!
.catch(err => console.log(err))
- 测试连接是否成功
执行命令npm run serve
启动系统,观察控制台输出.
PS E:\Code\Express\express-server> npm run serve
> [email protected] serve
> nodemon ./bin/www
[nodemon] 2.0.16
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./bin/www`
MongoDB connected!
输出MongoDB connected!
表示数据库连接成功.
三、项目代码
边栏推荐
- 小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串
- 基于深度学习的路面坑洞检测(详细教程)
- [QNX Hypervisor 2.2用户手册]10.6 vdev mc146818
- Nuclei(二)进阶——深入理解workflows、Matchers和Extractors
- uniapp横向选项卡(水平滚动导航栏)效果demo(整理)
- web3.js
- 如何写好测试用例
- The role of @ Import annotations as well as how to use
- what is MVCC
- Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
猜你喜欢
web3.js
Ab3d.PowerToys and Ab3d.DXEngine Crack
加解密在线工具和进制转化在线工具
【七夕情人节特效】-- canvas实现满屏爱心
[Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)
NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
Day118. Shangyitong: order list, details, payment
jenkins发送邮件系统配置
Kernel函数解析之kernel_restart
基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
随机推荐
kernel hung_task死锁检测机制原理实现
2022年华数杯数学建模
uniapp 分享功能-分享给朋友群聊朋友圈效果(整理)
3年,从3K涨薪到20k?真是麻雀啄了牛屁股 — 雀食牛逼呀
Xiaohei leetcode surfing: 94. Inorder traversal of binary tree
【七夕情人节特效】-- canvas实现满屏爱心
Kernel函数解析之kernel_restart
The role of the annotation @ EnableAutoConfiguration and how to use
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
OPENCV学习DAY8
golang 协程的实现原理
Web安全开发 | 青训营笔记
【软件测试】常用ADB命令
基于Appian低代码平台开发一个SpaceX网站
[QNX Hypervisor 2.2用户手册]10.4 vdev hpet
2022牛客暑期多校训练营5(BCDFGHK)
App测试和Web测试的区别
MYS-6ULX-IOT 开发板测评——使用 Yocto 添加软件包
Day118.尚医通:订单列表、详情、支付
web3.js