【阿里云服务器Ubuntu数据库部署MongoDB】

本文详细介绍如何在阿里云服务器Ubuntu上部署MongoDB数据库,包括安装配置、用户管理及权限测试等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


由于项目要使用的数据格式基本为JSON类型,故考虑使用该种数据库在云端进行部署。

Ubuntu 安装 MongoDB 以及基本配置

$ sudo apt-get install mongodb # apt-get 安装 mongodb

$ service mongodb status # 查看mongodb的启动状态

# 默认下载后自动启动,可以用以下命令进行验证
mongo

# 如果是开启状态我们需要关闭mongodb
$ service mongodb stop

# 修改配置
$ sudo vim /etc/mongodb.conf

修改内容如下(其中前两个path分别是默认的数据库存储位置,以及数据库日志文件存放位置)

... ...
#dbpath=/home/ctr_temp/data_base_temp/mongodb/data
dbpath=/var/lib/mongodb

#where to log
#logpath=/home/ctr_temp/data_base_temp/mongodb/log/mongodb.log
logpath=/var/log/mongodb/mongodb.log

logappend=true

# bind_ip = 127.0.0.1
bind_ip = 0.0.0.0 #允许外网访问

port = 27017

# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling
journal=true
... ...

之后重启MongoDB

$ service mongodb start

$ service mongodb status # 查看mongodb的启动状态

为MongoDB配置用户信息

首先进入数据库,首次没有配置安全认证的情况下进入无需使用用户名以及密码验证。

$ mongo
> show dbs # 查看当前已有的数据库
admin   0.000GB
config  0.000GB
local   0.000GB

超级管理员账户

第一步创建超级管理员用户,该用户拥有最高权限,在该用户状态下,可以通过指令关闭数据库。

> use admin # 切换到这个数据库进行用户创建
switched to db admin
> db.createUser({user:"xxx_xxxx",pwd:"xxxxxxxx",roles:[{role:"root", db:"admin"}]})
Successfully added user: {
	"user" : "xxx_xxxx",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

关闭数据库指令如下(仅在超级管理员用户模式下)

> db.shutdownServer()

管理员账户

第二步,拥有管理admin数据库权限的账户,当然也可操作其他数据库。

> use admin
> db.createUser({user:"xxx_xxxx",pwd:"xxxxxxxx",roles: [{role:"userAdminAnyDatabase", db: "admin"}]})
Successfully added user: {
	"user" : "xxx_xxxx",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

数据库管理员账户

第三步,没有管理admin数据库的权限,仅针对你创建的某一数据库的管理员。可以为该数据库创建其用户/创建表单等。

> use test_db ## 注意这里我们需要创建一个新的,你自己的数据库,并且切换到该数据库
# use 语句在没有此数据库的时候会创建一个名为此的数据库(这里是test_db)
> db.createUser({user:"db_owner", pwd:"xxxxxxxx", roles:[{role:"dbOwner", db:"test_db"}]})
Successfully added user: {
	"user" : "db_owner",
	"roles" : [
		{
			"role" : "dbOwner",
			"db" : "test_db"
		}
	]
}

数据库使用者账户

第四步,创建使用者账户,用户具有读写的权限(或者仅读)

> db.createUser({user:"db_user", pwd:"xxxxxxxx", roles:[{role:"readWrite", db:"test_db"}]})
Successfully added user: {
	"user" : "db_user",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "test_db"
		}
	]
}

测试

对以上账户进行测试:

测试账户是否创建成功

首先,在test_db下查看账户,可见在当前的test_db数据库下有两个账户,分别是:账户管理员–db_owner,以及账户获准读写的使用者db_user。

