Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb basic command

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Mongodb maintenance basic command

User management commands apply to 2.* version 3.* with some changes

1. View detailed sharding information

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:20200192.168.0.25:20100"}

{"_ id": "shard2", "host": "shard2/192.168.0.167:20020192.168.0.167:20030"}

{"_ id": "shard3", "host": "shard3/192.168.0.167:30003192.168.0.25:30001"}

Databases:

{"_ id": "admin", "partitioned": false, "primary": "config"}

{"_ id": "test", "partitioned": false, "primary": "shard3"}

two。 Show only fragments

> 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. View cluster-related information

Mongos > db.serverStatus ()

4. Database users and database information

Mongos > show dbs

Admin 0.016GB

Config 0.016GB

Testdb 0.156GB

Mongos > use ttdb\\ create the database

Switched to db ttdb

Mongos > db.addUser ('tt','123456')\\ add a user name and password to the database

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\\ failed to log in to the database with a user password and reported no permission error

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\\ View the database currently in use

Ttdb

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

1\ 1 means the verification is successful

Mongos > show collections

Mongos > use ttdb

Switched to db ttdb

Mongos > show collections

Mongos > db.dropDatabase ();\\ delete the database

{"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 ()\\ View the user of the current database

Mongos > use ttdb

Switched to db ttdb

Mongos > db.dropUser ("sa")\\ Delete a database user

True

> use ttdb

> db.table1.stats ();\\ View the status of the table

Db.addUser ("tx", "tx", ture);\\ add read only database users

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

}\\ create a collection will report an error

5. Balancer (replication related)

Mongos > use config

Switched to db config

Mongos > sh.getBalancerState ();\\ View process status

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report