首先官网下载:https://www.mongodb.com/try/download/community
MongoDB安装包括两部分,一个是将MongoDB的服务下载下来并且安装到自己电脑;一个是可视化界面(解释:你下了MongoDB服务当然要用起来啊,固然你可以使用命令行的客户端,但是建议你安装MongoDB Compass可视化界面,用起来很方便)
我这安装的是4.4.5,且下载是 zip
压缩包,下载完成后,解压

子啊根目录下,分别创建 data
、logs
目录,并创建文件 logs/mongo.log
,接着根目录创建配置文件 mongo.conf
dbpath=D:\soft\mongodb_64_windows_4.4.5\data #数据库路径
logpath=D:\soft\mongodb_64_windows_4.4.5\logs\mongo.log #日志输出文件路径
logappend=true #错误日志采用追加模式
journal=true #启用日志文件,默认启用
quiet=true #这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
port=27017 #端口号 默认为27017
打开cmd进入到D:\soft\mongodb_64_windows_4.4.5\\bin
,执行命令
mongod --config ../mongo.conf
执行后没被错就对了,等几秒,然后 crtl+c 停止,接着执行命令
mongod --dbpath ../data
显示
// ...
{"t":{"$date":"2021-04-18T14:06:00.955+08:00"},"s":"I", "c":"NETWORK", "id":23016, "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
看到最后一行,显示这个等待连接就启动成功了,然后当前窗口不要关闭,再次进入 bin
目录打开 cmd 窗口,输入 mongo
,显示如下:
D:\soft\mongodb_64_windows_4.4.5\bin>mongo
MongoDB shell version v4.4.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c2e5b7a3-5ef6-4817-9464-346fdf23829e") }
MongoDB server version: 4.4.5
---
The server generated these startup warnings when booting:
2021-04-18T14:06:00.813+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-04-18T14:06:00.813+08:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> db
test
>
看到如上信息,则安装成功。
添加到服务
运行命令
mongod --config D:\soft\mongodb_64_windows_4.4.5\mongo.conf --install --serviceName "MongoDB"
如遇问题
D:\soft\mongodb_64_windows_4.4.5\bin>mongod --config D:\soft\mongodb_64_windows_4.4.5\mongo.config --install --serviceName "MongoDB"
Error opening config file: Is a directory
try 'mongod --help' for more information
请以管理员方式运行 cmd 窗口
配置登录密码
先 cmd 打开窗口
mongo
执行,登录上去,执行如下
use admin
db.createUser({user: "root", pwd: "123456", roles:["root"]})
注:只有admin数据库才有root角色
(内置角色):
1. 数据库用户角色:read、readWrite;
2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;
3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4. 备份恢复角色:backup、restore;
5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6. 超级用户角色:root
// 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
7. 内部角色:__system
然后,先删除服务 sc delete MongoDB
,打开配置文件加上如下:
auth=true
在执行
mongod --auth --config D:\soft\mongodb_64_windows_4.4.5\mongo.conf --install --serviceName "MongoDB"
然后,执行命令,然后启动,接着打开 cmd 窗口,输入 mongo
需要登录
use admin
db.auth("root","123456")
返回1 说明成功
网友评论