mongodb基本命令
admin
2023-05-20 16:22:31
0

Mongodb维护基本命令

user管理命令适用在2.*版本3.*有些改动 

1.查看详细分片信息

mongos>sh.status() 

---Sharding Status ---

  sharding version: {

       "_id" : 1,

       "version" : 4,

       "minCompatibleVersion" : 4,

       "currentVersion" : 5,

       "clusterId" :ObjectId("569c8e920d9b1cb4bbf4169a")

}

  shards:

       { "_id" : "shard1", "host" :"shard1/192.168.0.167:20200,192.168.0.25:20100" }

       { "_id" : "shard2", "host" :"shard2/192.168.0.167:20020,192.168.0.167:20030" }

       { "_id" : "shard3", "host" :"shard3/192.168.0.167:30003,192.168.0.25:30001" }

  databases:

       { "_id" : "admin", "partitioned" : false, "primary" : "config" }

       { "_id" : "test", "partitioned" : false, "primary" : "shard3" }

 

2. 仅显示分片

>useconfig;

>db.shards.find()

{"_id" : "shard1", "host" :"shard1/10.10.10.39:20100,10.10.10.49:20200" }

{"_id" : "shard2", "host" :"shard2/10.10.10.39:20010,10.10.10.49:20020" }

{"_id" : "shard3", "host" :"shard3/10.10.10.39:30001,10.10.10.49:30002" }

 

3.查看集群相关信息

mongos>db.serverStatus()

 

4.数据库用户及数据库信息

mongos> show dbs;

admin  0.016GB

config 0.016GB

testdb 0.156GB

 

mongos> use ttdb           \\创建数据库

switched to db ttdb

 

mongos> db.addUser('tt','123456') \\为数据库添加用户名和密码

WARNING: The 'addUser' shell helper isDEPRECATED. Please use 'createUser' instead

Successfully added user: { "user": "tt", "roles" : [ "dbOwner" ] }

 

# /mnt1/data/mongodbtest/mongodb/bin/mongo 192.168.0.25:20001/ttdb

MongoDB shell version: 2.6.5

connecting to: 192.168.0.25:20001/ttdb

 

mongos> show collections      \\未使用用户密码登录数据库,报没有权限错误

2016-01-18T17:42:27.981+0800 error: {

         "$err": "not authorized for query on ttdb.system.namespaces",

         "code": 13

} at src/mongo/shell/query.js:131

mongos> db              \\查看当前所使用的数据库

ttdb

mongos> db.auth('tt','123456')

1                        \1 代表验证成功

mongos>show collections

mongos> use ttdb

switched to db ttdb

 

mongos> show collections

mongos> db.dropDatabase();      \\删除数据库

{ "dropped" : "ttdb","ok" : 1 }

mongos> use admin

switched to db admin

mongos> show dbs;

admin  0.016GB

config 0.016GB

 

mongos> use admin

mongos> db.system.users.find()     \\查看当前数据库的用户

 

mongos> use ttdb

switched to db ttdb

mongos> db.dropUser("sa")       \\删除数据库用户

true

 

>use ttdb

>db.table1.stats();         \\查看表的状态

 

db.addUser("tx","tx",ture);     \\添加只读的数据库用户

2016-01-26T16:16:26.153+0800ReferenceError: ture is not defined

mongos> db.addUser("tx","tx",true);

WARNING: The 'addUser' shell helper isDEPRECATED. Please use 'createUser' instead

Successfully added user: { "user": "tx", "roles" : [ "read" ] }

mongos>db.createCollection("Account")

{

         "ok": 0,

         "errmsg": "not authorized on ttdb to execute command { create:\"Account\" }",

         "code": 13

}                  \\创建集合会报错

 

5. balancer     (复制相关)

mongos> use config

switched to db config

mongos> sh.getBalancerState();    \\查看进程状态

true

mongos> sh.stopBalancer();

Waiting for active hosts...

Waiting for the balancer lock...

Waiting again for active hosts afterbalancer is off...

 

mongos> sh.getBalancerState();

false

mongos> sh.startBalancer();

mongos> sh.getBalancerState();

true

 


相关内容

热门资讯

“控制”格陵兰岛,特朗普罕见列... 据美国《新闻周刊》和英国《独立报》8月2日报道,美国总统特朗普日前在接受采访时声称,格陵兰岛将在他本...
脑机接口,新突破,深度布局的1... (来源:逍遥财情) 8月1日,两项关于脑机接口行业的新国家标准开始实施,这两项标准相当于为整个行业制...
特朗普:与伊朗的谈判已开始,计... 当地时间8月3日,美国总统特朗普在社交平台“真实社交”(Truth Social)发文称,与伊朗的谈...
唐山电缆测绘现场案例 唐山电缆测绘现场案例 探测背景: 本次服务对象为唐山某测绘公司,因后期甲方需开展开挖作业,需提前明确...
聚焦能源与前沿科技!山西启动重... 为持续完善全省科技创新平台体系,夯实基础研究根基、打通科技成果产业化链条,加快培育新质生产力,赋能全...
四川:到2026年底推动阿里云... 《四川省推进算力设施优化布局提质建设和算电融合发展实施方案(2026—2028年)》近日印发。其中提...
瑙鲁正式更改国名 △瑙鲁(资料图)瑙鲁共和国政府日前在社交媒体上发布公告说,该国国名已由“Nauru”正式更改为“Na...
中国科学家首获威廉·诺德伯格奖... 当地时间8月3日,在意大利佛罗伦萨举办的第46届国际空间研究委员会(COSPAR)科学大会上,中国气...
和县科技馆开展“科普邂逅盛夏 ... 为丰富青少年暑期精神文化生活,普及推广自然科学知识,7 月 29 日,和县科技馆组织开展夏日主题科普...
外部链接跳转微信小程序失败的8... 在使用外链这样的三方工具将小程序转成跳转链接推广使用时,在跳转全流程中每一个环节都有可能导致跳转失败...