Installing and setting up MongoDB on Linux
Switch to root access by running the following command:
sudo suDownload the Mongo database as follows:
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.1.tgz
Extract the MongoDB archive by running the following command:
tar -zxvf mongodb-linux-x86_64-3.2.1.tgzCopy MongoDB to the target directory by running the following commands:
mkdir -p /dbdata/mongodb
cp -R -n mongodb-linux-x86_64-3.2.1/ /dbdata/mongodbCreate the database data directory by running the following command:
mkdir -p /dbdata/mongodatawtCreate a key file that your deployment will use to authenticate servers to each other. Ensure that your key file meets the following requirements:
- Contains 6 to 1,024 characters and contains only characters in the base64 set
Does not have group or world permissions on UNIX system
To set read and write permissions for the file ownerchmod 600 <KeyFile>
Use either of the following example Linux commands:
openssl rand -base64 741 > mongo.key
chmod 600 mongo.keyor
tr -cd '[:alnum:]' < /dev/urandom | fold -w50 | head -n1 > "mongo.key"(Optional) Set the storage engine to wiredTiger for better performance and reduced storage size by running the following command:
/dbdata/mongodb/mongodb-linux-x86_64-3.0.6/bin/mongod --dbpath /dbdata/mongodatawt --port 27017 --storageEngine wiredTigerConnect to the mongo shell, and create the administrator user and the social database administrator as follows:
use admin
db.createUser( {
user: "siteUserAdmin",
pwd: "<siteUserPassword>",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
});
db.createUser( {
user: "siteRootAdmin",
pwd: "<siteRootAdminPassword>",
roles: [ { role: "root", db: "admin" } ]
});
use social
db.createUser( {
user: "social_admin",
pwd: "<social_adminPassword>",
roles:
[ { role: "dbOwner", db: "social" } ]
});- Stop the Mongo process.
Add MongoDB as a service with the key file parameter.
On the Red Hat Enterprise Linux (including CENT OS), create the /etc/mongod.conf file and add the following settings:
dbpath = /dbdata/mongodatawt
port = 27017
logpath = /dbdata/mongodb/mongo_social.log
logappend = true
quiet = true
journal = true
replSet = myitsocial
fork = true
keyFile = /dbdata/mongodb/mongo.key
nohttpinterface = true
storageEngine = wiredTigerCreate the /etc/init.d/mongorepl file with the appropriate permissions and the following content:
#!/bin/sh
#
# /etc/init.d/mongorepl
# Subsystem file for "MyIT-MongoRepl" server
#
# chkconfig: 2345 95 05 (1)
# description: MyIT-MongoRepl server daemon
#MONGOBIN -> point to mongod binary.
MONGOBIN="/dbdata/mongodb/mongodb-linux-x86_64-3.2.1/bin/mongod"
do_start()
{
echo "starting!";
eval "$MONGOBIN -f /etc/mongod.conf";
}
do_stop()
{
echo "stopping!"
eval "$MONGOBIN -f /etc/mongod.conf --shutdown";
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
esac
exit 0Use the Redhat chkconfig Utility to start the service automatically:
To add mongorepl to chkconfig, and enable in runlevel, run:
chkconfig --add mongorepl
chkconfig mongorepl on
To start or stop the mongorepl service:
service mongorepl start
service mongorepl stop
- (For highavailability) After installing and setting up MongoDB on Linux, go to Configuring-MongoDB-for-replica-set.