> use test_db
switched to db test_db
> show users
{
	"_id" : "test_db.db_owner",
	"userId" : UUID("fe3161d9-3f4a-496b-8329-f71b2ea055e8"),
	"user" : "db_owner",
	"db" : "test_db",
	"roles" : [
		{
			"role" : "dbOwner",
			"db" : "test_db"
		}
	]
}
{
	"_id" : "test_db.db_user",
	"userId" : UUID("fd4164c7-6133-4c13-8c02-af685b0c75b0"),
	"user" : "db_user",
	"db" : "test_db",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "test_db"
		}
	]
}

另外切换到admin库下,看下系统管理员有没有创建成功:可见超级管理员以及管理员账户位列其中,说明创建成功。

> use admin
switched to db admin
> show users
{
	"_id" : "admin.xxx_xxxx",
	"userId" : UUID("b5c14c35-22ae-4a3c-914f-a3d7c154fd15"),
	"user" : "xxx_xxxx",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
{
	"_id" : "admin.xxx_xxxx",
	"userId" : UUID("e0b0c10f-24bb-4e15-b603-ea8781e5d8d7"),
	"user" : "xxx_xxxx",
	"db" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

以特定用户登入数据库 / 修改数据库

首先应该将安全认证模式打开,之后进入mongodb数据库必须要用户认证才可进入。修改/etc/mongodb.conf文件

$ sudo vim /etc/mongodb.conf 

主要是将auth项的值置为true

... ...
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security.  Off is currently the default
#noauth = true
auth = true

# Verbose logging output.
#verbose = true
... ...

之后关闭数据库重新登入

> exit
> bye
$ service mongodb restart

如果不使用用户名进行登录,我们可以查看以下效果:

$ mongo
> show dbs
2022-08-27T14:50:30.755+0800 E QUERY    [thread1] Error: listDatabases failed:{
	"ok" : 0,
	"errmsg" : "there are no users authenticated",
	"code" : 13,
	"codeName" : "Unauthorized"
} :
... ...

发现也可连入数据库,但当要访问数据库时,比如想要查看有哪些数据库,被告知无法访问,并给出如上的error message。于是以下使用用户名与密码进行登入:

测试“超级管理员”用户

以数据库admin下创建的超级管理员用户身份登入

$ mongo -u xxx_xxxx -p xxxxxxxx --authenticationDatabase admin 
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("fc265ebf-b493-4b31-bb92-9aca50f4bf04") }
MongoDB server version: 3.6.8
Server has startup warnings:
... ...
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
# 注意这里没有显示之前我们创建的test_db数据库,这是因为如果没有向数据库填入任何信息的话,默认不显示
> use admin
switched to db admin
> show users
{
	"_id" : "admin.xxx_xxxx",
	"userId" : UUID("b5c14c35-22ae-4a3c-914f-a3d7c154fd15"),
	"user" : "xxx_xxxx",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
{
	"_id" : "admin.xxx_xxxx",
	"userId" : UUID("e0b0c10f-24bb-4e15-b603-ea8781e5d8d7"),
	"user" : "xxx_xxxx",
	"db" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> use test_db
switched to db test_db
> show users
{
	"_id" : "test_db.db_owner",
	"userId" : UUID("fe3161d9-3f4a-496b-8329-f71b2ea055e8"),
	"user" : "db_owner",
	"db" : "test_db",
	"roles" : [
		{
			"role" : "dbOwner",
			"db" : "test_db"
		}
	]
}
{
	"_id" : "test_db.db_user",
	"userId" : UUID("fd4164c7-6133-4c13-8c02-af685b0c75b0"),
	"user" : "db_user",
	"db" : "test_db",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "test_db"
		}
	]
}

以上说明,超级管理员用户可以访问到所有的数据库以及用户,测试通过。

测试 “数据库管理员用户”

退出后使用在test_db数据库下创建的 “数据库管理员” 身份登入

> exit
bye
$ mongo -u db_owner -p xxxxxxxx --authenticationDatabase test_db
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("bd3c1bb8-da1a-4ed7-8cd6-c3fe228bae52") }
MongoDB server version: 3.6.8
> show dbs
2022-08-27T15:07:47.070+0800 E QUERY    [thread1] Error: listDatabases failed:{
	"ok" : 0,
	"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, lsid: { id: UUID(\"bd3c1bb8-da1a-4ed7-8cd6-c3fe228bae52\") }, $db: \"admin\" }",
	"code" : 13,
	"codeName" : "Unauthorized"
} :
... ... 

