您的位置:首页 > 博客中心 > 数据库 >

Mongodb 的简单使用

时间:2022-03-13 23:36

一、环境及启动

Mongodb直接下载回来解压就能用。如,我解压到app/mongo目录下。并在appmongo目录下那建data/db目录用来存在mongodb数据库数据的一个文件夹。


root用户启动mongodb,这里如果不使用root用户,第二次再次启动的时候启动不了。具体原因没有找着。

cd app/mongo/
bin/mongod --dbpath data/db


进入mongo的shell:

./bin/mongo

关闭:
一种稳妥的停止MongoDB服务的方式就是使用shutdown命令,即{“shutdown” : 1},这是管理命令,要在admin数据库下使用。shell提供了辅助函数,如下:


use admin

db.shutdownServer();

若MongoDB服务器是最为前台进程运行在终端,那么可以直接关闭命令行窗口即可。


二、安全和认证

每个MongoDB实例中的数据库可以有许多用户,如果开启了安全性检查,则只有数据库认证用户才能执行读或者写操作。在数据库中添加用户,如下所示:

use test

db.addUser(“test_user”, “1234”)


addUser()函数中的第三个参数为可选项true或者false,表示该用户是否为只读用户。


注意:addUser不仅能添加用户,还能修改用户口令或者只读状态。


要开启安全性检查,重启服务器,同时加--auth命令行选项。然后通过shell重新连接数据库,操作如下:

use test
db.auth(“test_user”, “1234”)


之后用户就可以在自己的权限范围内进行操作了。
数据库的用户账户以文档的形式存储在system.users集合里面。文档的结构如下:
 {“user” : username, “readOnly” : true, “pwd” : password hash}


其中password hash是根据用户名和密码生成的散列。


用户认证时,服务器将认证和连接绑定来跟踪认证。所以如果驱动程序或是工具使用了连接池或是因故障切换到另一个节点,所有认证用户必须对每个新连接重新认证。有的驱动程序能够将这步透明化,但要是没有,就得手动完成了。


除了认证还有许多选项值得考虑来锁定MongoDB实例。建议将MongoDB服务器布置在防火墙后或者布置在只有应用服务器能访问的网络中。但要是MongoDB必须能被外面访问到的话,建议使用—bindip选项,可以指定mongod绑定到的本地IP地址。

例如,只能从本机应用服务器访问:

mongod –bindip localhost

三、简单的使用

1、使用crm数据库
use crm



2、为数据库中的user集合(类似sql中的表),增加一条数据
db.user.insert({ "username" : "admin","password" : "1234",  "email" : "testuser1@testdomain.com" }) 


3、查询user集合
db.user.find().pretty()


4、为user集合增加多条数据
stuff = [{ "username" : "test1","password" : "1234", "email" : "testuser2@testdomain.com" }, { "username" : "test2","password" : "1234", "email" : "testuser3@testdomain.com" }]

 db.user.insert(stuff);  


上面操作的结果如下:
benben@benben:~/app/mongo/bin$ ./mongo
MongoDB shell version: 2.6.4
connecting to: test
> use crm
switched to db crm
> db.user.insert({ "username" : "admin","password" : "1234",  "email" : "testuser1@testdomain.com" }) 
WriteResult({ "nInserted" : 1 })
> db.user.find().pretty()  
{
	"_id" : ObjectId("53fc73bf784eb7aa025aeb31"),
	"username" : "admon",
	"password" : "1234",
	"email" : "testuser1@testdomain.com"
}
> stuff = [{ "username" : "test1","password" : "1234", "email" : "testuser2@testdomain.com" }, { "username" : "test2","password" : "1234", "email" : "testuser3@testdomain.com" }]
[
	{
		"username" : "test1",
		"password" : "1234",
		"email" : "testuser2@testdomain.com"
	},
	{
		"username" : "test2",
		"password" : "1234",
		"email" : "testuser3@testdomain.com"
	}
]
> db.user.insert(stuff);  
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> db.user.find().pretty()
{
	"_id" : ObjectId("53fc73bf784eb7aa025aeb31"),
	"username" : "admon",
	"password" : "1234",
	"email" : "testuser1@testdomain.com"
}
{
	"_id" : ObjectId("53fc741f784eb7aa025aeb32"),
	"username" : "test1",
	"password" : "1234",
	"email" : "testuser2@testdomain.com"
}
{
	"_id" : ObjectId("53fc741f784eb7aa025aeb33"),
	"username" : "test2",
	"password" : "1234",
	"email" : "testuser3@testdomain.com"
}
> 


热门排行

今日推荐

热门手游