MongoDB Common Commands

Source: Internet
Author: User
Tags mongodb create database

Three ways to build a http://blog.csdn.net/luonanqin/article/details/8497860 MongoDB cluster


NoSQL = Not the only SQL MongoDB storage method is document-type storage, not key-value form.


MongoDB three ways to build a cluster: Replica set/sharding/master-slaver


Replica set replica set cluster principle: (The simplest way to cluster)

Master node, standby node, quorum node. The primary and standby node stores data, and the quorum node does not store data.

The client connects the primary node to the standby node at the same time and does not connect the quorum node.


By default, the master node provides all the additions and deletions, and the standby node does not provide any services. However, you can provide a query service by setting up the standby node, which can reduce the pressure on the primary node, and when the client makes a data query, the request is automatically forwarded to the standby node. This setting is called the Read Preference Modes, and the Java client provides a simple way to configure it without having to manipulate the database directly.


The quorum node is a special node that does not store data by itself, and the main purpose is to decide which standby node is promoted to the primary node after the master node is hung, so the client does not need to connect to this node. Although there is only one standby node, a quorum node is still required to raise the node level.


Test: One is to the main node to insert data, can be from the backup node to find the previous inserted data (query standby node may encounter a problem, you can check online). The second is to stop the main node, the standby node can become the main node to provide services. The third is to restore the master node, the standby node can also restore its role, rather than continue to act as the main role. Two and San du can see the changes in the cluster in real time through the rs.status () command.



Sharding Shard cluster:

Similar to replica set, a quorum node is required, but sharding also needs to configure nodes and routing nodes. This is the most complex of the three ways to build a cluster.


Master-slaver Master and Standby mode

The official has not recommended this way, the construction method is relatively simple.


Used people should know MongoDB eat memory problem, the solution can only through Ulimit to control memory usage, but if the control is not good, MongoDB will hang off.



The installation of MongoDB

The MongoDB service can be installed by executing script install_mongodb_20160510.sh. For more details, see Redme.


Installation of a MongoDB cluster


Common commands

MongoDB Start-up

/opt/server/mongodb/bin/mongod-f/opt/server/mongodb/mongodb.conf


The stop of MongoDB

Kill-15 PID

Pkill Mongod


MongoDB Login Method

/opt/server/mongodb/bin/mongo 192.168.1.200:27017


View status

Rs.status ()


To delete a node:

Rs.remove ("mongodb13.kk.net:27019") #可以删除节点


To add a node:

Rs.addarb ("mongodb13.kk.net:27019") #可以添加节点, but this adds the node as a quorum


How does the MongoDB replica set Add a node to make it a standby node?

operate on the master node

Use admin

cfg={_id: "WLB", members:[{_id:0,host: ' 192.168.11.215:27017 ', priority:2}, {_id:1,host: ' 192.168.11.187:27017 ', Priority:1},{_id:2,host: ' 192.168.11.25:27017 ', Arbiteronly:true}]};

Rs.reconfig (CFG); #使配置生效

Rs.status ()



View all databases

Show DBS


MongoDB CREATE Database

Use Znx

Db

Show DBS #会发现创建的数据库不在列表中 to display the data that needs to be inserted


Standby node If you want to view the database, you need to execute the following command, otherwise 13435 error

Rs.slaveok (True)



Inserting data

Db.znx.insert ({"Name": "Dengyong"})

Show DBS #会显示出znx数据库, the standby node will also be displayed


View all data Sheets

Show collections


View all table records

Db.znx.find ()



Node to view the data, execute the following command first, or it will error

Rs.slaveok (True)


MongoDB Delete Database

Use Znx

Db.dropdatabase () #执行删除命令

Show DBS #znx数据库已删除



Reference Link: http://blog.csdn.net/chen88358323/article/details/50206651

MongoDB Create user

Use admin

Db.createuser ({User: "Wjs", pwd: "Wjs", Roles:[{role: "Useradminanydatabase", DB: "Admin"}]}) #创建用户



Built-in roles:

1. Database user role: Read, readWrite;

2. Database Management role: DbAdmin, Dbowner, useradmin;

3. Cluster Management role: Clusteradmin, Clustermanager, Clustermonitor, Hostmanager;

4. Backup Restore role: backups, restore;

5. All database roles: Readanydatabase, Readwriteanydatabase, Useradminanydatabase, dbadminanydatabase

6. Super User role: Root

There are also several roles that indirectly or directly provide access to the system's Superuser (Dbowner, Useradmin, Useradminanydatabase)

7. Internal role: __system

Specific roles:

READ: Allows the user to read the specified database

ReadWrite: Allows the user to read and write to the specified database

DbAdmin: Allows the user to perform administrative functions in the specified database, such as index creation, deletion, viewing statistics, or accessing System.profile

Useradmin: Allows the user to write to the System.users collection to create, delete, and manage users in the specified database

Clusteradmin: Available only in the admin database, giving the user administrative privileges on all shards and replica set related functions.

Readanydatabase: Only available in the Admin database, giving users read access to all databases

Readwriteanydatabase: Only available in the Admin database, giving users read and write access to all databases

Useradminanydatabase: Only available in the Admin database, giving the user useradmin permissions for all databases

Dbadminanydatabase: Only available in the Admin database, giving the user dbadmin permissions for all databases.

Root: Available only in the admin database. Super account, Super privilege


Db.system.users.find () #查看用户

Show Users



Create a database Znx

Use Znx

Create a regular user wxc

Db.createuser ({User: "Wxc", pwd: "Wxc", Roles:[{role: "ReadWrite", DB: "Znx"}]})


This article is from the "Youth Deng Yong" blog, please be sure to keep this source http://dengyong.blog.51cto.com/8409869/1772863

MongoDB Common Commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.