MongoDB Installation and simple configuration

Source: Internet
Author: User
Tags auth create mongodb install mongodb iptables

1. Upload the downloaded installation package to the lniux server or curl -O
2.tar -zxvf mongodb-linux-x86_64-3.0.6.tgz mongodb-linux-x86_64-3.0.6 / / usr / local / mongodb # Copy the unzipped package to the specified directory
4. Create a corresponding folder in the specified directory to store data and log files of mongoDB, for example: create in the / usr / local / mongodb directory
/ usr / local / mongodb / data, used to store data, create / usr / local / mongodb / logs to store logs,
5.Execute / usr / local / mongodb / bin / mongod --port 27017 --fork --dbpath = / usr / local / mongodb / data / --logpath = / usr / local / mongodb / logs / mongodb.log- -logappend
The following text appears as a command success.
about to fork child process, waiting until server is ready for connections.
forked process: 8533
You can execute: ps aux | grep mongodb or pstree -p | grep mongod
Or execute netstat -lanp | grep "27017"
If this happens
 tcp 0 0* LISTEN 8533 / mongod unix 2
[ACC] STREAM LISTENING 75204 8533 / mongod /tmp/mongodb-27017.sock
You can also view the log file in logs. MongoDB shell version: 3.0.6 connecting to: test appears after executing ./mongo into the console
6. Stop the mongodb command db.shutdownServer () This command can be used only after using the admin user
7. ./mongod --dbpath = / usr / local / mongodb / data / db --rest command You can enter http: // localhost: 28017 in the browser on the virtual machine to display the interface service.

Explanation of startup parameters

Description of the common parameter options of the startup command:

--dbpath Specify the directory of the database

--port specifies the port of the database, the default is 27017

--bind_ip bind IP

--logpath Specify the log storage directory

--logappend Specify log generation method (append / overwrite)

--pidfilepath: Specify the path to the process file. If not specified, the process file will not be generated.

--keyFile Private key for cluster mode authorization verification

--journal enable logging

--nssize: Specify the size of the .ns file in MB. The default is 16M and the maximum is 2G.

--maxConns The maximum number of connections is not

--notablescan table scan is not allowed

--noprealloc Turn off the pre-allocation of data files

--fork Run the service as a background Daemon

For more parameter options, use mongod --help to view
War Displayed in the log when starting up 4Waring:

** WARNING: Readahead for / usr / local / mongodb is set to 4096KB
We suggest setting it to 256KB (512 sectors) or less

** WARNING: You are running this process as the root user, which is not recommended.

** WARNING: / sys / kernel / mm / transparent_hugepage / enabled is ‘always’.
We suggest setting it to ‘never’

** WARNING: / sys / kernel / mm / transparent_hugepage / defrag is ‘always’.
We suggest setting it to ‘never’

① Refer to the official website tips:

Ensure that the read-ahead settings for the block device used to store the data files are appropriate. For random access mode, set a low read-ahead value. A read-ahead value of 32 (16kb) usually works well.

For standard block devices, you can run sudo blockdev --report to get the read-ahead value setting, and run sudo blockdev --setra <value> <value> to change the read-ahead value setting.

Refer to your specific operating system manual for more information.


In fact, after running blockdev --report, a few lines of information are displayed, excluding non-4096, but this way it is impossible to determine which block device is used to store the data file.

Use df -h to view the file system information. You can see that the largest capacity is our hard disk. Use it to store data files.


Run blockdev --setra 256 your drive letter name (eg: / dev / sda2).


② The second one is completely negligible.

③④ Follow the recommended settings and refer to the setting methods:

Here I directly add the following to the end of rc.local, boot settings and start automatically:
If the current manual change, verify whether [always] madvise never becomes always madvise [never]

cat / sys / kernel / mm / transparent_hugepage / enabled
Specific operation:

First, close SElinux, configure the firewall / etc / selinux / config

# SELINUX = enforcing #comment out

# SELINUXTYPE = targeted #Commented out

SELINUX = disabled #Increase

: wq! #Save and exit

setenforce 0 #Make the configuration take effect immediately / etc / sysconfig / iptables #edit

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT #Allow port 27017 to pass through the firewall

: wq! #Save and exit

/etc/init.d/iptables restart #Restart the firewall for the configuration to take effect

Install MongoDB

MongoDB: #Current version

Upload mongodb-linux-x86_64-3.0.6.tgz to the / usr / local / src directory

cd / usr / local / src #Enter the software storage directory

tar zxvf mongodb-linux-x86_64-3.0.6.tgz #decompress

mv mongodb-linux-x86_64-3.0.6 / usr / local / mongodb #Move the unzipped folder to the MongoDB installation directory

mkdir -p / data / mongodb / mongodb_data / #Create MongoDB database storage path

mkdir -p / data / mongodb / mongodb_log / #Create MongoDB database log storage path

#Start MongoDB

/ usr / local / mongodb / bin / mongod --port 27017 --fork --dbpath = / data / mongodb / mongodb_data / --logpath = / data / mongodb / mongodb_log / mongodb.log --logappend

netstat -lanp | grep "27017" #Check if MongoDB is started

cd / usr / local / mongodb / bin /

./mongo #Enter the MongoDB database console

use admin #Enter the admin database

db.shutdownServer () #Shut down the MongoDB database

exit #Exit

Setting up the MongoDB database / usr / local / mongodb / #Enter the MongoDB installation directory

vi /usr/local/mongodb/mongodb.conf #edit

port = 27017 #Port number

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

logpath = / data / mongodb / mongodb_log / mongodb.log #log output file path

pidfilepath = / usr / local / mongodb /

fork = true #Set the background to run

logappend = true #Log output mode

shardsvr = true

# auth = true #Enable authentication

: wq! #Save and exit

Cd / usr / local / mongodb / bin /

/ usr / local / mongodb / bin / mongod --config /usr/local/mongodb/mongodb.conf #Start MongoDB

/ usr / local / mongodb / bin / mongo --eval "db.shutdownServer ()" #Shut down MongoDB

vi /etc/rc.d/init.d/mongod #Set the startup and start MongoDB

ulimit -SHn 655350

#! / bin / sh

# chkconfig:-64 36

# description: mongod

case $ 1 in


/ usr / local / mongodb / bin / mongod --maxConns 20000 --config /usr/local/mongodb/mongodb.conf



/ usr / local / mongodb / bin / mongo --eval "db.shutdownServer ()"



/ usr / local / mongodb / bin / mongo --eval "db.stats ()"



: wq! #Save and exit

chmod + x /etc/rc.d/init.d/mongod #Add script execution permissions

chkconfig mongod on #Set boot

service mongod start #Start MongoDB

Note: If an administrator account password is set, use the following command

Account: root

Password: 123456

/ usr / local / mongodb / bin / mongo --eval "db.auth (‘ root ‘,‘ 123456 ‘); db.shutdownServer ()"

/ usr / l
ocal / mongodb / bin / mongo --eval "db.auth (‘ root ‘,‘ 123456 ‘); db.stats ()"

3, vi / etc / profile #Add environment variables, edit, add the following code in the last line

export PATH = $ PATH: / usr / local / mongodb / bin

: wq! #Save and exit

source / etc / profile #Make the configuration take effect immediately

mongo #Enter the MongoDB console

show dbs #View the default database

use admin #Switch to the admin database

exit #Exit the MongoDB console

At this point, the MongoDB database installation and configuration are complete.

mongodb installation and simple configuration

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: 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.