MongoDB的简单学习5-Mongo数据库的管理

一、数据导出

命令:mongoexport

参数说明:

-d    指明使用的库

-c    指明使用的表(集合)

-o    指明要导出的文件名

查看帮助文档:>/usr/local/mongodb/bin/mongoexport --help

#导出到/opt/c1.out文件中,导出的库为test,导出的集合为c1
[root@localhost bin]# /usr/local/mongodb/bin/mongoexport -d test -c c1 -o /opt/c1.out
2018-05-27T03:35:53.256-0400	connected to: localhost
2018-05-27T03:35:53.257-0400	exported 10 records
[root@localhost bin]# cat /opt/c1.out 
{"_id":{"$oid":"5b0a37f3805413a0f74dbd27"},"name":"user1","age":1.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd28"},"name":"user2","age":2.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd29"},"name":"user3","age":3.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd2a"},"name":"user4","age":4.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd2b"},"name":"user5","age":5.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd2c"},"name":"user6","age":6.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd2d"},"name":"user7","age":7.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd2e"},"name":"user8","age":8.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd2f"},"name":"user9","age":9.0}
{"_id":{"$oid":"5b0a37f3805413a0f74dbd30"},"name":"user10","age":10.0}

导出csv格式文件

#导出csv格式文件
[root@localhost bin]# /usr/local/mongodb/bin/mongoexport -d test -c c1 --csv -f name,age -o /opt/c1_csv.dat
2018-05-27T03:44:11.360-0400	csv flag is deprecated; please use --type=csv instead
2018-05-27T03:44:11.363-0400	connected to: localhost
2018-05-27T03:44:11.365-0400	exported 10 records
[root@localhost bin]# cat /opt/c1_csv.dat 
name,age
user1,1
user2,2
user3,3
user4,4
user5,5
user6,6
user7,7
user8,8
user9,9
user10,10

参数说明:

--csv :指要导出的为csv格式

-f:指明需要导出那些列

二、数据导入

导入JSON数据:

#Mongo中删除c1集合的数据,现在我们导入!

[root@localhost bin]# /usr/local/mongodb/bin/mongoimport -d test -c c1 /opt/c1.out
2018-05-27T03:39:48.198-0400	connected to: localhost
2018-05-27T03:39:48.202-0400	imported 10 documents

导入csv格式:

#先删掉c1集合内容
>db.c1.remove({});

[root@localhost bin]# /usr/local/mongodb/bin/mongoimport -d test -c c1 --type csv --headerline --file /opt/c1_csv.dat 
2018-05-27T03:52:52.724-0400	connected to: localhost
2018-05-27T03:52:52.725-0400	imported 10 documents

参数说明:

--type : 指明要导入的文件格式

--headerline : 表明不导入第一行,因为第一行是列名

--file : 指明要导入的文件路径

注意:CSV格式良好,主流数据库都支持导出为CSV的格式,所以这种格式非常利于异构数据迁移!

三、数据备份

不指定参数:当然,我们也可以导出一个集合,但是,导出一个集合时会丢失索引!

[root@localhost bin]# /usr/local/mongodb/bin/mongodump -d test 
2018-05-27T04:11:14.451-0400	writing test.c1 to 
2018-05-27T04:11:14.453-0400	done dumping test.c1 (10 documents)
[root@localhost bin]# ll dump/
total 0
drwxr-xr-x. 2 root root 45 May 27 04:11 test
[root@localhost bin]# ll dump/test/
total 8
-rw-r--r--. 1 root root 471 May 27 04:11 c1.bson
-rw-r--r--. 1 root root 179 May 27 04:11 c1.metadata.json
[root@localhost bin]# 
[root@localhost bin]# /usr/local/mongodb/bin/mongodump -d test -o /opt/db
2018-05-27T04:01:11.003-0400	writing test.c1 to 
2018-05-27T04:01:11.005-0400	done dumping test.c1 (10 documents)
[root@localhost bin]# ll /opt/db/
total 0
drwxr-xr-x. 2 root root 45 May 27 04:01 test
[root@localhost bin]# ll /opt/db/test/
total 8
-rw-r--r--. 1 root root 471 May 27 04:01 c1.bson
-rw-r--r--. 1 root root 179 May 27 04:01 c1.metadata.json
[root@localhost bin]# 

