In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What is MongoDB?
MongoDB is written in C++ language and is an open source database system based on distributed file storage.
In the case of high load, adding more nodes can ensure server performance.
MongoDB aims to provide scalable, high-performance data storage solutions for web applications.
MongoDB stores the data as a document, and the data structure consists of key-value (key= > value) pairs. MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.
! [] (https://cache.yisu.com/upload/information/20200310/34/99634.jpg)
# # main Features
* MongoDB provides a document-oriented storage that is simple and easy to operate.
* you can set the index of any property in the MongoDB record (such as FirstName= "Sameer", Address= "8 Gandhi Road") for faster sorting.
* data mirrors can be created locally or on the network, which makes MongDB more scalable.
* if the load increases (more storage space and stronger processing power are needed), it can be distributed among other nodes in the computer network, which is called sharding.
* MongoDB supports rich query expressions. Query instructions use tags in the form of JSON to easily query objects and arrays embedded in documents.
* MongoDB uses the update () command to replace the completed document (data) or some specified data fields.
* Map/reduce in MongoDB is mainly used for batch processing and aggregation of data.
* map and reduce. The Map function calls emit (key,value) to iterate through all the records in the collection, passing key and value to the reduce function for processing.
* map and reduce functions are written in JavaScript and can perform MapReduce operations through db.runcommand or marpreduce commands.
* GridFS is a built-in feature in MongDB that can be used to store a large number of small files.
* MongoDB allows scripts to be executed on the server. You can write a function in JavaScript and execute it directly on the server, or you can store the definition of the function on the server and call it directly next time.
* MongoDB supports various programming languages: RUBY,PYTHON,JAVA,C++.PHP,C# and other languages.
* MongoDB is easy to install.
# # MongoDB tool
There are several management tools available for MongoDB.
# Monitoring
MongoDB provides a network and system monitoring tool, Munin, which is used in MongoDB as a plug-in.
Gangila is a high-performance system monitoring tool for MongoDB, which is used in MongoDB as a plug-in.
Cacti, an open source tool based on graphical interface, is used to view CPU load and network bandwidth utilization. It also provides a plug-in for monitoring MongoDB.
# GUI
* Fang of Mongo-Web page type, made up of Django and jQuery.
* Futon4Mongo-an mongodb copycat version of CouchDB Futon web.
* written by Mongo3-Ruby.
* MongoHub-an application for OSX.
* Opricot-A browser-based MongoDB console written by PHP.
* Database Master-Windows's mongodb management tool
* RockMongo-the best MongoDB management tool for PHP, lightweight, supporting multiple languages.
# # installation and deployment of MongoDB
# 1. Install dependency packages
`yum install-y openssl-devel openssl`
# 2. Download the mongodb package
`cd / home/test/ tools`
`wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.4.tgz`
`tar xf mongodb-linux-x86_64-rhel62-3.2.4.tgz`
`mv mongodb-linux-x86_64-rhel62-3.2.4 / application/mongodb-3.2.4/ `
`ln-s / application/mongodb-3.2.4/ / application/mongodb `
`mkdir-p / data/ mongodb`
`mkdir-p / data/logs/ mongodb`
`touch / data/logs/mongodb/ mongodb.log`
`chmod-R 777 / data/logs/mongodb/ Mongodb.log`
# 3. Configuration file
Vim / application/mongodb/bin/mongodb.conf
Port=27017 # port
Dbpath= / data/mongodb # data file storage directory
Logpath=/data/logs/mongodb/mongodb.log # log file storage directory
Logappend=true # writes logs by appending
Fork=true # is enabled as a daemon, that is, running in the background
MaxConns=5000 # maximum number of simultaneous connections defaults to 2000
Bind_ip=127.0.0.1172.16.20.126 # is only allowed through local area network IP:172.16.20.126 and local access
Noauth=true # does not enable authentication
Nohttpinterface=true
Rest=false
# 4. Environment variable setting
Echo 'export PATH=/application/mongodb/bin:$PATH' > > / etc/profile
Source / etc/profile
# 5. Start and shut down
Numactl-interleave=all / application/mongodb/bin/mongod-config / application/mongodb/bin/mongodb.conf
Or
# numactl-- interleave=all / application/mongodb/bin/mongod-f / application/mongodb/bin/mongodb.conf
About to fork child process, waiting until server is ready for connections.
Forked process: 20394
Child process started successfully, parent exiting
Normal stop method:
/ application/mongodb/bin/mongod-shutdown-dbpath / data/mongodb/
# ps aux | grep mongod
# kill-2 PID
Or
# / application/mongodb/bin/mongo-port 27017
> use admin
> db.shutdownServer ()
2015-03-19T00:43:27.760+0800 I CONTROL [initandlisten] * * WARNING: / sys/kernel/mm/transparent_hugepage/enabled is' always'.
2015-03-19T00:43:27.760+0800 I CONTROL [initandlisten] * * We suggest setting it to 'never'
For the solution without restarting the server, execute it under Linux:
Echo never > / sys/kernel/mm/transparent_hugepage/enabled
Echo never > / sys/kernel/mm/transparent_hugepage/defrag
Take effect immediately after the server is restarted:
# vi / etc/rc.local
If test-f / sys/kernel/mm/transparent_hugepage/enabled; then
/ bin/echo never > / sys/kernel/mm/transparent_hugepage/enabled
Fi
If test-f / sys/kernel/mm/transparent_hugepage/defrag; then
/ bin/echo never > / sys/kernel/mm/transparent_hugepage/defrag
Fi
# 6. Power on and start mongodb automatically
Vi / etc/rc.d/rc.local
/ bin/rm-rf / data/mongodb/mongod.lock
/ usr/bin/numactl-- interleave=all / application/mongodb/bin/mongod-f / application/mongodb/bin/mongodb.conf
# detailed description of Mongodb configuration file
# in the Mongod installation package, there are two process startup files: mongod and mongos;, where mongod is the core basic process, which is used to receive read and write requests and is responsible for storing actual data. Mongodb instances are the basic units of the cluster, such as Replication set, sharding Cluster, Config Servers, etc. Mongos is a legal person "routing" process in Sharding Cluster architecture mode, that is, the client requests to access the mongos, and then the mongos forwards the request to the appropriate sharding server to perform the operation and returns the result to the client, so the mongos basically does not store data, but caches the corresponding relationship between shark key and sharding server in memory, which is convenient for routing.
# # explanation of important configuration parameters
# 1.processManagement:
Fork:
# description: whether to run the mongod/mongos process in fork mode. Default is false.
PidFilePath:
# description: write the mongod/mongos process ID to the specified file with the "fork:true" parameter. If not specified, the PID file will not be created.
# 2.net:
Bindip:
# description: the IP,application bound by the mongod/mongos process establishes a connection through this IPport. You can bind to any Nic interface. If your mongos/mongod only needs private network access, you can bind private network IP. If you need public network access, bind public network IP. If the current value is "0.0.0.0", then bind to all interfaces, that is, private network and public network IP can be accessed. (not recommended) can be bound to multiple IP, IP addresses are separated by ",".
Port:27017
# description: mongos and mongod listening ports. Default is 27017. However, because mongodb has two typical architecture modes: replica set and sharding, if a developer deploys multiple mongod instances on a node, it is necessary to modify this port to avoid conflicts.
Maxincomconnections:65536
# description: the maximum number of connections allowed by the mongod/mongos process. If this value exceeds the configured connection threshold of the operating system, it will not take effect (ulimit); the default value is 65536. Usually the client will use the connection pooling mechanism, which can effectively control the number of connections per client.
WireObjectCheck:true
# description: when the client writes data, whether mongod/mongos detects the validity of the data (BSON). If the data is not well formatted, the inster and update operations will be rejected. Default is true.
Ipv6:false
# description "whether the use of IPV6 network between multiple instances of mongos/mongod is supported. The default value is false. This value needs to be consistent throughout the cluster."
# 3.storage:
Dbpath:db
# description: the mongod process stores the data directory. This configuration is only valid for mongod. The default value is / data/db
IndexBuildRetry:true
# description: if mongod closes unexpectedly when building an index, whether to rebuild the index at the next startup; if the index construction fails, the unfinished index will be deleted after mongod restart, but whether to rebuild this parameter is determined. The default is true.
RepairPath:_tmp
# description: with-repair startup command parameter, use this directory to store temporary data during repair, and the data in this directory will be deleted at the end of repair. This configuration is only valid for mongod processes. It is not recommended to configure it in the configuration file, but use the mongod startup command to specify it.
Engine:mmapv1
# description: storage engine type. "mmapv1" and "wiredTiger" are supported after mongod3.0. The default is "mmapv1". It is officially declared that wiredTiger engine is more excellent.
Journal:enable;true
# description: whether to enable persistent storage of journal logs. Journal logs are used for data recovery, which is the most basic feature of mongod and is usually used for fault recovery. The 64-bit system defaults to true,32 bit and defaults to false. It is recommended to enable it. It is only valid for mongod processes.
DirectoryPerDB:false
# description: whether to store data from different DB in different directories, subdirectories of dbPath, and the name of the directory is db. To modify this value for a mongod that already stores data, you need to first export the data using the mongodump directive, then close the mongod, modify the value and specify a new dbPath, and then use the mongorestore directive to re-import the data. (that is, export the data and use mongorestore to rewrite the data to the new directory of mongod)
# for replica set architecture mode, you only need to operate in each secondary in turn: close secondary, configure a new dbPath, and then start (initialize sync and fully synchronize data from primary to local). It is best to operate primary. This parameter is valid only for mongod processes. The default value is false. It is not recommended to modify this value.
SyncPeriodSecs:60
# description: the time interval for mongod to flush data to disk using fsync operation. The default value is 60 (in seconds). It is strongly recommended not to modify this value. Mongod writes the changed data to memory after writing to journal, and intermittently flush the memory data to disk, that is, delay writing to disk, which can effectively improve disk efficiency. This directive does not affect journal storage and is valid only for mongod.
Mmapv1: (the following configuration is valid for MMAPV1 engine only)
Quota:enforce;false
# description: quota management, whether to limit the maximum file data that each DB can hold. It is only valid for mongod. The default value is false. It is recommended to keep the default value.
MaxFilesPerDB:8
# description: if enforce is enabled, the storage files held by each DB will not exceed this threshold. Valid only for mongod processes.
SmallFile:false
# description: whether to use small files to store data; if this value is true,mongod, the size of each data file will be limited to 512m (default is 2G), and journal will be reduced to 128m (default is 1G). If the amount of data in DB is large, it will cause each DB to create a large number of small files, which will have a certain impact on performance. In production environment, it is not recommended to modify this value. It can be set to true during testing to save disk.
Journal:commitlntervalMs:100
# description: the interval between which the mongod process commits the journal log, that is, the interval of the fsync. Considering the disk effect, mongod intermittent flush log data; the lower the value, the lower the likelihood of data loss, the lower the disk consumption, and the lower the performance. If you want the write operation to force writing to the journal immediately, you can pass the parameter option "journal:true" (which can be specified in the client-side write operation), which (including those that have not been previously committed) will immediately fsync to disk. Valid for mongod only (in milliseconds)
# nsSize: the namespace file size of each database is 16 by default, in M; the maximum value can be set to 2048, which increases the size of the ".ns" suffix file. 16m can basically save 24000 named entries, create a new collection or index information, which will add a namespace entry; if you need to create a large number of collection (such as data analysis) under your database, you can appropriately increase this value.
WiredTiger: (the following configuration only applies to wiredTiger engine version 3.0 or later)
EngineConfig:cacheSizeGB:8
# description: wiredTiger caches the memory size of working set (working set) data in GB. This value determines that wiredTiger differs from mmapv1's memory model in that it can limit the amount of memory used by mongod, while mmapv1 cannot (depending on system-level mmap). By default, the value of cacheSizeGB assumes that only one mongod instance is deployed on the current node, which is half the size of physical memory; if multiple mongod processes are deployed on the current node, this value needs to be configured reasonably. If mongod is deployed in a virtual container (for example, lxc,cgroups,Docker), it will not be able to use the physical memory of the entire system, so this value needs to be adjusted appropriately. The default value is half of physical memory.
JournalCompressor:snappy
# description: compression algorithm for journal logs. Available values are "none", "snappy" and "zlib".
DirectoryForlndexes:false
# description: whether to store index and collection data in a separate directory of dbPath. That is, the index data is saved in the "index" subdirectory, and the collection data is saved in the "collection" subdirectory. The default value is false, which is valid only for mongod.
Collectionconfig:
BlockCompressor:snappy
# description: collection data compression algorithm. Available values include "none", "snappy" and "zlib". Developers can specify values to override this configuration item when creating the collection. If the data already exists in the mongod, modifying the secondary value will not cause a problem, the old data will still be decompressed using the original algorithm, and the new data file will be decompressed with the new algorithm.
IndexConfig:
Prefixcompress:true
# description: whether to use prefix compression (prefix compress, an algorithm) for index data. Prefix compression, for those sorted value storage, is of great help, can effectively reduce the memory usage of data, the default value is true.
# 4.operationProfiling:
SlowOpThresholdMs:100
# description: the database profile determines the time threshold (in milliseconds) for an operation to be a "slow query". Mongdb will log the slow query to the log, even if profile is disabled. When profile is turned on, slow query records are also written to the collection of the "system.profile" system. The default value is valid only for mongd processes.
Mode: off
# description: database profiler level, the performance information of the operation will be written to the log file. Available values:
1) off: close profiling
2) slowOp:on, which only contains operation log
3) all:on, record all operations
Database profiling can affect performance. It is recommended to enable it only during the performance debugging phase. This parameter is valid only for mongd.
# 5.replication: (copy set architecture mode configuration, if it is only a single point, no configuration is required)
OplogSizeMB:10240
# description: the maximum size of replication operation log (in MB). The mongod process creates an oplog based on the maximum available disk space, such as a 64-bit operating system, and oplog is 5% of the disk free space. Once mongd has created an oplog file, modifying oplogSizeMB again will not take effect. This value should not be set in size, and should be sufficient to save 24-hour operation logs to ensure that secondary has sufficient maintenance time; if it is too small, secondary will not be able to synchronize data through oplog, only full synchronization. This value is valid only for mongd.
EnableMajorityReadConcern:false
# description: the level of whether to enable readConcern is "majority", and the default value is false;. Only when this option is enabled can "majority" be used in read operations. (version 3.2 +)
ReplSetName:
# description: the name of "replication set". All mongd instances in the replication set must have the same name. Under sharding distribution, different sharding should use different replSetName. Valid for mongod only
SecondaryIndexPrefetch: all
# description: it is only valid for mmapv1 storage engine. The secondary in the replication set will load the index into memory before applying the change operation from oplog. By default, secondaries will first load the index related to the operation into memory, and then apply the operation according to oplog. Optional values:
1) none:secondaries does not load index data into content
2) all:sencondaries loads the index data related to this operation into memory
3) _ id_only: only _ id index is loaded
The default value is all, and this configuration is valid only for mongod.
LocalPingThresholdMs: 15
# description: ping time (in milliseconds). Mongos is used to determine which secondary to send the client read request. Valid for mongos only. The default value is 15, which is the same as the default value in the client driver. When mongos receives a client-side read request, it will:
1. Find out the member with the lowest ping value in the replication set.
2. Build a list of members where the delay value is allowed by this value
3. Randomly select a member from the list.
The ping value is dynamic and is calculated every 10 seconds. Mongos forwards client requests to a secondary node with less latency (compared to this value). Valid for mongos only.
# 6sharding: (valid only in sharding architecture mode)
ClusterRole:
# description: in sharding cluster, the role of this mongod instance. Available values:
1. Configsvr: this instance is config server, and this instance listens on port 27019 by default
2. Shardsvr: this instance is shard (sharding), listening on port 27018
This configuration is valid only for mongod. Usually config server and sharding server need to use their own configuration files.
ArchiveMovedChunks: true
# description: when chunks migrates to other nodes because of "load balancing", whether mongod will archive these chunks and save them in the "moveChunk" directory under dbPath. Mongod will not delete the files under moveChunk. The default is true.
AutoSplit: true
# description: whether to enable automatic splitting of sharded collections is only valid for mongos. If all mongos is set to false, the collections data grows but cannot be split into new chunks. Because any mongos process in the cluster can trigger split, this value needs to be consistent across all mongos lines. Valid for mongos only.
ConfigDB:
# description: set the address list of config server. Each server address is divided by ",". One or three config server are usually specified in the sharded cluster. (production environment, usually 3 config server, but 1 is also OK). All mongos instances must be configured the same, otherwise it may cause unnecessary problems. Valid for mongos only.
ChunkSize: 64
# description: the size of each chunk in sharded cluster (in MB). The default is 64, which is ideal for most applications. Too much chunkSize will lead to uneven distribution, and too small will cause it to split into a large number of chunk and move frequently.
# # this value needs to be consistent throughout the sharding cluster. Modifying this value after the cluster is started will no longer take effect. Valid for mongos only.
# 7. SytemsLog: (Syslog, which must be configured)
Verbosity: 0
# description: log level, 0: default, containing "info" information. 1-5, that is, values greater than 0 will contain debug information.
Quiet: true
# description: "quiet", when mongod/mongos will try to reduce the output of logs. It is not recommended to turn it on in the production environment, otherwise it will make it difficult to trace errors.
TraceAllExceptions: true
# description: print exception details.
Path: logs/mongod.log
LogAppend: false
# description: if true, logs will be added at the end of the existing log when mongod/mongos is restarted. Otherwise, the current log file is backed up and a new log file is created; the default is false.
LogRotate: rename
# description: log "turnaround" to prevent a log file from being particularly large, use the logRotate directive to "rotate" the file. Available values:
1) rename: rename the log file, default.
2) reopen: using the linux log rotate feature, close and reopen this log file to avoid log loss, but the logAppend must be true.
Destination: file
# description: log output destination can be specified as "file" or "syslog". If not specified, it will be output to standard output (standard output).
# 8. Security-related configuration (summary introduction)
# Java Code Collection Code
# security:
Authorization: enabled
ClusterAuthMode: keyFile
KeyFile: / srv/mongodb/keyfile
JavascriptEnabled: true
# setParameter:
EnableLocalhostAuthBypass: true
AuthenticationMechanisms: SCRAM-SHA-1
1) authorization:disabled or enabled is only valid for mongod; indicates whether to enable user access Control (Access Control), that is, the client can access system data through user name and password authentication. The default is "disabled", that is, the client can access database data without a password. (limit client authentication with mongod and mongos)
2) clusterAuthMode: the authentication mode between members in the cluster. Available values are "keyFile", "sendKeyFile", "sendX509" and "x509", which are valid for mongod/mongos. The default value is "keyFile". Mongodb officially recommends x509, but I personally think keyFile is easier to learn and use. However, in version 3.0, mongodb has added support for TLS/SSL, and if possible, it is recommended to use SSL-related configurations to authenticate the cluster's member, which will not be covered in this article. (limit authentication between members in the cluster)
3) keyFile: when clusterAuthMode is "keyFile", this parameter specifies the location of keyfile, and mongodb needs permission to access this file.
4) javascriptEnabled:true or false, which defaults to true and is only valid for mongod, indicates whether to disable the javascript function on server, that is, whether to allow javascript scripts to be executed on mongod. If false, mapreduce, group commands, etc., cannot be used because they need to execute javascript script methods on server. If there is no need for operations such as mapreduce in your application, you can turn off javascript for security reasons.
"setParameter" allows you to specify some Server-side parameters that do not depend on the storage engine and interaction mechanism, but only fine-tune the running state of the system, such as "authentication mechanism", "thread pool parameters", and so on. See [parameter]
1) enableLocalhostAuthBypass:true or false, default is true, which is valid for mongod/mongos; indicates whether to enable "localhost exception". For sharding cluster, we prefer to enable it on mongos and disable it on mongod of shard node.
2) authenticationMechanisms: authentication mechanism. Available values are "SCRAM-SHA-1", "MONGODB-CR", "PLAN", etc. It is recommended to be "SCRAM-SHA-1", which is valid for mongod/mongos. Once the authentication mechanism is selected, the client needs to match it when accessing databases.
# 9. Performance-related parameters
# # Java Code Collection Code
# setParameter:
ConnPoolMaxShardedConnsPerHost: 200
ConnPoolMaxConnsPerHost: 200
Notablescan: 0
1) connPoolMaxShardedConnsPerHost: the default value is 200, which is valid for mongod/mongos. It represents the maximum capacity of the current mongos or shard link pool with other shards in the cluster, which we usually do not adjust. The capacity of the connection pool does not prevent the creation of new links, but the number of links obtained from the connection pool does not exceed this value. Maintaining a connection pool requires a certain amount of expense, and maintaining a link also requires a certain amount of system resources.
2) connPoolMaxConnsPerHost: the default value is 200, which is valid for mongod/mongos. As above, it represents the capacity of the connection pool between mongos or mongod and other mongod instances, which is limited by host.
# # configuration example
# the configuration file after mongodb 3.0 is in YAML format, which is very simple. It is indicated by "space" and indented at the beginning. It is important to note that if there is a value after ":", it needs to be followed by a space, and if key only represents a hierarchy, there is no need to add a space after ":" (for example: systemLog: no space is required). According to the hierarchy, 4 spaces per line are indented, the second level is 8 spaces, which is pushed in turn, and the top level does not need space indentation. If the format is incorrect, the following error will occur:
# Java Code Collection Code
Error parsing YAML config file: yaml-cpp: error at line 3, column 1: illegal tab when looking for indentation
Error parsing YAML config file: yaml-cpp: error at line 15, column 12: illegal map value
# 1 、 mongod.conf
# Java Code Collection Code
# systemLog:
Quiet: false
Path: / data/mongodb/logs/mongod.log
LogAppend: false
Destination: file
# processManagement:
Fork: true
PidFilePath: / data/mongodb/mongod.pid
# net:
BindIp: 127.0.0.1
Port: 27017
MaxIncomingConnections: 65536
WireObjectCheck: true
Ipv6: false
# storage:
DbPath: / data/mongodb/db
IndexBuildRetry: true
Journal:
Enabled: true
DirectoryPerDB: false
Engine: mmapv1
SyncPeriodSecs: 60
Mmapv1:
Quota:
Enforced: false
MaxFilesPerDB: 8
SmallFiles: true
Journal:
CommitIntervalMs: 100
WiredTiger:
EngineConfig:
CacheSizeGB: 8
JournalCompressor: snappy
DirectoryForIndexes: false
CollectionConfig:
BlockCompressor: snappy
IndexConfig:
PrefixCompression: true
# operationProfiling:
SlowOpThresholdMs: 100
Mode: off
# if your architecture mode is replication Set, you also need to add the following configuration to all "replication set" members:
# Java Code Collection Code
# replication:
OplogSizeMB: 10240
ReplSetName: rs0
SecondaryIndexPrefetch: all
# if it is sharding Cluster architecture, you need to add the following configuration to the shard node:
# Java Code Collection Code
# sharding:
ClusterRole: shardsvr
ArchiveMovedChunks: true
# of course, a mongod instance can be either one of the member of a "replication set" or a shard in a sharding cluster, depending on your architecture model.
# the mongod process can be used as a "config server" instance, as long as you need to use "clusterRole: configsvr". Thus, a mongod instance can be one of the roles of "single point instance", "config server", "sharding server" + "replication set member". It is recommended to start it with different configuration files.
# 2 、 mongos.conf
# Java Code Collection Code
# systemLog:
Quiet: false
Path: / data/mongodb/logs/mongod.log
LogAppend: false
Destination: file
# processManagement:
Fork: true
PidFilePath: / data/mongodb/mongod.pid
# net:
BindIp: 127.0.0.1
Port: 37017
MaxIncomingConnections: 65536
WireObjectCheck: true
Ipv6: false
# replication:
LocalPingThresholdMs: 15
# sharding:
AutoSplit: true
ConfigDB: m1.com:27018,m2.com:27018,m3.com:27018
ChunkSize: 64
# mongos instances do not need to store actual data and consume a certain amount of memory, so they are used in sharding architecture mode. Mongos needs to receive requests to clients (while backend sharded and replication set do not need to be known to clients). It can forward client requests to secondary with relatively small latency on a sharding cluster (the sharding cluster is based on replication sets) and is also responsible for the splitting and migration of chunk.
# # other
# 1. Startup and shutdown
# most of the parameters in the configuration file can be specified through the process startup command. Usually, the parameters in the startup command line will overwrite the parameters in the configuration file.
# Java Code Collection Code
. / mongod-f mongod.conf
# of course, you can also start it by using multiple command line parameters, as shown in the following example:
# Java Code Collection Code
. / mongod-- bind_ip 127.0.0.1-- port 27017-- dbpath / data/mongodb/db-- logpath / data/mongodb/logs-- storageEngine mmapv1-- fork
# the directory address specified in the mongod configuration must be created first, otherwise it cannot be started, which is different from other systems.
# the startup method of mongos is the same as above. If you want to start mongod and mongos based on service, please refer to other documentation. You can shut down the mongod process through the kill directive, but this is a bit rough and can lead to data corruption in a production environment. It is recommended to use mongo shell to "cleanly" the mongod process, which is safe and does not cause data corruption.
# Java Code Collection Code
. / mongo
> use admin
> db.shutdownServer ()
# you can use "kill" to shut down, which is also "cleanly". If you use "kill-9" to force thread exit, it may result in data loss. If you are running mongod under non-fork, using the "CTRL-C" mode directly on the shell also means "cleanly" exit. For online environments, it is best not to "kill-9".
# 2 、 repair
# "repair" the database. When mongodb is running for a period of time, especially after a large number of deletions and update operations, we can use the repair instruction to "repair" the data storage, which will organize and compress the underlying data storage files and reuse disk space, which is equivalent to reorganizing the data, which plays a certain role in data optimization.
# if the journaling log feature is not enabled in mongod, the repair instruction can be used to organize data and eliminate corrupted data after the system exception crash. If the journaling log feature is enabled, we do not need to use repair to repair the data, because journal can help mongod recover data. In replication set mode, you can use repair, but you can usually delete old data directly and use "data synchronization" operation to achieve the purpose of "restoring" and "collating" data. The effect is the same as repair, and it is more efficient.
# repair requires a certain amount of spare space on the disk, which is the current amount of database data + 2GB. You can use "--repairpath" to specify the directory where temporary data is stored during repair. The repair directive also rebuilds the indexes, which reduces the data size of the index.
# if mongod accidentally crash, you need to start mongod normally first so that the data can be recovered according to the journal log before you can execute repair;. If there is any data in the journal log that has not been recovered, starting mongod using the repair instruction will fail.
# when repair, you need to close the mongod process, and then start it after execution.
# Java Code Collection Code
. / mongod-dbpath=/data/mongodb/db-repair
# mongodb tends to use shell to repair a specific database, which is relatively controllable and has the same internal working mechanism.
# Java Code Collection Code
>. / mongo
> user mydatabase
> db.repairDatabase ()
# 3. Mongodump and mongorestore
# We usually use the backup feature of mongodb data, or import a backup into a new mongod instance (data cold processing), so we need to use these two instructions.
# mongodump exports the entire contents of the databases to a binary file, and you can use mongorestore to load the entire file in other mongod. It is important to note that mongodump does not export data from the "local" database, and of course this local library does not make much sense for data recovery.
# "- u" parameter specifies the user name to access database, "- p" specifies the password, "--host" and "--port" specifies the location of the mongod instance, "--db" specifies the database that requires dump, if not specified, dump all databases, "--collection" specifies the collection table that requires dump, if not specified, dumpl all collections under the entire db "--query" specifies the query condition for dump, and "--out" specifies the path to the result output file:
# Java Code Collection Code
>. / mongodump-- host m1.com-- port 27017-u root-p pass-- out / data/mongodb/backup/dump_2015_10_10
# mongorestore imports dump data files into database,mongorestore to create a new database or add data to an existing database. If you restore the data into an existing database, mongorestore only executes insert, not update, and if the same "_ id" data already exists in the database, mongorestore will not overwrite the original document. Mongorestore recreates the indexes, and all operations are insert, not update.
The basic directive is similar to mongodump. "--db" specifies the db to which the data needs to be restore, and if the db does not exist, it will be created. If "--db" is not specified, the mongorestore will be recreated based on the db to which the original data belongs, which may result in data overwriting. " "--drop" means that deleting the original collections,--drop in the target db before the restore data will not delete the collection that is not in the dump file. " -- stopOnError "means to force an exit if an error occurs.
# Java Code Collection Code
. / mongorestore-- db mydatabase / data/mongodb/backup/dump_2015_10_10
# 4, mongoimport and mongoexport
# mongoexport exports the data to JSON or CSV format for other applications to parse.
# because the mongodb data is in BSON format and some data types are not available in JSON, the data types will still be lost when exporting JSON format. Therefore, if the exported data is intended to be recovered to other mongodb, it is recommended to use mongodump and mongorestore.
# Command parameters are the same as 3)
# 5. The mongostat instruction can intermittently print out "data storage", "flush", read / write times, network output and other parameters in the current mongod instance, which is an effective means to check the performance of mongod. Mongotop can view the read and write status of each database according to it.
# 6. Overview of mongo shell operation:
1) help: list all function
2) show dbs: show all the databases in the current instance.
3) use: switch to the specified db, and the next operation will be in this db.
4) show collections: shows all the collections in the current db.
5) show users: show all the users that have been added to the current db.
6) show roles: displays all built-in or custom user roles in the current db.
7) show profile: this involves the configuration related to profile. By default, it shows the last 5 operations that took more than 1 second, and is usually used to track slow queries.
8) db.help (): shows the operation function that can be done on db.
9) db..help (): shows what can be done on colleciton.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.