安装版本:db version v4.2.6
MongoDB 默认安装并没有开启用户名密码登录,这样太不安全。首先开启一个无需验证用户的 MongoDB 服务后,进入 MongoDB 的 shell 控制台创建用户:
use admin db.createUser( { user: "admin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] } )
创建用户后,关闭连接,进入正题:
安装路径: D:PROGRA~1\MongoDB\Server\4.2\bin\
配置文件中添加 authorization 项 ,配置文件地址如下,没有可创建 D:PROGRA~1\MongoDB\Server\4.2\bin\mongod.cfg
配置文件内容如下:
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: D:\PROGRA~1\MongoDB\Server\4.2\data\db journal: enabled: true # engine: # mmapv1: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: D:\PROGRA~1\MongoDB\Server\4.2\log\mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1 #processManagement: security: authorization: enabled setParameter: enableLocalhostAuthBypass: false #operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp:
配置好之后注册服务:
D:\PROGRA~1\MongoDB\Server\4.2\bin\mongod.exe --config "D:\PROGRA~1\MongoDB\Server\4.2\bin\mongod.cfg" --install --serviceName "MongoDB" --serviceDisplayName "MongoDB"
启动服务:
net start MongoDB
遇到的问题:
Error1:
F CONTROL [main] Failed global initialization: BadValue: dbPath requires an absolute file path with Windows services
Error2:
Error parsing YAML config file: yaml-cpp: error at line 2
以上都是配置文件配置问题,注意文件中不要出现单引号,双引号之类的;
配置项每层缩进都是 4 个空格,不要用 tab 缩进
Error3:
控制台显示服务未注册
这个一般是注册服务的命令有引号错误或者路径错误,检查有没有少斜线。正常的注册完服务并启动时,会显示服务已开启。
Microsoft Windows [版本 10.0.18363.657] HLJYY@YJY-108 F:\Program # D:\PROGRA~1\MongoDB\Server\4.2\bin\mongod.exe --config "D:\PROGRA~1\MongoDB\Server\4.2\bin\mongod.cfg" --install --serviceName "MongoDB" --serviceDisplayName "MongoDB" HLJYY@YJY-108 F:\Program # net start MongoDB MongoDB 服务正在启动 . MongoDB 服务已经启动成功。 HLJYY@YJY-108 F:\Program #
新开窗口进入 mongodb 的 shell 控制台验证:
Microsoft Windows [版本 10.0.18363.657] HLJYY@YJY-108 F:\Program # mongo MongoDB shell version v4.2.6 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("46e409af-7220-4fed-965c-bb03099fec8d") } MongoDB server version: 4.2.6 > show dbs > use admin switched to db admin > db.auth("admin","123456") 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB test 0.000GB >
参考链接:
Mongodb 学习(4)通过配置文件启动 mongod
windows 系统,MongoDB 开启用户验证
解决“Error parsing YAML
权限认证已 ok
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于