导出一个集合:

/usr/local/mongodb/bin/mongodump -d test -c c1 -o /opt/

参数说明:

-o : 指定备份的目录,不指定会默认到/bin下面!

四、数据恢复

#先删除MongoDB的test数据库
>db.dropDatabase();

[root@localhost bin]# /usr/local/mongodb/bin/mongorestore -d test dump/test
2018-05-27T04:13:36.987-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-05-27T04:13:36.987-0400	building a list of collections to restore from dump/test dir
2018-05-27T04:13:36.988-0400	reading metadata for test.c1 from dump/test/c1.metadata.json
2018-05-27T04:13:37.005-0400	restoring test.c1 from dump/test/c1.bson
2018-05-27T04:13:37.008-0400	restoring indexes for collection test.c1 from metadata
2018-05-27T04:13:37.022-0400	finished restoring test.c1 (10 documents)
2018-05-27T04:13:37.022-0400	done
[root@localhost bin]# 
#先删除数据库
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }

#恢复开始
/usr/local/mongodb/bin/mongorestore -d test /opt/db/test

导入一个集合:记得要写上/opt/test ,而不仅仅是/opt/

/usr/local/mongodb/bin/mongorestore -d test  /opt/test

五、用户授权

简介:https://blog.youkuaiyun.com/u010523770/article/details/54599548

每个MongoDB实例中的数据库都有许多用户,如果启用了安全性认证后,只有数据库认证的用户才可以进行读写操作,MongoDB默认的启动是不验证用户名和密码的,启动MongoDB后,可以直接用mongo连接上来,对所有的库具有root权限。所以,启动的时候指定参数,可以阻止客户端的访问和连接。

只需要在启动时指定--auth参数即可。

> use admin;
#创建一个超级用户角色的用户root/root
>db.createUser({user:"root",pwd:"root",roles:["root"]});

#重启mOngod
root@localhost conf]#pkill mongod
root@localhost conf]#/usr/local/mongodb/bin/mongod --auth --config /usr/local/mongodb/conf/mongodb.conf

#注意事项:因为刚才我们设置的是admin数据库的超级用户,所以,只能使用该用户登录admin数据库。
[root@localhost conf]# /usr/local/mongodb/bin/mongo -uroot -proot 127.0.0.1:27017
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017/test
MongoDB server version: 3.6.4
2018-05-27T04:55:18.924-0400 E QUERY    [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20
@(auth):6:1
@(auth):1:2
exception: login failed
[root@localhost conf]# /usr/local/mongodb/bin/mongo -uroot -proot 127.0.0.1:27017/admin


#########从下面的语句我们也可以看到,我们只对admin数据库有权限
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB
test    0.000GB
> use test
switched to db test
> db.auth("root","root");
Error: Authentication failed.
0
> use admin
switched to db admin
> db.auth("root","root");
1
> 

给test数据库增加一个用户:

> use test

>db.createUser({user:"test",pwd:"test",roles:["readWrite","dbAdmin"]});

> show users;

> db.auth("test","test");
1
> show collections;
c1
> db.c1.find();

添加一个超级管理员,可以有很多库的权限的:

> db.createUser({user:"admin",pwd:"admin",roles:["userAdminAnyDatabase","dbAdminAnyDatabase","readWriteAnyDatabase","root"]});

> show collections;
system.users
system.version
> db.system.users.find(); ##注意普通的角色不能查这个!!

转载于:https://my.oschina.net/hanchao/blog/1819495

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值