MongoDB3.2---基本操作汇总

来源:互联网 发布:网络聊天软件 编辑:程序博客网 时间:2024/06/01 10:39
连接方式:
单点连接数据库:
root#mongo 192.168.1.100:30000/testDB -u tuser -p  --authenticationDatabase admin
注:testDB是默认登录的数据库,也可以是有权限访问的其他数据库
admin是账号tuser创建时所在的数据库,也就是认证数据库,不可改变,
如果--authenticationDatabase缺省,则默认使用testDB这个位置上的数据库作为认证库

 复制集连接:
mongodb://username:pwd@ip_addr:27017,ip_secondary_addr:27017
URI连接方式,以这种方式连接集群,当primary宕机后,可自动切换failover到新的Primary节点
 
show 命令:
db          查看当前连接的库名称
show dbs    列出所有DB
use dbname  切换当前DB,无论此库是否存在都依然会切换
show tables  或 show collections  列出当前DB的所有表/集合
show users  列出当前DB的所有用户
show profile 列出当前DB的所有慢查询
show logs    列出运行日志

常用信息查询命令:
* db.serverStatus()                                查看mongod运行状态信息
* db.stats()                                      查看db元数据
* db.collection.stats()                            查看集合元数据
* db.collection.insert() / update / remove / find  对集合增删改查
* db.collection.createIndex()                      创建索引
* db.collection.dropIndex()                        删除索引
* db.dropDatabase()                                删除DB
* db.printReplicationInfo()
* db.printSlaveReplicationInfo()                  查看复制集同步信息
* rs.status()                                      查看复制集当前状态
* rs.conf()                                        查看复制集配置
* rs.initiate()                                    初始化复制集
* rs.reconfig()                                    重新配置复制集
* rs.add() / rs.remove()                          增加/删除复制集节点 
* sh.enableSharding()                              对DB启用分片
* sh.shardCollection()                            对集合进行分片
* sh.status()                                      查看sharding状态信息
* ...

shell执行mongo命令:
$ mongo --host localhost:27017 --eval "printjson(db.serverStatus().opcounters)"        
MongoDB shell version: 3.2.10
connecting to: localhost:27017/test
{
        "insert" : 81285690,
        "query" : 20,
        "update" : 0,
        "delete" : 0,
        "getmore" : 0,
        "command" : 45467
}


Man手册:
* help
* db.help()
* rs.help()
* sh.help()
* db.collection.find().help()
* help misc

0 0
原创粉丝点击