Centos 7.5 installation configuration MongoDB 4.0.4

MongoDB is a database based on distributed file storage. Written in C + +. It aims to provide scalable high-performance data storage solutions for WEB applications. MongoDB is a product between relational database and non relational database. It has the most abundant functions and is the most like relational database.
1. Download and unzip mongodb

cd /data/
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.4.tgz
tar zxvf mongodb-linux-x86_64-4.0.4.tgz

2. Create mongodb related directories

mv mongodb-linux-x86_64-4.0.4 mongodb
mkdir -p mongodb/{data/db,log}
mkdir -p /etc/mongodb

3. Create mongodb configuration file
vim /etc/mongodb/mgdb.conf

dbpath=/data/mongodb/data/db  #Data file storage directory
logpath=/data/mongodb/log/mongodb.log  #Log file storage directory
port=37485  #Port, 27017 by default, can be customized
logappend=true  #Open log append add log
fork=true  #Enabled as a daemons, running in the background
bind_ip=0.0.0.0  #Local monitoring IP, 0.0.0.0 means all local IP
auth=true  #Whether authentication permission login (user name and password) is required

4. Add environment variables
vim /etc/profile

export MONGODB_HOME=/data/mongodb
export PATH=$PATH:$MONGODB_HOME/bin

Make environment variables effective immediately
source /etc/profile
5. Create mongodb startup configuration file
vim /usr/lib/systemd/system/mongodb.service

[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
RuntimeDirectory=mongodb
PIDFile=/data/mongodb/data/db/mongod.lock
ExecStart=/data/mongodb/bin/mongod --config /etc/mongodb/mgdb.conf
ExecStop=/data/mongodb/bin/mongod --shutdown --config /etc/mongodb/mgdb.conf
PrivateTmp=true

[Install]  
WantedBy=multi-user.target

6. Start mongodb and add it to startup

systemctl daemon-reload
systemctl start mongodb
systemctl enable mongodb

7. Configure firewall policy

firewall-cmd --permanent --add-port=37485/tcp
firewall-cmd --reload

8, test
(1) create management user

mongo --port 37485
> use admin
> db.createUser({user:"admin",pwd:"xuad830818",roles:[{role:"userAdminAnyDatabase",db: "admin"}]})
> db.auth('admin','xuad830818')

(2) create test user

> use test
> db.createUser({user:"xuad",pwd:"123456",roles:[{role:"readWrite",db:"securitydata"}]})
> db.auth('xuad','123456')
> exit


(3) log in with test user

mongo --port 37485 -u xuad -p 123456

Keywords: Database MongoDB Linux firewall

Added by labmixz on Tue, 10 Dec 2019 02:48:02 +0200