1、创建数据库
在MongoDB中创建数据库的命令使用的是use命令,该命令有两层含义
1)切换到指定数据库
2)如果切换的数据库不存在,则创建该数据库
我们使用use命令创建一个名为 pi 的数据库
> use pi
switched to db pi
>
2、查看所有数据库
通过show dbs | show databases 命令查看当前MongoDB中的所有数据库
# 启动mongodb客户端,连接到mongodb服务器
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# ./mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("856dad8b-52f2-4cc9-a5ea-dddeaa83d3c8") }
MongoDB server version: 4.2.1
# 切换为admin数据库
> use admin
switched to db admin
# 用户认证
> db.auth("pipi","love")
1
# 切换为 pi 数据库
> use pi
switched to db pi
# 查询所有使用过的数据库
> show dbs
admin 0.000GB
commonuser 0.000GB
config 0.000GB
local 0.000GB
# 在 pi 数据库中创建用户 a,一个用户可以拥有多个数据库的权限
> db.createUser({user:"a",pwd:"n",roles:[{role:"readWrite",db:"pi"}]})
Successfully added user: {
"user" : "a",
"roles" : [
{
"role" : "readWrite",
"db" : "pi"
}
]
}
>
测试用户是否创建成功
# mongodb配置了环境变量,所以可以在任意目录下启动
[root@iZ2ze5v2vdwv6veyksylhxZ ~]# mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3bae04d8-c13a-47bc-9ba4-fb73f22c6f05") }
MongoDB server version: 4.2.1
# 切换到 pi 数据库
> use pi
switched to db pi
# 用户认证
> db.auth("a","n")
1
>
使用具有数据库管理员角色的用户登录查询数据库
> use admin
switched to db admin
> db.auth("pipi","love")
1
> show users
{
"_id" : "admin.pipi",
"userId" : UUID("3c779c02-d43e-48a7-aa87-a3204c9ba2ef"),
"user" : "pipi",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "dbAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
> show dbs
admin 0.000GB
commonuser 0.000GB
config 0.000GB
local 0.000GB
>
在查询结果中并未包含我们刚刚创建的 pi 数据库,因为,在show dbs 命令中不显示未含有任何信息的数据库的库
使用普通用户登录查询数据库
我们在 pi 数据库中创建一个只具备读写权限的普通用户
> use pi
switched to db pi
> show dbs
admin 0.000GB
commonuser 0.000GB
config 0.000GB
local 0.000GB
> show users
{
"_id" : "pi.a",
"userId" : UUID("0d973857-9a60-473f-b2a8-15381a03073d"),
"user" : "a",
"db" : "pi",
"roles" : [
{
"role" : "readWrite",
"db" : "pi"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
>
3、删除数据库
在MongoDB中使用db.dropDatabase()函数来删除数据库。在删除数据库之前,需要使用具备dbAdminAnyDatabase角色的管理员用户登录,然后切换到需要删除的数据库,执行db.dropDatabase()函数即可。删除成功后会返回一个{ “ok” : 1 }的JSON字符串
# 进入mongodb的bin目录
[root@iZ2ze5v2vdwv6veyksylhxZ ~]# cd /usr/local/mongodb/mongodb-linux-x86_64-rhel70-4.2.1/bin/
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# ll
total 282152
-rwxr-xr-x 1 root root 10100811 Oct 16 2019 bsondump
-rwxr-xr-x 1 root root 7694 Oct 16 2019 install_compass
-rwxr-xr-x 1 root root 47534944 Oct 16 2019 mongo
-rwxr-xr-x 1 root root 71584856 Oct 16 2019 mongod
-rwxr-xr-x 1 root root 14765905 Oct 16 2019 mongodump
-rwxr-xr-x 1 root root 14511931 Oct 16 2019 mongoexport
-rwxr-xr-x 1 root root 14473296 Oct 16 2019 mongofiles
-rwxr-xr-x 1 root root 14690724 Oct 16 2019 mongoimport
-rwxr-xr-x 1 root root 18154584 Oct 16 2019 mongoreplay
-rwxr-xr-x 1 root root 15088133 Oct 16 2019 mongorestore
-rwxr-xr-x 1 root root 39806704 Oct 16 2019 mongos
-rwxr-xr-x 1 root root 14258080 Oct 16 2019 mongostat
-rwxr-xr-x 1 root root 13918118 Oct 16 2019 mongotop
# 启动mongodb服务器
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# ./mongod --config /usr/local/mongodb/mongodb-linux-x86_64-rhel70-4.2.1/etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4919
child process started successfully, parent exiting
# 启动mongodb客户端
[root@iZ2ze5v2vdwv6veyksylhxZ bin]# ./mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("472b1439-0efc-4cbd-9def-98b001439e91") }
MongoDB server version: 4.2.1
# 切换为admin数据库
> use admin
switched to db admin
# 用户认证为管理员
> db.auth("pipi","love")
1
# 切换为要删除的普通数据库中
> use commonuser
switched to db commonuser
# 删除此普通数据库
> db.dropDatabase()
{ "dropped" : "commonuser", "ok" : 1 }
>