Home  >  Article  >  Database  >  What should I do if the remote connection authentication fails under mongodb 3.4?

What should I do if the remote connection authentication fails under mongodb 3.4?

零下一度
零下一度Original
2017-07-03 16:33:201149browse

This article mainly introduces to you the solution to the remote connection authentication failure under mongodb 3.4. The article introduces it in detail through the sample code, which has certain reference and learning value for everyone. Friends in need Let’s follow the editor to learn together.

Preface

It is quite troublesome when mongodb turns on or off the authorization function. You need to create a new service and type mongod --auth. For convenience, I have built two services here, and I can switch to whichever service I use.


--需要授权

mongod --logpath "D:\data\log\mongodb.log" --logappend --dbpath "D:\data\db" --auth --serviceName 

"MongoDBService" --serviceDisplayName "MongoDBService" --install

--不需要授权

mongod --logpath "D:\data\log\mongodb.log" --logappend --dbpath "D:\data\db" --serviceName 

"MongoDBServiceNoAuth" --serviceDisplayName "MongoDBServiceNoAuth" --install

Let’s get to the point. After I configure mongodb here, I can enter it locally through db.auth('username','password') , remote access using client tools and c# code is not possible. Check that the firewall is not turned on, and the mongodb database is accessible by all IPs by default.

Later I accidentally saw "Using Robomongo to connect to MongoDB 3.x reported Authorization failed solution"This article just follow the above method.

The method is as follows:

1. First, I checked the database version and found that this is the case.


> use admin

switched to db admin

> db.system.version.find()

{ "_id" : "featureCompatibilityVersion", "version" : "3.4" }

{ "_id" : "authSchema", "currentVersion" : 5 }

2. Turn on the MongoDBServiceNoAuth service and use the db.dropUser('username') command to delete all the original ones User, execute the version change command again.


>db.dropUser('admin')

>db.dropUser('root')

>db.dropUser('dba')

>db.system.version.update({"_id":"authSchema"},{$set:{"currentVersion":3}})

3. Create a duplicate user

> db.createUser({user:"admin",pwd:"********",roles:[{"role":"userAdminAnyDataba

se","db":"admin"},{"role":"readWrite","db":"qxkf"}]})

Successfully added user: {

  "user" : "admin",

  "roles" : [

    {
      "role" : "userAdminAnyDatabase",

      "db" : "admin"

    },
    {
      "role" : "readWrite",

      "db" : "qxkf"

    }

  ]

}

4. You can use the Robomongo tool to check whether it can be accessed. It shows PASS here.

The above is the detailed content of What should I do if the remote connection authentication fails under mongodb 3.4?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn