Detailed configuration instructions for MongoDB database in Linux

Source: Internet
Author: User
Tags auth file size mongodb socket syslog file permissions server port


There are 2 ways to start MongoDB, one is to specify configuration parameters directly, and the other is to specify a configuration file. The configuration file is introduced here, starting with the following:


Mongod--config/etc/mongodb.conf
The configuration is as follows:
VERBOSE: Log information redundancy. Default false. Improve internal reporting standard output or log files to LogPath configuration. To enable verbose or enable verbosity with VVVV parameters, such as:


Verbose = True
VVVV = True

PS: Start verbose verbose information, it has a level of vv~vvvvv,v the higher the level, the more detailed information recorded in the log file.
Port: Ports. Default 27017,MONGODB The default service TCP port that listens for client connections. If the port settings are less than 1024, such as 1021, you need root permission to start, can not use the MongoDB account to start, (ordinary account even if 27017 do not do) otherwise error: [MONGO--port=1021 connection]


Error:listen (): Bind () failed errno:13 Permission denied for socket:127.0.0.1:1021


Port = 27017
BIND_IP: Binding address. The default 127.0.0.1 can only be connected locally. Process bindings and listens for application connections from this address. If you need to connect to another server, you need to comment out this or change the IP address, such as 192.168.200.201[other servers with MONGO--host=192.168.200.201 connection], you can use a comma-separated list to bind multiple IP addresses.


BIND_IP = 127.0.0.1
Maxconns: Maximum number of connections. Default value: Depends on the system (that is, the Ulimit and file descriptor) restrictions. The MongoDB does not restrict its own connections. When the setting is greater than the system limit, it is invalid and is subject to system restrictions. This is useful for clients to create many "tables" that allow a connection to time out without closing the table. Set this value above the connection pool and the total number of connections to prevent spikes in connection. Note: This value is not set to be greater than 20000.


Maxconns = 100
Objcheck: Force authentication Client request. The default setting of 2.4 is Objcheck to true, and Objcheck defaults to False in earlier versions. Because it forces validation of client requests, make sure that the client never inserts invalid files into the database. There is a little performance impact for objects that are nested in documents. Set Noobjcheck off.


Objcheck = True
#noobjcheck = False
Noobjcheck: Ditto, turn off false by default.


#noobjcheck = False
LogPath: Specifies a log file that will hold all of the logging and diagnostic information. Unless otherwise specified, Mongod outputs all log information to the standard output. If Logappend is not specified, the log will overwrite the operation if it is restarted.


Logpath=/var/log/mongodb/mongodb.log
Logappend: Write Log mode: set to True for append. The default is overwrite. If this setting is not specified, MongoDB will overwrite the existing log file at startup.


Logappend=true
Syslog: Log output is sent to the host's syslog system, rather than standard output to the LogPath specified log file. Syslog and LogPath can not be used together, will be an error:


cant use both a logpath and syslog


Syslog = True
Pidfilepath: Process ID, not specified then there is no PID file at startup. Default defaults.


Pidfilepath =/var/run/mongo.pid
KeyFile: Specifies the path to the key file where authentication information is stored. Default defaults. See the details: "
Replica set security "and" Replica set administration. "


KeyFile =/srv/mongodb/keyfile
Nounixsocket: Socket file, default to False, generate socket file. When set to True, the socket file is not generated.


Nounixsocket = False
Unixsocketprefix: Socket file path, default/tmp


Unixsocketprefix =/tmp
Fork: Whether to run in the background, set to True to start daemon mode that the process runs in the background. Default false.


Fork = True
Auth: User authentication, default false. No certification required. When set to true, access to the database requires Auth authentication, when there is no user in the database, you do not need to verify or operate. Until the first user is created, the operation needs to be validated.


Auth = True
For example: Through Db.adduser (' sa ', ' sa ') under the Admin library to create a superuser, can only be authenticated under the Admin Library: Ab.auth (' sa ', ' sa ') in order to go to other library operations, can not be verified in other libraries. You also need to specify a library to connect to the database:


MONGO-USA-PSA Admin #sa Account Connection Admin

MONGO-UAA-PAA Test #aa Account Connection test
Noauth: Prohibit user authentication, default true. Ditto


Noauth = True
CPU: Setting to True forces MongoDB to report CPU utilization and Io waiting per 4s, writing log information to standard output or log files. The default is False.


CPU = True
Opening the log will appear:


Mon June 10:21:42.241 [Snapshotthread] cpu:elapsed:4000 writelock:0%
DBPath: Data storage directory. Default:/data/db/


Dbpath=/var/lib/mongodb
Diaglog: Create a very detailed troubleshooting and diagnostic logging of various errors. Default 0. Set to 1, to generate a diaglog in the DBPath directory. The log file that begins with his value is as follows:

Value Setting
0 off.       No logging. #关闭. No record.
1 Log write operations. #写操作
2 Log read operations. #读操作
3 Log Both read and write operations. #读写操作
7 Log Write and some read operations. #写和一些读操作
setting is not equal to 0, the log is flush once per minute:


Mon June 11:16:17.504 [Datafilesync] Flushing diag log
Mon June 11:17:17.442 [Datafilesync] Flushing diag log
The resulting log can be viewed with Mongosniff: If mongosniff[is similar to Tcpdump's specific TCP/IP network traffic as a MongoDB, there are errors and specific usage, see here, before you execute: Apt-get Install Libpcap-dev


root@m3:/var/lib/mongodb# Mongosniff--source Diaglog diaglog.51b542a9
Note: When reset to 0, the file is stopped, but Mongod continues to open the file even if it is no longer written to the data file. If you want to rename, move or delete diagnostics logs, you must completely close the Mongod instance.


Diaglog = 3
Directoryperdb: Set to True to modify the data directory Storage mode, where the files for each database are stored in a different folder in the DBPath specified directory. With this option, MongoDB can be configured to store data on different disk devices to improve write throughput or disk capacity. The default is False.
Note: In a database running for a period of time, opening the parameter will cause the original data to disappear (the annotation parameter will come back). Because the data directories are different, unless you migrate existing data files to the database directory generated by DIRECTORYPERDB, such as:
root@m3:/var/lib/mongodb# MV test.* test/
Migrate the test data file to the database test directory generated by Directoryperdb. So you need to decide whether to turn it on after planning.


Directoryperdb = ture

Original data structure:
Journal
Mongod.lock
local.0
Local.1
Local.ns
test.0
Test.1
Test.ns

The structure that opens the DIRECTORYPERDB and migrates the data files to the relevant data directory:

Journal
Mongod.lock
local/local.0
Local/local.1
Local/local.ns
test/test.0
Test/test.1
Test/test.ns
Journal: Log,
Default value: (on 64-bit system) true.
Default value: (32-bit system) false.
Set to True to enable the action log to ensure write persistence and data consistency, creating the journal directory under the DBPath directory.
Set to False to prevent log persistence, overhead is not required. To reduce the impact of logs used on disk, you can enable nojournal and set to true.
Note: Disabling the log on a 64-bit system must use a nojournal.


#journal =true
Journal=false
32-bit OS:


Tue June 12:17:09.628 [Initandlisten] * * Note:this is a bit MongoDB binary.
Tue June 12:17:09.628 [Initandlisten] * * Bit builds are limited to less than 2GB of data (or less with--journa L).
64-bit OS:


Tue June 12:29:34 [Initandlisten] Journal Dir=/var/lib/mongodb/journal
Tue June 12:29:34 [Initandlisten] recover:no journal files present, no recovery needed
Nojournal: Prohibit log
Default value: (on 64-bit system) false.
Default value: (32-bit system) true.
Set Nojournal to true turn off the log, 64-bit, 2.0-version MongoDB the default is to enable journal logging.


Nojournal=true
Journalcommitinterval: Brush Write submission mechanism, the default is 30ms or 100ms. Lower values, which will consume more disk performance.
This option accepts values between 2 and 300 milliseconds:
If a single piece of equipment provides logs and data files, the default journal submission interval is 100 milliseconds.
If different block devices provide log and data files, the default journal submission interval is 30 milliseconds.


journalcommitinterval=100
IPv6: IPv6 is supported, default false.
JSONP: Allow JSONP access via an HTTP interface, default false.
Nohttpinterface: Whether the HTTP interface, or 28017-port-enabled service, is prohibited. Default false, supported.


Nohttpinterface = False
Noprealloc: Pre-allocation method.
Default false: Use the pre-allocation method to ensure write performance is stable, pre-allocation is done in the background, and each of the preconfigured files is populated with 0. This allows MongoDB to always maintain extra space and free data files, thus avoiding the blocking of allocating disk space caused by the data growing too fast.
Setting noprealloc= True to disable pre-allocated data files shortens startup time, but can cause significant performance degradation during normal operation.


Noprealloc = False
Noscripting: Whether the scripting engine is blocked. Default is false: not prohibited. Ture: Forbidden
If set to true: Some scripts will appear when running:


JavaScript Execution Failed:group command failed: {"OK": 0, "errmsg": "Server-side JavaScript execution is disabled"}


#noscripting = True <====> Noscripting = False
Notablescan: Whether table scan operation is prohibited. Default false: Not prohibited, ture: prohibited
Prohibit if a table scan appears:


Error: {"$err": "Table scans not allowed:test.emp", "Code": 10111}
You can modify settings dynamically:


Db.admincommand ({setparameter:1, notablescan:false})


#notablescan = True <====> Notablescan = False
Nssize: The default size of the file (that is, NS) for the namespace, default 16M, Max 2G.
The file for all newly created default size namespaces (that is, NS). This option does not affect the size of the file in the existing namespace. The default value is 16M bytes, with a maximum size of 2 GB. Let the small database not waste too much disk space, while allowing large data to have contiguous space on the disk.

-RWXRWXRWX 1 MongoDB zhoujy 16M June 14:44 test.0
-RWXRWXRWX 1 MongoDB zhoujy 32M June 1 21:36 test.1
-RWXRWXRWX 1 MongoDB zhoujy 16M June 14:44 test.ns
Drwxr-xr-x 2 root 4.0K June 11:57 _tmp


Nssize = 16
Profile: Database analysis level setting. Record some operational performance to the standard output or the specified LogPath log file, default 0: Off.


Level setting
0 off. No analysis.
1 Open. Includes only slow operations.
2 Open. Include all operations.
Control Profiling switches and levels: 2 kinds
The first is to set directly in the startup parameters or to start MongoDB with the –profile= level, and the information is stored in the generated system.profile.


Profile = 2
The second is to configure the client with the Db.setprofilinglevel (level) command, whose information is stored in the generated system.profile.


[Initandlisten] Creating profile Collection:local.system.profile


> Db.setprofilinglevel (2)
{"was": 0, "slowms": +, "OK": 1}
> Db.getprofilingstatus ()
{"was": 2, "SLOWMS": 100}
By default, Mongod is disabled for analysis. Database analysis can affect the performance of the database because the parser must record and process all database operations. So you can use dynamic modification when you need it.
SLOWMS: The time to record a slow query for profile analysis, which defaults to 100 milliseconds. Specific ibid.


SLOWMS = 200


> Db.getprofilingstatus ()
{"was": 2, "SLOWMS": 200}
Quota: Quota, default false. Whether to turn on the limit for configuring the maximum number of files per database. True to configure the maximum number of files with Quotafiles.


Quota = True
Quotafiles: Quota quantity. The limit of the number of data files per database. This option requires quota to be true. The default is 8.


Quotafiles = 8
Rest: Default False, set to true to make a simple REST API.


Rest = True
Set to True, after opening, the port in MongoDB will open an HTTP protocol to provide rest services (Nohttpinterface = False), which is your server port plus 1000, or 28017, The default HTTP port is the Database status page (the commands in the commands line of the Web page can be lit after it is turned on). MongoDB's own rest, does not support the addition, deletion, change, but also does not support authorization.
Repair: Repair Database operation, default is False.
When set to True, repair all databases after startup, and setting this option is best on the command line, not in the configuration file or control script. Such as:
Command line repair:

> Db.repairdatabase (' xxx ')
{"OK": 1}
> Db.repairdatabase ()
{"OK": 1}
Repair at startup:


Repair = True


root@m3:/var/log/mongodb# Mongod--repair
Repair at startup, need to turn off journal, or error:


Can ' t specify both--journal and--repair options.
And at startup, after you fix it with the parameters specified in the control file and the parameters specified in the configuration file (the repair information is in log), you need to disable the repair parameter to enable MongoDB.
Note: When Mongod is repaired, all database files need to be rewritten. If you cannot run a repair under the same account, you will need to run Chown to modify the database file permissions.
Repairpath: Fixes the path, by default is the _tmp directory under the DBPath path.


Drwxr-xr-x 2 root 4.0K June 20:23 _tmp
Smallfiles: Whether to use a smaller default file. Default is False, not used.
Set to true to use a smaller default data file size. Smallfiles reduces the initial size of the data files and restricts them to 512M, also reduces the size of the log files and restricts them to 128M.
If the database is large and holds a small amount of data, it can cause MongoDB to create many files that can affect performance.


Smallfiles = True
Syncdelay: The frequency of the brush writes data to the log, through the Fsync operation data. Default 60 seconds.

Syncdelay = 60
By default, you do not need to set it. Does not affect the log files (journal files)
Warning: A memory-mapped file that is not synchronized to the disk if set to 0,syncdelay. On the production system, do not set this value.
SysInfo: System Information, default false.
Set to True,mongod will diagnose the system-related page size, number of physical pages, and the number of physical pages available to the standard output.

Tue June 21:07:15.031 SysInfo:
Tue June 21:07:15.035 Page size:4096
Tue June 21:07:15.035 _sc_phys_pages:256318
Tue June 21:07:15.035 _sc_avphys_pages:19895
When the SysInfo parameter is turned on, only the information above will be printed and the MONGODB program will not be started. So to turn off this argument, you can open MongoDB.
Upgrade: Upgrade. The default is False.
When set to True, specifies dbpath to upgrade the data format files on the disk to the latest version. Can affect database operations and update metadata. In most cases, you do not need to set this value.
Traceexceptions: Whether to use internal diagnostics. Default false.


Traceexceptions = False
Quiet: Quiet mode.


Quiet = True
setparameter:2.4 A new parameter that specifies the startup option configuration. To set multiple options, specify with a setparameter option, Setparameter set in this file, using the following format:


Setparameter = <parameter>=<value>
As set syncdelay in the configuration file:


Setparameter = syncdelay= 55,notablescan = True,journalcommitinterval = 50,traceexceptions = True
Replication Options Replication option
Replset: Use this setting to configure the replica set. Specifies a replica set name as an argument, and all hosts must have the same name as the same replica set.
Oplogsize: The maximum size of the specified replication action log (Oplog). Mongod creates a oplog size based on the maximum amount of free space. For 64-bit systems, Oplog is usually 5% of the available disk space.
Once Mongod first created the oplog, changing the oplogsize will not affect the size of the oplog.
Fastsync: Default is False. Under the replica set, set to True, enable the library Replication Service from a dbpath, the DBPath database is a snapshot of the master library that can be used to quickly enable synchronization, otherwise the Mongod will attempt to perform an initial synchronization. Note: If the data is not fully synchronized, Mongod specifies that Fastsync is turned on, secondary or slave is permanently out of sync with the master, which can lead to significant consistency issues.
The replindexprefetch:2.2 version appears with the new parameter, which defaults to all. The values that can be set are: all, none, and _id_only. Can only be used in the replica set (Replset). By default, members of the secondary replica set will load all indexes into memory that are related to the operation prior to Oplog. You can modify this behavior so that secondary only loads the _id index. Specifies _id_ or none to prevent any index of mongod from being loaded into memory.
Master/slave Replication: Related settings for master-slave replication
Master: Default to False, and when set to True, configure the current instance as the primary instance.


Master = True
Slave: The default is False, and when set to true, the current instance is configured as a from instance.


Slave = True
Source: Default is NULL, the format is:


Source = 127.0.0.1:30001
Only: null by default, used to specify a database for replication from the option.

only = ABC #只同步abc集合 (library)
Slavedelay: Sets the delay time for synchronizing the main library from the library, which defaults to 0 from the settings.


Slavedelay = #延迟60s同步主数据
Autoresync: Default to False, used from settings. Whether to automatically resynchronize. Set to TRUE if the trailing main exceeds 10 seconds, the automatic resynchronization is forced. This setting may be problematic if the oplogsize is too small. Forced resynchronization is not necessary if the oplog size is not sufficient to store the difference between the main change state and the change in state. When the Set Autoresync option is set to false,10 minutes there will be no automatic resynchronization greater than 1 times.

Autoresync = False

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.