http://space.itpub.net/?uid-20498361-action-viewspace-itemid-7194911, Super User related
#进入数据库admin
Use admin
#增加或修改用户密码
Db.adduser (' name ', ' pwd ')
#查看用户列表
Db.system.users.find ()
#用户认证
Db.auth (' name ', ' pwd ')
#删除用户
Db.removeuser (' name ')
#查看所有用户
Show Users
#查看所有数据库
Show DBS
#查看所有的collection
Show collections
#查看各collection的状态
Db.printcollectionstats ()
#查看主从复制状态
Db.printreplicationinfo ()
#修复数据库
Db.repairdatabase ()
#设置记录profiling, 0=off 1=slow 2=all
Db.setprofilinglevel (1)
#查看profiling
Show profile
#拷贝数据库
Db.copydatabase (' mail_addr ', ' mail_addr_tmp ')
#删除collection
Db.mail_addr.drop ()
#删除当前的数据库
Db.dropdatabase ()
2, delete and change
#存储嵌套的对象
Db.foo.save ({' name ': ' Ysz ', ' address ': {' city ': ' Beijing ', ' Post ': 100096}, ' phone ': [138,139]})
#存储数组对象
Db.user_addr.save ({' Uid ': ' [email protected] ', ' Al ': [' [email protected] ', ' [email protected] ']})
#根据query条件修改, insert if not present, allow multiple records to be modified
Db.foo.update ({' yy ': 5},{' $set ': {' xx ': 2}},upsert=true,multi=true)
Records of #删除yy =5
Db.foo.remove ({' yy ': 5})
#删除所有的记录
Db.foo.remove ()
5, Management
#查看collection数据的大小
Db.deliver_status.dataSize ()
#查看colleciont状态
Db.deliver_status.stats ()
#查询所有索引的大小
Db.deliver_status.totalIndexSize ()
#启动
./mongod #从命令行, Mongod--help can get help with this command
#配置 (config file)
./mongod--config ~/.mongodb.conf #使用配置文件
#停止数据库
(1) If you use it directly at the front desk: Ctrl + C
(2) If you know the process number, directly with Kill-2 PID #注意千万不要用kill-9, may result in inconsistent database data
(3) Use command: Db.shutdownserver ()
# Monitoring
. Working with Web pages
MongoDB launches an HTTP service at startup and can see the status directly with http://ip:28017
. Open View by command
Db.runcommand ({"Serverstatus": 1})
. through external processes
Mongostat
. Via third-party plugins
such as Cacti,nagios and other systems
#查看数据库状态
(1) Db.runcommand ({"Serverstatus": 1})
(2) $MONGO _home/bin/mongostat
6, database backup
4 ways to back up a database
(1) After you close the Mongod service, copy the data file specified by the--dbpath parameter. The advantage speed is fast, the disadvantage needs to stop MONGO service.
(2) Export data using Mongodump and import data with Mongorestore. The advantage does not need to stop the MONGO service, the disadvantage is that the data inserted by the user during Mongodump operation may not be backed up.
(3) Fsync and lock lock the database so that users can only use the Read feature, and then use method B to export and import data. The advantage does not need to stop the MONGO service, the disadvantage is that users cannot perform insert operations during database lock.
(4) Use Slavedb and use method C to lock the Slavedb, and then use method B to export and import the data. The advantage does not need to stop the MONGO service and does not affect user insert operations (this method is recommended).
7, database Repair
There are 3 ways to repair a data file when the database file is compromised
(1) Mongo_home/bin/mongod--repair
(2) Use test
Db.repairdatabase ()
(3) Db.runcommand ({"RepairDatabase": 1});
8. View Document Commands
Db.<coll>.find ()
Db.<coll>.findone ()
Specific ways to view Help:db.< collection name >.help ()
9. How to view Help for a command
. Print a list of all commands
Db.listcommands ()
. Display a description of a command
Db.commandhelp ("IsMaster")//view IsMaster instructions for this command
MongoDB Basic Command