日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mongo创建用户和创建数据库

發(fā)布時間:2024/1/17 数据库 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongo创建用户和创建数据库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

================================mongo創(chuàng)建用戶和創(chuàng)建數(shù)據(jù)庫===========================

1、MongoDB

ps -ef | grep mongod

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

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

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

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

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

use admin 進入admin表
switched to db admin

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

#查看用戶是否創(chuàng)建成功

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

2、驗證創(chuàng)建用戶
[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.創(chuàng)建數(shù)據(jù)庫

#創(chuàng)建數(shù)據(jù)庫語句 use 數(shù)據(jù)庫名字

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

查看數(shù)據(jù)庫列表

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

4、為單個數(shù)據(jù)庫添加管理用戶
#切換到要添加用戶的數(shù)據(jù)庫中

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

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

5、mongo數(shù)據(jù)庫備份和還原:
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

數(shù)據(jù)還原:

導(dǎo)入成功數(shù)據(jù)
[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

轉(zhuǎn)載于:https://blog.51cto.com/guoshaoliang789/2107683

總結(jié)

以上是生活随笔為你收集整理的mongo创建用户和创建数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。