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. .

role

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)
Backup
Restore

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
db.createUser({user:"admin",pwd:"adminadmin",roles:[{role:"userAdminAnyDatabase",db:"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

db.createUser({user:"root",pwd:"root",roles:[{role:"root",db:"admin"}]})
Insert picture description here


3. Create your own database management role

use kgcdsj
db.createUser({user:"kgcuser",pwd:"123456",roles:[{role:"dbOwner",db:"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
db.auth("admin","adminadmin")
//删除单个用户
db.system.users.remove({user:"xxxxx"})
//删除所有用户
db.system.users.remove({})

Authentication connection

1. vi mongo.conf

security:
	authorization:enabled

2. Verification

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

java connection

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