This document is very good https://blog.csdn.net/u010523770/article/details/54599548
Add Superuser First:
Use admin
db.createuser (
{
User: "admin",
pwd: "Password",
roles: [{role: "root", DB: "admin"}]
}
);
MongoDB 3 Add a new user:Db.createuser ({"User": "Test1",
"pwd": "Test1",
"CustomData": {employeeid:12345},
"Roles": [{role: ' Clusteradmin ', db: ' admin '},
{role: "Readanydatabase", DB: "Admin"},
"ReadWrite"
] },
{w: "Majority", wtimeout:5000})
Reprint Source: http://www.cnblogs.com/zj1111184556/p/3599828.html
Installing MongoDB
sudo apt-get install MongoDB
sudo apt-get install MongoDB
User Action (2.4.9 above):
Mongo
User admin;
Db.auth ("', '");
db[' System.users '].find ({}, {user:1, pwd:1, _id:0});
To delete a user:
Db.removeuser (' Root2 ')
To modify a user's password:
Db.changeuserpassword ("Accountuser", "SOH3TBYHX8YPJPXMT1OOFL")
Close/Start
sudo service MongoDB stop
sudo service MongoDB start
To set the database connection password:
Creating a folder with directory: data/db
Close the existing service.
sudo service MongoDB stop
Restart Service
$ mongod–auth
Create a connected user
$ MONGO
> Use admin
Switched to DB admin
> Db.adduser ("Root", "1983")
Close the Service (Command window launched directly in Mongod "CTRL + C")
Restart Service:
$: Mongod–auth
To see if validation starts:,
$ MONGO
MongoDB Shell version:2.0.4
Connecting To:test
> Use admin
Switched to DB admin
> Show Collections
Fri Mar 09:07:08 uncaught exception:error: {
"$err": "Unauthorized db:admin lock Type:-1 client:127.0.0.1",
"Code": 10057
}
There is a hint link error.
For user authentication:
> Db.auth ("Root", "1983")
1
Re-view to view datasets
> Show Collections
System.indexes
System.users
To set up a client connection:
The default installation allows only 127.0.0.1 IP connections.
The following records need to be modified/etc/mongodb.conf comments:
Open File:
$ sudo gedit/etc/mongodb.conf
Note Record:
#bind_ip = 0.0.0.0
A later version of MongoDB 2.6 Adds a user method, each library requires authorization to access:
https://docs.mongodb.org/manual/reference/method/db.createUser/
Use the Products
Db.createuser (
{
User: "Accountuser",
pwd: "Password",
roles: ["ReadWrite", " DbAdmin "]
}
)
After installing the user in centos6.5 above the connection error, check the log mongodb.log:
AUTHENTICATIONFAILED:MONGODB-CR credentials missing in the user document
Very feeling the answer of this elder brother Vivek (Http://stackoverflow.com/users/2512718/vivek):
Http://stackoverflow.com/questions/29006887/mongodb-cr-authentication-failed
Go to MongoDB console and delete your current user & set Authschema version to 3 instead of 5, follow these commands In MONGO console-
Mongo
Use admin
Db.system.users.remove ({}) <== removing all users
Db.system.version.remove ({}) <== removing current version
Db.system.version.insert ({"_id": "Authschema", "CurrentVersion": 3})
Now restart the Mongod and create new user then it should work fine.
Note:use remove commands in test db only, if in production use Update.
Restart MongoDB and then use the following command to create a new user, as normal:
Db.createuser (
{
User: "username",
PWD: "passwd",
roles: ["ReadWrite", "DbAdmin"]
}
)
=========================
Db.createuser (
{
User: "11",
PWD: "11",
Roles: [{role: ' Useradminanydatabase ', db: ' admin '}]
}
)
Admin Library:
Db.grantrolestouser (
"Admin",
[
{role: "ReadWrite", DB: "Admin"},
{role: "Readanydatabase", DB: "Admin"},
]
)
General Library
Db.grantrolestouser (
"Admin",
[
{role: "ReadWrite", DB: "Test"},
{role: "ReadWrite", DB: "Files"},
]
)
To set up auto-start:
UPDATE-RC.D MongoDB Defaults