mongodb create user, authenticate connection, java connection

Article Directory

Under normal circumstances, the mongoDB database must establish an authentication connection, which requires the user to create an administrator and user authentication for the database. For mongoDB, when creating an administrator and a user, the assigned roles are the key to determining the user's authority to operate the database. .


Database User Roles
read: Authorize User to read-only data, allow user to read specified database
readWrite Authorize User to read/write data, allow user to read/write specified database

Database Admininstration Roles
dbAdmin: perform management operations in the current database, such as index creation, deletion, statistics, viewing, etc.
dbOwner: perform any operation in the current database, add, delete, modify, check, etc.
userAdmin : Manage User in the current database, create, delete and manage users.

Backup and restore the role of (the Roles Backup and Restoration)

All-Database Roles
readAnyDatabase: Authorize the permission to read data on all databases, only available in admin
readWriteAnyDatabase: Authorize the permission to read and write data on all databases, only available in admin
userAdminAnyDatabase: Authorization Manage User permissions on all databases, only available in admin
dbAdminAnyDatabase: Authorize the permissions to manage all databases, only available in admin

Cluster Administration Roles (Cluster Administration Roles)
clusterAdmin: the highest authority authorized to manage the cluster, only available in the admin
clusterManager: authority to manage and monitor the cluster
clusterMonoitor: authority to monitor the cluster, with readonly authority to the monitoring tool
hostManager: management server

Super role (super master Roles)
root: super account and permissions, only available in admin

mongodb set account and password

The mongodb password is somewhat different from traditional data such as mysql: The user name and password of mongodb are based on a specific database, not on the entire system. All database db need to set a password.
1. Create an administrator account

use admin
Insert picture description here

The user in mongodb is based on the identity role, and the role of the administrator account is userAdminAnyDatabase. The admin user is used to manage accounts and cannot perform operations such as closing the database.
2. Created root

Insert picture description here

3. Create your own database management role

use kgcdsj
Insert picture description here

4. View users

show users
Insert picture description here

5. Delete user
Delete user must be deleted by account administrator, switch to admin role

use admin

Authentication connection

1. vi mongo.conf


2. Verification

./bin/mongod  -config mongo.conf
show dbs //mongo进入数据库,没有权限
use kgcdsj  //选中要使用的数据库
db.auth("kgcuser","123456") //权限验证 验证通过后返回1
show tables;

java connection

ServerAddress serverAddress = new ServerAddress("", 27017);
        ArrayList<ServerAddress> addrs = new ArrayList<>();
        MongoCredential scramSha1Credential = MongoCredential.createScramSha1Credential("kgcuser", "kgcdsj", "123456".toCharArray());
        ArrayList<MongoCredential> credentials = new ArrayList<>();
        MongoClient mongoClient = new MongoClient(addrs, credentials);
        MongoDatabase kgcdsj = mongoClient.getDatabase("kgcdsj");
        MongoIterable<String> strings = kgcdsj.listCollectionNames();
        for (String string : strings) {
        MongoCollection<Document> students = kgcdsj.getCollection("students");
Insert picture description here