MongoDB Complex command line

Source: Internet
Author: User
Tags auth findone mongodb server server memory mongodump mongorestore

mongodb{


First, start {

# Do not start authentication

./mongod--port 27017--fork--logpath=/opt/mongodb/mongodb.log--logappend--dbpath=/opt/mongodb/data/

# Start Authentication

./mongod--port 27017--fork--logpath=/opt/mongodb/mongodb.log--logappend--dbpath=/opt/mongodb/data/--auth


# configuration file Mode startup

Cat/opt/mongodb/mongodb.conf

PORT=27017 # port number

Fork=true # runs as a daemon, creating a server process

Auth=true # Enable user authentication

Logappend=true # Log in Append mode

Logpath=/opt/mongodb/mongodb.log # Log output file path

dbpath=/opt/mongodb/data/# Database path

Shardsvr=true # setting whether to Shard

MAXCONNS=600 # Maximum number of connections to the database

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

# Other parameters

BIND_IP # bind IP use MONGO login needs to specify corresponding IP

Journal # turn on log function, reduce the recovery time of single machine failure, replace Dur parameter

Syncdelay # Time of the system to flush disks synchronously, default 60 seconds

DIRECTORYPERDB # Separate directory for each DB, recommended settings. Similar to MySQL standalone table space

Repairpath # Temp directory when executing repair. If the journal is not turned on and an abnormal restart occurs, the repair action must be performed

# MongoDB does not have parameters to set the memory size. Using the OS mmap mechanism to cache data files is very efficient when the amount of data is not more than the memory. The amount of data that exceeds the system available memory can affect write performance


}


Second, close {


# method One: Login MongoDB

./mongo

Use admin

Db.shutdownserver ()


# method: Kill to transmit the signal can both

Kill-2 PID

Kill-15 PID


}


Third, open authentication and user management {


./mongo # Log In first

Use admin # Switch to admin library

Db.adduser ("root", "123456") # Create User

Db.adduser (' Zhansan ', ' Pass ', true) # if the user's readonly is true then the user can only read the data and add a readonly user Zhansan

./mongo 127.0.0.1:27017/mydb-uroot-p123456 # Login again, only for the user's library

#虽然是超级管理员, but admin can't log in to other database directly or error

#Fri Nov 15:03:21.886 error:18 {code:18, ok:0.0, errmsg: "Auth Fails"} at src/mongo/shell/db.js:228

Show Collections # View link status sign in again use the following command to display an error without authorization

Db.system.users.find (); # View Create user Information

Db.system.users.remove ({User: "Zhansan"}) # Delete user


#恢复密码只需要重启mongodb do not add--auth parameters


}


Iv. Login {


192.168.1.5:28017 # HTTP Login to view status

./mongo # Open Test library after default login

./mongo 192.168.1.5:27017/databasename # Direct connection to a library does not exist then creating a boot authentication requires specifying the corresponding library to log in


}


V. View status {


#登录后执行命令查看状态

Db.runcommand ({"Serverstatus": 1})

GlobalLock # indicates how much time (in microseconds) the global write lock consumes the server

MEM # contains the amount of data mapped by the server memory, the virtual memory of the server process, and the occupancy of the Resident memory (MB)

Indexcounters # Indicates the number of times the B-tree was retrieved on disk (misses) and memory retrieved (hits). If these two ratios start to rise, consider adding memory.

Backgroudflushing # indicates how many times fsync and how much time was spent in the background

Opcounters # contains the number of each major hit

Asserts # counts the number of assertions


#状态信息从服务器启动开始计算, if too large will be reset, send reset, all counts will be reset, asserts in the Roolovers value increased


#mongodb自带的命令

./mongostat

Insert #每秒插入量

Query #每秒查询量

Update #每秒更新量

Delete #每秒删除量

Locked #锁定量

QR|QW #客户端查询排队长度 (Read | write)

Ar|aw #活跃客户端量 (Read | write)

Conn #连接数

Time #当前时间


}


Vi. Common Commands {


Db.listcommands () # All commands supported by the current MongoDB (also can query all commands by running command Db.runcommand ({"Listcommands": ' 1})


Db.runcommand ({"Buildinfo": 1}) # Returns the version number of the MongoDB server and information about the server OS.

Db.runcommand ({"Collstats": Collection name}) # returns statistics for the collection, including data size, allocated storage space size, index size, and so on.

Db.runcommand ({"DISTINCT": Collection name, "Key": Key, "query": Querying document}) # returns all the different values of the specified key for all documents that match the criteria specified in the query document.

Db.runcommand ({"Dropdatabase": 1}) # Clears the information for the current database, including deleting all collections and indexes.

Db.runcommand ({"IsMaster": 1}) # Check whether this server is a primary server or a slave server.

Db.runcommand ({"Ping": 1}) # Check that the server link is healthy. The command returns immediately even if the server is locked.

Db.runcommand ({"Repairedatabase": 1}) # Repairs and compresses the current database, which can be time-consuming if the database is particularly large.

Db.runcommand ({"Serverstatus": 1}) # View administrative statistics for this server.

# Some commands must be run under the Admin database with the following two commands:

Db.runcommand ({"Renamecollection": Collection name, "to": Collection name}) # Rename the collection, note that the two collection names are the complete collection namespace, such as Foo.bar, which represents the collection bar under Database foo.

Db.runcommand ({"ListDatabases": 1}) # list all databases on the server


}


Vii. Process Control {


Db.currentop () # View Active processes

Db. $cmd. Sys.inprog.findOne () # View the active process as above

OPID # operation Process number

OP # action type (query \ Update)

NS # namespace, which object is manipulated

Query # If the action type is a query, the specific query content will be displayed here

LockType # The type of lock, indicating whether it is a read or write lock


Db.killop (opid value) # End Process

Db. $cmd. Sys.killop.findOne ({op:opid value}) # End Process


}


Eight, backup restore {


./mongoexport-d test-c t1-o t1.dat # Export JSON format

-C # indicates the Export collection

-D # using libraries

./mongoexport-d test-c t1-csv-f num-o t1.dat # export CSV format

-csv # indicates export CSV format

-F # indicates that you need to export those examples


Db.t1.drop () # Delete data after login

./mongoimport-d test-c t1-file t1.dat # mongoimport Restore JSON format

./mongoimport-d test-c T1-type csv--headerline-file t1.dat # mongoimport restore CSV format data

--headerline # indicates that the first row is not imported because the first row is a column name


./mongodump-d Test-o/bak/mongodump # mongodump Data backup

./mongorestore-d Test--drop/bak/mongodump/* # Mongorestore Recovery

--drop #恢复前先删除

Db.t1.find () #查看


# Mongodump Although it can be backed up, the city has the ability to get a real-time view of the data, using the Fsync command to replicate the data directory at run time without damaging the data

# Fsync forces the server to write data from all buffers to disk. Mate lock also prevents further writes to the database until the lock is released

# Backup is backed up from the library, does not delay read and write, and can guarantee real-time snapshot backup

Db.runcommand ({"Fsync": 1, "Lock": 1}) # Perform force update and write lock

Db. $cmd. Sys.unlock.findOne () # Unlocking

Db.currentop () # To see if unlocking is OK


}


Nine, repair {


# Some data corruption is lost when a power outage or other malfunction causes an abnormal shutdown

./mongod--repair # Repair operation: Add--repair when starting up

# Repair Process: Export all documents and import them immediately, ignoring invalid documents. Rebuild the index when finished. A long time, will discard the damaged document

# Repair data can also play a role in compressing the database

Db.repairdatabase () # running MongoDB can use RepairDatabase to repair the database currently in use

{"RepairDatabase": 1} # via Driver


}


This article is from "Hao son of ▁ Yun's finger licking" blog, please be sure to keep this source http://chenhao6.blog.51cto.com/6228054/1723118

MongoDB Complex command line

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.