================================mongo创建用户和创建数据库===========================
1、MongoDB
ps -ef | grep mongod
普通用户启动方式
mongod --config /etc/mongod.conf
认证用户启动方式
mongod --auth --config /etc/mongod.conf
=======================================================================
正确的添加用户和密码方式:
1、添加超级管理员用户
#mongo
添加一个超级管理员:用户名:myadmin 密码:[email protected] 数据库:admin 角色:超级管理员。
use admin 进入admin表
switched to db admindb.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、验证创建用户
[[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
#报错,没有验证成功。
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","[email protected]")
db.auth("myadmin","[email protected]")
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
[[email protected] 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)
[[email protected] backup]# cd /backup/date/
[[email protected] date]# ll
total 0
drwxr-xr-x. 2 root root 128 Apr 25 01:38 admin
[[email protected] date]# cd admin/
[[email protected] 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
数据还原:
导入成功数据
[[email protected] 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
原文地址:http://blog.51cto.com/guoshaoliang789/2107683