MongoDB常用操作

①MongoDB配置文件(mongo.conf):

 1 #mongodb配置文件
 2 port=27017
 3 dbpath=~/file/Test/mongodb/data
 4 logpath=~/file/Test/mongodb/log/mongodb.log
 5 logappend=true
 6 fork=true
 7 journal=
 8 jsonp=
 9 directoryperdb=true
10 maxConns=500

②MongoDB启动脚本(mongo-start):

1 #!/bin/bash
2 mongod --config ~/file/Test/mongodb/bin/mongo.conf

③MongoDB停止启动脚本(mongo-shutdown):

1 #!/bin/bash
2 mongod --shutdown --dbpath ~/file/Test/mongodb/data/

④连入数据库:

1 > mongo2 MongoDB shell version: 2.4.6
3 connecting to: test
4 Server has startup warnings: 
5 Tue Oct 22 17:04:23.414 [initandlisten] 
6 Tue Oct 22 17:04:23.414 [initandlisten] ** WARNING: Readahead for /home/liangzhitao/file/Test/mongodb/data is set to 512KB
7 Tue Oct 22 17:04:23.414 [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
8 Tue Oct 22 17:04:23.414 [initandlisten] **          http://dochub.mongodb.org/core/readahead

⑤添加用户名密码:

1 > use admin
2 switched to db admin
3 > db.addUser('admin','manager')
4 {
5     "user" : "admin",
6     "readOnly" : false,
7     "pwd" : "ef228edd753456ba9ef70d8971d3093d",
8     "_id" : ObjectId("52664b72cebcee8243ad99bf")
9 }

⑥使用创建的用户登录MongoDB:

 1 > use admin
 2 switched to db admin
 3 > db.auth('admin','manager')
 4 1
 5 > show dbs
 6 admin    0.203125GB
 7 local    0.078125GB
 8 > use admin
 9 switched to db admin
10 > show collections
11 system.indexes
12 system.users
13 > db.system.users.find()
14 { "_id" : ObjectId("52664b72cebcee8243ad99bf"), "user" : "admin", "readOnly" : false, "pwd" : "ef228edd753456ba9ef70d8971d3093d" }

⑦查看各个集合的状态:

 1 > db.printCollectionStats()
 2 system.indexes
 3 {
 4     "ns" : "admin.system.indexes",
 5     "count" : 2,
 6     "size" : 208,
 7     "avgObjSize" : 104,
 8     "storageSize" : 4096,
 9     "numExtents" : 1,
10     "nindexes" : 0,
11     "lastExtentSize" : 4096,
12     "paddingFactor" : 1,
13     "systemFlags" : 0,
14     "userFlags" : 0,
15     "totalIndexSize" : 0,
16     "indexSizes" : {
17         
18     },
19     "ok" : 1
20 }
21 ---
22 system.users
23 {
24     "ns" : "admin.system.users",
25     "count" : 1,
26     "size" : 96,
27     "avgObjSize" : 96,
28     "storageSize" : 8192,
29     "numExtents" : 1,
30     "nindexes" : 2,
31     "lastExtentSize" : 8192,
32     "paddingFactor" : 1,
33     "systemFlags" : 1,
34     "userFlags" : 0,
35     "totalIndexSize" : 16352,
36     "indexSizes" : {
37         "_id_" : 8176,
38         "user_1_userSource_1" : 8176
39     },
40     "ok" : 1
41 }
42 ---

 ⑧拷贝删除数据库:

 1 > db.copyDatabase('local','local_backup')
 2 { "ok" : 1 }
 3 > use local_backup
 4 switched to db local_backup
 5 > show collections
 6 startup_log
 7 system.indexes
 8 > db.startup_log.drop()
 9 true
10 > db.dropDatabase()
11 { "dropped" : "local_backup", "ok" : 1 }

 ⑨删除用户:

 1 > db.system.users.find()
 2 { "_id" : ObjectId("52664b72cebcee8243ad99bf"), "user" : "admin", "readOnly" : false, "pwd" : "ef228edd753456ba9ef70d8971d3093d" }
 3 > db.addUser('Test','Test')
 4 {
 5     "user" : "Test",
 6     "readOnly" : false,
 7     "pwd" : "173f8b0eab7de019014a7de2fbd72088",
 8     "_id" : ObjectId("5267682ea47e33b74004989a")
 9 }
10 > db.system.users.find()
11 { "_id" : ObjectId("52664b72cebcee8243ad99bf"), "user" : "admin", "readOnly" : false, "pwd" : "ef228edd753456ba9ef70d8971d3093d" }
12 { "_id" : ObjectId("5267682ea47e33b74004989a"), "user" : "Test", "readOnly" : false, "pwd" : "173f8b0eab7de019014a7de2fbd72088" }
13 > db.removeUser('Test')
14 > db.system.users.find()
15 { "_id" : ObjectId("52664b72cebcee8243ad99bf"), "user" : "admin", "readOnly" : false, "pwd" : "ef228edd753456ba9ef70d8971d3093d" }

你可能感兴趣的:(mongodb)