说明,数据库管理员无法访问到全局的数据库有哪些。

> use admin 
switched to db admin
> show users
2022-08-27T15:10:10.135+0800 E QUERY    [thread1] Error: not authorized on admin to execute command { usersInfo: 1.0, lsid: { id: UUID("bd3c1bb8-da1a-4ed7-8cd6-c3fe228bae52") }, $db: "admin" } :
... ...

意料之中,本身就不是在admin下创建的账户,当然无法访问admin下的users,下面我们切换到其原本的数据库。

> use test_db
switched to db test_db
> show users
{
	"_id" : "test_db.db_owner",
	"userId" : UUID("fe3161d9-3f4a-496b-8329-f71b2ea055e8"),
	"user" : "db_owner",
	"db" : "test_db",
	"roles" : [
		{
			"role" : "dbOwner",
			"db" : "test_db"
		}
	]
}
{
	"_id" : "test_db.db_user",
	"userId" : UUID("fd4164c7-6133-4c13-8c02-af685b0c75b0"),
	"user" : "db_user",
	"db" : "test_db",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "test_db"
		}
	]
}

访问成功,测试通过。

测试 “数据库使用者” 账户
> exit
bye
$ mongo -u db_user -p xxxxxxxx --authenticationDatabase test_db
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("6b57424e-ba6d-408e-8d12-6f6d71c633c1") }
MongoDB server version: 3.6.8
> use test_db
switched to db test_db
> show users 
2022-08-27T15:22:02.055+0800 E QUERY    [thread1] Error: not authorized on test_db to execute command { usersInfo: 1.0, lsid: { id: UUID("6b57424e-ba6d-408e-8d12-6f6d71c633c1") }, $db: "test_db" } :
... ...

可见使用者账户对于当前数据库,也无法访问到其在该库创建的其他账户,测试结果合理。

用户切换以及用户删除

删除用户指令

使用以下命令删除用户,注意要切换到特定的数据库下,拥有权限才可删除。建议切换数据库后先看以下当前库下的用户有哪些。

> show users
... ...
> db.system.users.remove({user:"user_name"})

简单建表

这里我们在test_db数据库简单建立一张表,为之后的测试做准备。首先使用test_db的使用者账户(之前创立权限最低的那个用户)

> exit
bye
$ mongo -u db_user -p xxxxxxxx --authenticationDatabase test_db
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("6b57424e-ba6d-408e-8d12-6f6d71c633c1") }
MongoDB server version: 3.6.8
> use test_db
switched to db test_db
> db.test_table.insert({"name":"ctr_temp", "age":"111", "gender":"male"})
WriteResult({ "nInserted" : 1 })
> show tables
test_table
> db.test_table.find()
{ "_id" : ObjectId("6309cad7d2b5efef4dbde6dd"), "name" : "ctr_temp", "age" : "111", "gender" : "male" }

以上证明插入成功,现在我们使用超级管理员账户重新登入,看开插入数据之后,是否可以用show dbs看到test_db这个数据库:

$ mongo -u xxx_xxxx -p xxxxxxxx --authenticationDatabase admin
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("a220b785-e908-423b-b3e5-c5aa2ecc2e4e") }
MongoDB server version: 3.6.8
Server has startup warnings: 
... ...
> show dbs
admin    0.000GB
config   0.000GB
local    0.000GB
test_db  0.000GB

可见,在成功建表并插入数据项后,test_db终于变得可见。

参考文章

参考文章一:from~zhihu
参考文章二:from~优快云

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值