搜集mongodb命令
2018-08-30 本文已影响0人
w_tiger
shell交互式下输入mongo就可以直接无密码登录到数据库。
show dbs; #查看全部数据库
show collections; #显示当前数据库中的集合(类似关系数据库中的表)
show users; #查看当前数据库的用户信息
use <db name>; #切换数据库跟mysql一样
db;或者db.getName(); #查看当前所在数据库
db.help(); #显示数据库操作命令,里面有很多的命令
db.foo.help(); #显示集合操作命令,同样有很多的命令,foo指的是当前数据库下,一个叫foo的集合,并非真正意义上的命令
db.foo.find(); #对于当前数据库中的foo集合进行数据查找(由于没有条件,会列出所有数据)
db.foo.find( { a : 1 } ); #对于当前数据库中的foo集合进行查找,条件是数据中有一个属性叫a,且a的值为1
> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost) - deprecated
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - deprecated
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.dropUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
>
创建一个test数据库例子:
> use test; #创建数据库
switched to db test
> db;
test
> show dbs; #检查数据库
admin 0.000GB
local 0.000GB
> db.test.insert({"_id":"520","name":"xiaoming"}) #创建表
WriteResult({ "nInserted" : 1 })
> db.createUser({user:"xiaoming",pwd:"123456",roles:[{role:"userAdmin",db:"test"}]}) #创建用户
Successfully added user: {
"user" : "xiaoming",
"roles" : [
{
"role" : "userAdmin",
"db" : "test"
}
]
}
db.dropUser("userName"); #删除用户
show users; #显示当前所有用户
db.dropDatabase(); #删除当前使用数据库
> show dbs;
admin 0.000GB
local 0.000GB
test 0.000GB
test_1 0.000GB
> db;
test_1
> db.dropDatabase();
{ "dropped" : "test_1", "ok" : 1 }
> show dbs;
admin 0.000GB
local 0.000GB
test 0.000GB
db.stats(); #显示当前db状态
> db.stats();
{
"db" : "test_1",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"ok" : 1
}
db.version(); #当前db版本
> db.version()
4.0.1
db.getMongo(); #查看当前db的链接机器地址
> db.getMongo()
connection to 172.16.40.205:27017
开启远程访问
编辑配置文件:vi /etc/mongod.conf #具体路径看情况
bindIp: 172.16.40.205 #数据库所在服务器IP地址
保存重启数据库!
本地登录:mongo 172.16.40.205/admin -uadmin -p123456
远程登录:
1. 下载mongodb压缩包
mongodb-linux-x86_64-3.4.10.tgz
2. 解压
> tar zxvf mongodb-linux-x86_64-3.4.10.tgz
3. 进入bin目录
> cd mongodb-linux-x86_64-3.4.10/bin
4. 连接远程数据库
> ./mongo 172.16.40.205:27017/admin -u user -p password