================================mongo Create a user and create a database ===========================
1. MongoDB
Ps-ef | grep Mongod
Normal User startup mode
Mongod--config/etc/mongod.conf
Authentication User startup mode
Mongod--auth--config/etc/mongod.conf
=======================================================================
To add users and passwords correctly:
1. Add Super Admin user
#mongo
Add a Super Administrator: User name: myadmin password: [email protected] Database: Admin role: Super Administrator.
Use admin to enter admin table
Switched to DB admin
Db.createuser (
... {
... User: "MyAdmin",
... pwd: "[email protected]",
... roles:[{role: "Root", DB: "admin"}]
... }
... )
#查看用户是否创建成功
Show Users
{
"_id": "Admin.myadmin",
"User": "MyAdmin",
"DB": "admin",
"Roles": [
{
"Role": "Root",
"DB": "admin"
}
]
}
2. Verify that the user is created
[Email protected] admin]# MONGO
MongoDB Shell version v3.4.14
Connecting to:mongodb://127.0.0.1:27017
MongoDB Server version:3.4.14
Use admin
Switched to DB admin
Show DBS
#报错, no validation was successful.
2018-04-25t02:10:47.497-0400 E QUERY [thread1] error:listdatabases failed:{
"OK": 0,
"ErrMsg": "Not authorized in Admin to execute command {listdatabases:1.0}",
"Code": 13,
"codename": "Unauthorized"
} :
Db.auth ("MyAdmin", "[email protected]")
Db.auth ("MyAdmin", "[email protected]")
1
Show DBS
Admin 0.000GB
Local 0.000GB
Description Validation Successful
3. Create a Database
#创建数据库语句 Use database name
Use kaitaiming
Switched to DB kaitaiming
#查看当前选择的命令
Db
Kaitaiming
View the list of databases
Show DBS
Admin 0.000GB
Local 0.000GB
MyDB 0.000GB
#创建的数据库不存在, to display the database, you need to insert him into at least one file
Db.movie.insert ({"Name": "AAA"})
Writeresult ({"ninserted": 1})
#查看已经创建的数据库
Show DBS
Admin 0.000GB
Kaitaiming 0.000GB
Local 0.000GB
MyDB 0.000GB
4. Add an administrative user to a single database
#切换到要添加用户的数据库中
Use library name
switched to DB Library name
DB #查看库
Library name
#创建用户demo, Password: 123456
Db.createuser ({
User: ' Demo ',
PWD: ' 123456 ',
Roles: [{role: ' ReadWrite ', DB: ' Library name '}]
})
Successfully added User: {
"User": "Demo",
"Roles": [
{
"Role": "ReadWrite",
"DB": "Library name"
}
]
}
5. MONGO database backup and restore:
Mkdir/backup/date
[Email protected] backup]# mongodump-h 127.0.0.1--port 27017-d Admin-
Umyadmin-p Password-o/backup/date
2018-04-25t01:38:32.043-0400 writing Admin.system.users to
2018-04-25t01:38:32.043-0400 done Dumping Admin.system.users (1 document)
2018-04-25t01:38:32.043-0400 writing admin.system.version to
2018-04-25t01:38:32.044-0400 done Dumping Admin.system.version (2
Documents
[Email protected] backup]# cd/backup/date/
[email protected] date]# LL
Total 0
Drwxr-xr-x. 2 root root Apr 01:38 admin
[Email protected] date]# CD admin/
[email protected] admin]# LL
Total 16
-rw-r--r--. 1 root root 288 Apr 01:38 System.users.bson
-rw-r--r--. 1 root root 183 Apr 01:38 System.users.metadata.json
-rw-r--r--. 1 root root 104 Apr 01:38 System.version.bson
-rw-r--r--. 1 root root 207 Apr 01:38 System.version.metadata.json
Data restore:
Import Success Data
[Email protected] admin]# mongorestore-h 127.0.0.1--port 27017-d Admin-
Umyadmin-p Password--drop/backup/date/admin
2018-04-25t01:44:49.065-0400 the--db and--collection args should only
be used when restoring from a BSON file. Other uses is deprecated and would
Not exist in the future; Use--nsinclude instead
2018-04-25t01:44:49.065-0400 building a list of collections to restore
From/backup/date/admin dir
2018-04-25t01:44:49.066-0400 restoring users from
/backup/date/admin/system.users.bson
2018-04-25t01:44:49.083-0400 Done
MONGO creating users and creating databases