Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Source code installation mongoDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Installation steps:

1. Prepare for

1.1 display system version

[root@centos ~] # cat / etc/redhat-release

CentOS Linux release 6.4.1406 (Core)

1.2 install basic software packages (basic compilation tools, preferably with all developed package groups)

[root@centos ~] # yum install vim wget lsof gcc gcc-c++ bzip2-y

[root@centos ~] # yum install net-tools bind-utils-y

1.3 display IP address

[root@centos ~] # ifconfig | grep inet

Inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255

two。 Compile and install mongodb

2.1 download package

[root@centos ~] # cd / usr/local/src/

[root@centos ~] # wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.6.tgz

[root@centos] # tar-zvxf mongodb-linux-x86_64-2.6.6.tgz

[root@centos] # mv mongodb-linux-x86_64-2.6.6 / opt/mongodb/

2.2 configure the path environment variable to ensure that the bin directory of mongodb is included in the path environment variable.

[root@centos ~] # vim / etc/profile

Find export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL and add the following to the line:

# set for mongodb

Export MONGODB_HOME=/opt/mongodb

Export PATH=$MONGODB_HOME/bin:$PATH

Save exit

[root@centos ~] # echo $PATH

[root@centos ~] # source / etc/profile

[root@centos ~] # echo $PATH

[root@centos ~] # mongod-version

If the following is displayed, the installation is successful

Db version v2.6.6

2014-12-18T11:02:15.100+0800 git version: 608e8bc319627693b04cc7da29ecc300a5f45a1f

2.3 establish a directory to store data and logs:

[root@centos] # mkdir-p / data/mongodb/journal

[root@centos] # mkdir-p / data/mongodb/log

[root@centos ~] # touch / data/mongodb/log/mongodb.log

2.4 add mongodb users and set permissions

[root@centos] # useradd mongodb-M-s / sbin/nologin

[root@centos] # chown-R mongodb.mongodb / data/mongodb

2.5 create a profile

[root@centos ~] # vim / etc/mongodb.conf

Enter the following

Dbpath=/data/mongodb

Logpath=/data/mongodb/log/mongodb.log

Logappend=true

Port=27017

Fork=true

Noauth=true

Nojournal = true

Smallfiles = true

Noprealloc = true

Save, exit

# *

# parameters of mongodb:

#

#-dbpath database path (data file)

#-logpath log file path

#-master designated as the master machine

#-slave specified as slave machine

#-source specifies the IP address of the host machine

#-pologSize specifies that the log file size does not exceed 64m. Because resync is very heavy and time-consuming.

# it is best to avoid resync by setting an oplogSize large enough (the default oplog size is 5% of the free disk size).

#-add at the end of logappend log file

#-port enable port number

#-fork runs in the background

#-only specifies which database is replicated only

#-slavedelay refers to the time interval detected from replication

#-whether auth needs to verify permission to log in (username and password)

Note: there are many parameters in the mongodb configuration file. To customize specific requirements, please refer to the official documentation.

2.6.Add the mongod service to the boot service (this is needed in Centos7 to add to the system service)

[root@centos ~] # vim / lib/systemd/system/mongodb.service

Enter the following

[Unit]

Description=mongodb

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

PIDFile=/data/mongodb/mongod.lock

ExecStart=/opt/mongodb/bin/mongod-f / etc/mongodb.conf

ExecReload=/bin/kill-s HUP $MAINPID

ExecStop=/bin/kill-s QUIT $MAINPID

PrivateTmp=true

[Install]

WantedBy=multi-user.target

Save, exit

[root@centos ~] # systemctl enable mongodb.service

[root@centos ~] # systemctl list-unit-files | grep enabled | grep mongodb

[root@centos ~] # systemctl daemon-reload

[root@centos ~] # systemctl start mongodb.service

[root@centos ~] # systemctl status mongodb.service-l

2.7 add a service script to Centos6 as follows

The preparations are as follows, the links to be used in the script

Mkdir-p / usr/local/mongodb/srv

Vim / usr/local/mongo/srv/mongodb-start

Add the following

#! / bin/sh

Mongod-f / usr/local/mongodb/mongodb.conf

Vim / usr/local/mongo/srv/mongodb-stop

Add the following

#! / bin/bash

Pid= `ps-o pid,command ax | grep mongod | awk'! / awk/ & &! / grep/ {print $1}'`

If ["${pid}"! = "]; then

Kill-2 ${pid}

Fi

Add execution permission

Chmod axix / usr/local/mongo/srv/mongodb-start

Chmod axix / usr/local/mongo/srv/mongodb-stop

# script file is as follows:

#! / bin/sh

#

# mongodb-this script starts and stops the mongodb daemon

#

# chkconfig:-85 15

# description: MongoDB is a non-relational database storage system.

# processname: mongodb

# config: / usr/local/mongodb/mongodb.conf

# pidfile: / usr/local/mongodb/mongodb.pid

PATH=/usr/local/mongo/bin:/sbin:/bin:/usr/sbin:/usr/bin

NAME=mongodb

Test-x $DAEMON | | exit 0

Set-e

Case "$1" in

Start)

Echo-n "Starting MongoDB..."

/ usr/local/mongo/srv/mongodb-start

Stop)

Echo-n "Stopping MongoDB..."

/ usr/local/mongo/srv/mongodb-stop

*)

N=/etc/init.d/$NAME

Echo "Usage: $N {start | stop}" > & 2

Exit 1

Esac

Exit 0

You can join the system service and start up automatically.

Chmod axix / etc/init.d/mongodb

Chkconfig-add mongodb

Chkconfig-level 345 mongodb on

/ etc/init.d/mongodb start

3 Test whether the database is normal or not

[root@centos ~] # ps-ef | grep mongod

[root@centos ~] # mongo admin

Add the admin username and password and log in to MongoDB with the user you created:

> show dbs

> use admin

> db.addUser ('admin','manager')

> db.auth ('admin','manager')

> show collections

> db.system.users.find ()

> exit

4 add port 27017 to the firewall (the method of opening the firewall in Centos7)

[root@centos ~] # iptables-L | grep ACCEPT

[root@centos] # firewall-cmd-zone=public-add-port=27017/tcp-permanent

[root@centos] # firewall-cmd-- reload

[root@centos ~] # iptables-L | grep ACCEPT

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.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report