首页 > 解决方案 > Mongo auth fails to login when using mongod.conf

问题描述

So this is the weirdest thing.

I have two centOS 7 servers running mongo. I now wanted to enforce authentication so I added the security.authorization: enabled to the mongod.conf file.

I already have a user on database "buzzztv".

So when I ran mongod --conf /etc/mongod.conf on the first server everything went fine.

Then I did the exact same thing on the second server and whenever I try to connect with one of the users I get the following error:

connecting to: mongodb://127.0.0.1:27017/?authSource=buzzztv&compressors=disabled&gssapiServiceName=mongodb
2020-02-20T13:02:35.166+0000 E  QUERY    [js] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-02-20T13:02:35.168+0000 F  -        [main] exception: connect failed
2020-02-20T13:02:35.168+0000 E  -        [main] exiting with code 1

Now if I run mongod --fork --logpath /var/log/mongodb/mongod.log --auth the login works perfectly fine.

So obiously I could just run this command, but I want to use the mongod.conf.

Here is my mongod.conf file, I checked and it is a perfect copy of the file from the server in which it does work.

Any ideas?

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0 # 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

标签: mongodbcentoscentos7

解决方案


所以经过几个小时的研究,我需要改变

storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true

显然我的有问题,/var/lib/mongo所以我备份了数据,并创建了一个新文件夹/var/lib/mongodb

然后将 mongod.conf 文件编辑为:

storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

然后一切正常。现在我将重新创建用户并重新插入所有数据,一切顺利。

希望这可以节省我浪费的时间


推荐阅读