mongosniff
Mongosniff provides low-level operation tracking and sniffing graph for real-time database activities. Mongosniff can be considered as customized for MongoDB, similar to tcpdump used for TCP/IP network traffic analysis. Mongosniff is commonly used to drive development. Mongosniff Libpcap is required and only available to Unix-like systems. Compared to Mongosniff, Wireshark, a popular network sniffing tool, can be used to detect and parse MongoDB line protocols.
grammar
The following commands connect to mongod or Mongos running on 27017 and 27018 of localhost:
mongosniff --source NET lo 27017 27018
Copy the code
The following command logs only invalid BSON objects from Mongod or Mongos running on 27018 of localhost for development and problem tracking:
mongosniff --objcheck --source NET lo 27018
Copy the code
mongodump
In Mongodb, we use mongodump command to back up Mongodb data. This command can export all data to a specified directory. The mongodump command can specify the server to which the exported data is to be transferred.
grammar
The syntax of the mongodump command script is as follows:
>mongodump -h dbhost -d dbname -o dbdirectory
Copy the code
- H: IP address of the server where MongDB resides, for example, 127.0.0.1. You can also specify the port number 127.0.0.1:27017
- D: database instance to be backed up, for example, test
- O: indicates the location for storing backup data, for example, c:\data\dump. Of course, this directory must be created in advance. After the backup is complete, the system automatically creates a test directory in the dump directory to store backup data of the database instance.
The instance
Start your Mongod service locally using 27017. Go to the bin directory of the MongoDB installation directory and run the mongodump command:
>mongodump
Copy the code
After the preceding command is executed, the client will connect to the MongoDB service whose IP address is 127.0.0.1 and port number is 27017, and back up all data to the bin/dump/ directory. The following command output is displayed:The optional parameters of the mongodump command are as follows:
grammar | describe | The instance |
---|---|---|
mongodump –host HOST_NAME –port PORT_NUMBER | This command backs up all MongoDB data | mongodump –host w3cschool.cc –port 27017 |
mongodump –dbpath DB_PATH –out BACKUP_DIRECTORY | mongodump –dbpath /data/db/ –out /data/backup/ | |
mongodump –collection COLLECTION –db DB_NAME | This command backs up a collection of specified databases. | mongodump –collection mycol –db test |
mongorestore
Mongodb uses the mongorerstore command to restore the backup data.
grammar
The mongorestore command script syntax is as follows:
>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
Copy the code
- H: IP address of the MongoDB server
- D: Database instance to be restored, for example, test. However, the name may be different from the backup instance, for example, test2
- Directoryperdb: directory where backup data is stored e.g. c:\data\dump\test why add test instead of dump when backup is performed
- — DROP: Deletes the current data and then restores the backup data. That is to say, after recovery, after backup add modified data will be deleted, use with caution!
Next we execute the following command:
>mongorestore
Copy the code
The following output is displayed:
mongoimport
The Mongoimport tool in Mongodb can import content from a particular format file into a specified collection. The tool can import data in JSON format or CSV format. The specific usage is as follows:
[root@localhost mongodb]# ./bin/mongoimport --help options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --version print the program's version and exit -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets) --port arg server port. Can also use --host hostname:port --ipv6 enable IPv6 support (disabled by default) -u [ --username ] arg username -p [ --password ] arg password --dbpath arg directly access mongod database files in the given path, instead of connecting to a mongod server - needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path --directoryperdb if dbpath specified, each db is in a separate directory --journal enable journaling -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) -f [ --fields ] arg comma separated list of field names e.g. -f name,age --fieldFile arg file with fields names - 1 per line --ignoreBlanks if given, empty fields in csv and tsv will be ignored --type arg type of file to import. default: json (json,csv,tsv) --file arg file to import from; if not specified stdin is used --drop drop collection first --headerline CSV,TSV only - use first line as headers --upsert insert or update objects that already exist --upsertFields arg comma-separated fields for the query part of the upsert. You should make sure this is indexed --stopOnError stop importing at first error rather than continuing --jsonArray load a json array, not one item per line. Currently limited to 4MB.Copy the code
Parameter Description:
- H: Specifies the IP address of the database host
- U: indicates the database user name
- P: indicates the password of the database
- D: Specifies the name of the database
- C: Specify the name of the collection
- F: Specify which columns to import
Example:
Delete the data in students first and verify
> db.students.remove()
> db.students.find()
Copy the code
Then import the contents of the exported students.dat file above
[root@localhost mongodb]# ./bin/mongoimport -d test -c students students.dat connected to: [root@localhost mongodb]#Copy the code
Parameter Description:
- D: Specifies the database name, which is test in this example
- C: Specify the collection name, which in this case is students
- Dat: indicates the file name of the import file
- Query the data in the STUDENTS collection
> db.students.find()
{ "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }
>
Copy the code
The data import is successful
To import content from a JSON file, use the –type parameter to specify the import format, as shown below:
Delete the data first
> db.students.remove()
> db.students.find()
Copy the code
Then import the exported students_csv.dat file
[root@localhost mongodb]# ./bin/mongoimport -d test -c students --type csv --headerline --file students_csv.dat
connected to: 127.0.0.1
imported 10 objects
[root@localhost mongodb]#
Copy the code
Parameter Description:
- Type: indicates the format of the file to be imported
- Headerline: indicates that the first line is the column name and does not need to be imported
- File: indicates the file to be imported
Query the students collection to verify whether the import was successful:
> db.students.find()
{ "_id" : ObjectId("503266029355c632cd118ad8"), "classid" : 1, "name" : "kobe", "age" : 20 }
{ "_id" : ObjectId("503266029355c632cd118ad9"), "classid" : 1, "name" : "nash", "age" : 23 }
{ "_id" : ObjectId("503266029355c632cd118ada"), "classid" : 2, "name" : "james", "age" : 18 }
{ "_id" : ObjectId("503266029355c632cd118adb"), "classid" : 2, "name" : "wade", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118adc"), "classid" : 2, "name" : "bosh", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118add"), "classid" : 2, "name" : "allen", "age" : 25 }
{ "_id" : ObjectId("503266029355c632cd118ade"), "classid" : 1, "name" : "howard", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118adf"), "classid" : 1, "name" : "paul", "age" : 22 }
{ "_id" : ObjectId("503266029355c632cd118ae0"), "classid" : 2, "name" : "shane", "age" : 24 }
>
Copy the code
The import is successful.
mongoexport
The Mongoexport tool in Mongodb can export a collection to JSON format or CSV format. You can specify the data item to export with parameters, or you can export data based on specified conditions. Mongoexport specific usage is as follows:
[root@localhost mongodb]# ./bin/mongoexport --help Export MongoDB data to CSV, TSV or JSON files. options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --version print the program's version and exit -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets) --port arg server port. Can also use --host hostname:port --ipv6 enable IPv6 support (disabled by default) -u [ --username ] arg username -p [ --password ] arg password --dbpath arg directly access mongod database files in the given path, instead of connecting to a mongod server - needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path --directoryperdb if dbpath specified, each db is in a separate directory --journal enable journaling -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) -f [ --fields ] arg comma separated list of field names e.g. -f name,age --fieldFile arg file with fields names - 1 per line -q [ --query ] arg query filter, as a JSON string --csv export to csv instead of json -o [ --out ] arg output file; if not specified, stdout is used --jsonArray output to a json array rather than one object per line -k [ --slaveOk ] arg (=1) use secondaries for export if available, default trueCopy the code
Parameter Description:
- H: Specifies the IP address of the database host
- U: indicates the database user name
- P: indicates the password of the database
- D: Specifies the name of the database
- C: Specify the name of the collection
- F: Specify which columns to export
- O: Indicates the file name to export
- Q: Specifies the filtering conditions for the exported data
The instance
There is a set of students in the test library, and the data in the set is as follows:
db.students.find()
{ “_id” : ObjectId(“5031143350f2481577ea81e5”), “classid” : 1, “age” : 20, “name” : “kobe” } { “_id” : ObjectId(“5031144a50f2481577ea81e6”), “classid” : 1, “age” : 23, “name” : “nash” } { “_id” : ObjectId(“5031145a50f2481577ea81e7”), “classid” : 2, “age” : 18, “name” : “james” } { “_id” : ObjectId(“5031146a50f2481577ea81e8”), “classid” : 2, “age” : 19, “name” : “wade” } { “_id” : ObjectId(“5031147450f2481577ea81e9”), “classid” : 2, “age” : 19, “name” : “bosh” } { “_id” : ObjectId(“5031148650f2481577ea81ea”), “classid” : 2, “age” : 25, “name” : “allen” } { “_id” : ObjectId(“5031149b50f2481577ea81eb”), “classid” : 1, “age” : 19, “name” : “howard” } { “_id” : ObjectId(“503114a750f2481577ea81ec”), “classid” : 1, “age” : 22, “name” : “paul” } { “_id” : ObjectId(“503114cd50f2481577ea81ed”), “classid” : 2, “age” : 24, “name” : “shane” }
It can be seen from the above that there are three fields in the document: Classid, AGE, name 1. Export data directly to a file
[root@localhost mongodb]# ./bin/mongoexport -d test -c students -o students.dat connected to: 127.0.0.1 exported nine recordsCopy the code
Dat file -rw-r–r– 1 root root 869 Aug 21 00:05 Students. Dat The following information is displayed:
[root@localhost mongodb]# cat students.dat
{ "_id" : { "$oid" : "5031143350f2481577ea81e5" }, "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : { "$oid" : "5031144a50f2481577ea81e6" }, "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : { "$oid" : "5031145a50f2481577ea81e7" }, "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : { "$oid" : "5031146a50f2481577ea81e8" }, "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : { "$oid" : "5031147450f2481577ea81e9" }, "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : { "$oid" : "5031148650f2481577ea81ea" }, "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : { "$oid" : "5031149b50f2481577ea81eb" }, "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : { "$oid" : "503114a750f2481577ea81ec" }, "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : { "$oid" : "503114cd50f2481577ea81ed" }, "classid" : 2, "age" : 24, "name" : "shane" }
Copy the code
Parameter Description:
- D: Specifies the library to use, in this case test
- C: Specify the collection to export, in this case students
- O: Specifies the file name to export. In this case, the file name is students.dat
As you can see from the above results, we do not display the specified export style when exporting data, and export data in JSON format by default. If we need to export data in CSV format, we need to use the — CSV parameter, as shown below:
[root@localhost mongodb]# ./bin/mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat connected to: 127.0.0.1 exported 9 Records [root@localhost mongodb]# cat students_csv.dat Classid,name,age 1.0," Kobe ",20.0 2.0, 1.0, "Nash", 23.0 "James", 18.0, 2.0, "wade", 19.0, 2.0, "bosh", 19.0, 2.0, "Allen", 25.0, 1.0, "Howard", 19.0, 1.0, "Paul", 22.0 2.0, "shane", 24.0 [root @ localhost mongo] #Copy the code
Parameter Description:
- CSV: Indicates the CSV format to be exported
- F: Indicates that the data in columns classid, NAME, and age needs to be exported
As can be seen from the above results, Mongoexport successfully exported the data to students_csv.dat file according to CSV format.
bsondump
Dump the Bson file to JSON data
mongorestore
MongoDB data restoration tool. MongoDB uses the mongorerstore command to restore backup data.
grammar
The mongorestore command script syntax is as follows:
>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
Copy the code
-
H: IP address of the MongoDB server
-
D: Database instance to be restored, for example, test. However, the name may be different from the backup instance, for example, test2
Directoryperdb: directory where backup data is stored e.g. c:\data\dump\test why add test instead of dump when backup is performed
— DROP: Deletes the current data and then restores the backup data. That is to say, after recovery, after backup add modified data will be deleted, use with caution!
Next we execute the following command:
>mongorestore
Copy the code
The following output is displayed:
mongod.exe
MongoDB service startup tool, we can view all parameters of Mongod through mongod –help, the following is the Chinese explanation of each parameter. Basic configuration parameters:
parameter | instructions |
---|---|
–quiet | Quiet output |
–port arg | Specify the service port number. The default port number is 27017 |
–bind_ip arg | If the service IP address is bound to 127.0.0.1, only the local host can access the IP address |
–logpath arg | Specify the MongoDB log file, not the directory |
–logappend | Use appending to write logs |
–pidfilepath arg | Full path to PID File. If not set, no PID File |
–keyFile arg | The full path of the cluster’s private key is valid only for the Replica Set architecture |
–unixSocketPrefix arg | UNIX domain socket replacement directory,(default: / TMP) |
–fork | Run MongoDB as a daemon to create a server process |
–auth | Enables validation |
–cpu | Display CPU utilization and IOWAIT periodically |
–dbpath arg | Specify the database path |
–diaglog arg | Diaglog options 0=off 1=W 2=R 3=both 7=W+some reads |
–directoryperdb | Set up that each database will be saved in a separate directory |
–journal | With logging enabled, MongoDB data operations will be written to files in the Journal folder |
–journalOptions arg | Enable log diagnostics |
–ipv6 | Enabling the IPv6 option |
–jsonp | Allow JSONP form access over HTTP (with security implications) |
–maxConns arg | The default maximum number of concurrent connections is 2000 |
–noauth | Disable authentication |
–nohttpinterface | Disable the HTTP interface. By default, access to port 27018 is disabled |
–noprealloc | Disable data file preallocation (often affecting performance) |
–noscripting | Disabling the Script Engine |
–notablescan | Table scanning is not allowed |
–nounixsocket | Disable Unix socket listening |
–nssize arg (=16) | Set the size of the letter database.ns file (MB) |
–objcheck | After receiving customer data, checking its validity, |
–profile arg | File parameters 0=off 1=slow, 2=all |
–quota | Limit the number of files per database to 8 by default |
–quotaFiles arg | # number of files allower per db, requires –quota |
–rest | Enable simple REST apis |
–repair | Run repair on all DBS |
–repairpath arg | The directory of the files generated by the repair library. The default directory name is dbpath |
–slowms arg (=100) | value of slow for profile and console log |
–smallfiles | Use a smaller default file |
–syncdelay arg (=60) | Number of seconds for data to be written to disk (0=never, not recommended) |
–sysinfo | Print some diagnostic system information |
–upgrade | If you need to upgrade the database |
Replicaton parameters
parameter | instructions |
---|---|
–fastsync | Enable the slave library replication service from a DBPATH whose database is a snapshot of the master library and can be used to quickly enable synchronization |
–autoresync | If the slave database synchronizes data much worse than the master database, automatically resynchronizes, |
–oplogSize arg | Setting the size of oplog (MB) |
Master/slave parameters
parameter | instructions |
---|---|
–master | The main library |
–slave | From library mode |
–source arg | Slave library port number |
–only arg | Specify a single database replication |
–slavedelay arg | Set the delay for synchronizing the master library from the secondary library |
Replica Set option
parameter | instructions |
---|---|
–replSet arg | Sets the replica set name |
Sharding option
parameter | instructions |
---|---|
–configsvr | Declare this to be a config service for the cluster, default port 27019, default directory /data/configdb |
–shardsvr | Declare this to be a cluster shard, default port 27018 |
–noMoveParanoia | Turn off paranoia for moveChunk data saving |
The sample
./mongod -shardsvr -replSet shard1 -port 16161 -dbpath /data/mongodb/data/shard1a -oplogSize 100 -logpath /data/mongodb/logs/shard1a.log -logappend -fork -rest
Copy the code
The above parameters can be written into the mongod. Conf configuration file for example:
dbpath = /data/mongodb
logpath = /data/mongodb/mongodb.log
logappend = true
port = 27017
fork = true
auth = true
Copy the code
mongostat
Mongostat is mongodb’s own state detection tool, which can be used on the command line. It gets the current running status of mongodb at regular intervals and outputs it. If you find that your database is suddenly slowing down or you have other problems, consider using Mongostat first hand to view mongo’s state.
Start your Mongod service, go to the bin directory in your installed MongoDB directory, and then type mongostat as follows:
D:\set up\mongodb\bin>mongostat
Copy the code
The following output is displayed:
Mongofiles – GridFS management tool, can achieve binary file access
mongotop
Mongotop is also a built-in tool in MongoDB. Mongotop provides a way to track a MongoDB instance and see what time is being spent reading and writing data. Mongotop provides level statistics for each collection. By default, Mongotop returns the value every second.
Start your Mongod service, goto the bin directory in your installed MongoDB directory, and then type mongotop as follows:
D:\set up\mongodb\bin>mongotop
Copy the code
The following output is displayed:Case with parameters
E:\mongodb-win32-x86_64-2.21.\bin>mongotop 10
Copy the code
After 10 is the parameter, can not use, wait length, in seconds, mongotop wait between calls. Go through the default mongotop to return data every second.
E:\mongodb-win32-x86_64-2.21.\bin>mongotop --locks
Copy the code
Mongotop: mongotop: mongotop: mongotop: mongotop: mongotop: mongotop: mongotop: mongotop: mongotop: mongotop: mongotop: mongotopThe fields in the output are as follows:
- ns:
Contains the database namespace, which combines the database name and collection.
- db:
Contains the name of the database. A database named. Is targeted at global locking, not specific databases.
- total:
The amount of time mongod spends working in this namespace is provided.
- Read:
Provides a lot of time that mongod spends performing read operations in this namespace.
- Write:
The Mongod took a lot of time to provide this namespace for writing operations.