mongo創建用戶和創建數據庫

================================mongo創建用戶和創建數據庫===========================

1、MongoDB

ps -ef | grep mongod

普通用戶啓動方式
mongod --config /etc/mongod.conf

認證用戶啓動方式
mongod --auth --config /etc/mongod.conf

=======================================================================

正確的添加用戶和密碼方式:

1、添加超級管理員用戶
#mongo
添加一個超級管理員:用戶名:myadmin 密碼:ktm@123 數據庫:admin 角色:超級管理員。

use admin 進入admin表
switched to db admin

db.createUser(
... {
... user:"myadmin",
... pwd:"ktm@123",
... roles:[{role:"root",db:"admin"}]
... }
... )

#查看用戶是否創建成功

show users
{
"_id" : "admin.myadmin",
"user" : "myadmin",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}

2、驗證創建用戶
[root@localhost 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
#報錯,沒有驗證成功。
2018-04-25T02:10:47.497-0400 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :

db.auth("myadmin","ktm@123")

db.auth("myadmin","ktm@123")
1
show dbs
admin 0.000GB
local 0.000GB

說明驗證成功

3.創建數據庫

#創建數據庫語句 use 數據庫名字

use kaitaiming
switched to db kaitaiming
#查看當前選擇的命令
db
kaitaiming

查看數據庫列表

show dbs
admin 0.000GB
local 0.000GB
mydb 0.000GB
#創建的數據庫不存在,要顯示的數據庫,需要把他插入到至少一個文件
db.movie.insert({"name":"aaa"})
WriteResult({ "nInserted" : 1 })
#查看已經創建的數據庫
show dbs
admin 0.000GB
kaitaiming 0.000GB
local 0.000GB
mydb 0.000GB

4、爲單個數據庫添加管理用戶
#切換到要添加用戶的數據庫中

use 庫名
switched to db 庫名
db #查看庫
庫名
#創建用戶demo,密碼:123456
db.createUser({
user: 'demo',
pwd: '123456',
roles: [ { role: "readWrite", db: "庫名" } ]
})

Successfully added user: {
"user" : "demo",
"roles" : [
{
"role" : "readWrite",
"db" : "庫名"
}
]
}

5、mongo數據庫備份和還原:
mkdir /backup/date

[root@localhost backup]# mongodump -h 127.0.0.1 --port 27017 -d admin -

umyadmin -p 密碼 -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)
[root@localhost backup]# cd /backup/date/
[root@localhost date]# ll
total 0
drwxr-xr-x. 2 root root 128 Apr 25 01:38 admin
[root@localhost date]# cd admin/
[root@localhost admin]# ll
total 16
-rw-r--r--. 1 root root 288 Apr 25 01:38 system.users.bson
-rw-r--r--. 1 root root 183 Apr 25 01:38 system.users.metadata.json
-rw-r--r--. 1 root root 104 Apr 25 01:38 system.version.bson
-rw-r--r--. 1 root root 207 Apr 25 01:38 system.version.metadata.json

數據還原:

導入成功數據
[root@localhost admin]# mongorestore -h 127.0.0.1 --port 27017 -d admin -

umyadmin -p密碼 --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 are deprecated and will

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章