Linux下MongoDB简介及部署使用

随着互联网web2.0兴起,传统的SQL已经不能满足海量数据查询所带来的缺点和不足,所以人们为了克服相应的问题而出现了NoSQL,而NoSQL中有四个分类,它们分别是:键值数据库(hash)、列式存储数据库、文档型数据库、图形数据库。而其中文档型数据库中最为典型的就是Mongodb,它是由C++编写的,用于为WEB应用提供可扩展的高性能数据存储解决方案,它的特点是高性能、易部署、易使用,存储数据非常方便,它支持动态查询、完全索引、自动分片、文件是使用Bson的格式等,其中使用的语法类似于Json的语法,可适用于网站数据、缓存、大尺寸、低价值的数据、用于对象及JSON 数据的存储等,不足是不能提供的对SQL的支持,支持的特性不够丰富,现在的产品相比关系型数据库还不够成熟,不过由于其良好的性能已经被大量使用与生产环境中。

具体的其他特性在这里就不做过多的赘述了,现在说说Linux下Mongodb简单的部署,在这里为了快速部署使用就使用了官网的最新二进制包去快速部署,当然如果要用的不是最新的可以用官网上的rpm包,Mongodb的官网:https://www.mongodb.com

[root@localhost src]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.2.tgz
[root@localhost src]# tar zxvf mongodb-linux-x86_64-rhel62-3.4.2.tgz -C /usr/local/mongodb/
[root@localhost src]# cd /usr/local/mongodb/mongodb-linux-x86_64-rhel62-3.4.2
[root@localhost src]# mv ./* ../
[root@localhost mongodb]# rm -rf mongodb-linux-x86_64-rhel62-3.4.2
[root@localhost mongodb]# groupadd -r mongodb
[root@localhost mongodb]# useradd -M -g mongodb -r -s /sbin/nologin -d /data/mongodata mongodb
#设置mongodb用户的家目录为Mongodb的dbpath路径
[root@localhost mongodb]# chown -R mongodb:mongodb /usr/local/mongodb
#修改Mongodb的basedir文件的属组
[root@localhost mongodb]# mkdir /var/log/mongodb
[root@localhost mongodb]# chown mongodb:mongodb /var/log/mongodb
#创建Mongodb的log路径及修改属组
[root@localhost mongodb]# mkdir /var/run/mongodb/
[root@localhost mongodb]# chown mongodb:mongodb /var/run/mongodb
#创建Mongodb的pid文件路径及修改属组
[root@localhost mongodb]# vim /etc/profile.d/mongo.sh
export PATH=$PATH:/usr/local/mongodb/bin
[root@localhost mongodb]# source /etc/profile.d/mongo.sh
#添加Mongodb的二进制环境路径
#最后的mongod的启动脚本和mongodb.conf配置文件可以从rpm包安装的服务器上下载,修改一下,在这里有一点
#有一个Mongodb默认读取配置文件的配置文件实际是调用mongod执行的:
OPTIONS="--quiet -f /etc/mongodb.conf"
[root@localhost mongodb]# vim /etc/sysconfig/mongod#如果没有需要添加
#当然也可以修改mongod的启动脚本直接添加,下文会提及

Mongodb的启动脚本中需要修改相应的环境路径,启动脚本如下:

#!/bin/sh
#
# mongodb      init file for starting up the MongoDB server
#
# chkconfig:  - 90 10
# description: Starts and stops the MongDB daemon that handles all \
#              database requests.
 
# Source function library.
. /etc/rc.d/init.d/functions
 
exec="/usr/local/mongodb/bin/mongod"
prog="mongod"
logfile="/var/log/mongodb/mongodb.log"
 
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
pidfile=${PIDFILE-/var/run/mongodb/mongodb.pid}
options="$MONGODB_OPTIONS $OPTIONS"
lockfile="/var/lock/subsys/mongod"
#要修改的就是相应的环境路径
# Nicer version of killproc that does not kill mongodb when it takes
# a long time to shut down and does not hang for a long time when mongo
# shuts down quickly
killproc_nice() {
        local RC base pid pid_file= delay i
 
        RC=0; delay=3
        # Test syntax.
        if [ "$#" -eq 0 ]; then
                echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
                return 1
        fi
        if [ "$1" = "-p" ]; then
                pid_file=$2
                shift 2
        fi
        if [ "$1" = "-d" ]; then
                delay=$2
                shift 2
        fi
 
        # Save basename.
        base=${1##*/}
 
        # Find pid.
        __pids_var_run "$1" "$pid_file"
        RC=$?
        if [ -z "$pid" ]; then
                if [ -z "$pid_file" ]; then
                        pid="$(__pids_pidof "$1")"
                else
                        [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;}
                fi
        fi
 
        # Kill it.
        if [ -n "$pid" ] ; then
                [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
                if checkpid $pid 2>&1; then
                        # TERM first, then KILL if not dead
                        kill -TERM $pid >/dev/null 2>&1
                        usleep 100000
 
                        # Check every one second if the program is stopped.
                        # Do so for a maximum of $delay seconds
                        for ((i = 0 ; i < $delay; i++))
                        do
                                if checkpid $pid; then
                                        sleep 1
                                else
                                        break
                                fi
                        done
 
                        # If the program is not stopped, kill it
                        if checkpid $pid ; then
                                kill -KILL $pid >/dev/null 2>&1
                                usleep 100000
                        fi
                fi
                checkpid $pid
                RC=$?
                [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
                RC=$((! $RC))
        else
                failure $"$base shutdown"
                RC=0
        fi
 
        # Remove pid file if any.
        rm -f "${pid_file:-/var/run/$base.pid}"
        return $RC
}
 
start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $prog: "
    daemon --pidfile=${pidfile} --user mongodb "$exec $options run >> $logfile 2>&1"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc_nice -p ${pidfile} -d 300 $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    stop
    start
}
 
reload() {
    restart
}
 
force_reload() {
    restart
}
 
rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p ${pidfile} $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

Mongodb的配置文件mongodb.conf,如下:

##
### Basic Defaults
##
bind_ip = 127.0.0.1
port = 27017
fork = true
pidfilepath = /var/run/mongodb/mongodb.pid
logpath = /var/log/mongodb/mongodb.log
dbpath =/data/mongodata
journal = true
 
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
 
# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true
 
# Verbose logging output.
#verbose = true
 
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true
 
# Enable db quota management
#quota = true
 
# Set oplogging level where n is
#  0=off (default)
#  1=W
#  2=R
#  3=both
#  7=W+some reads
#oplog = 0
 
# Diagnostic/debugging option
#nocursors = true
 
# Ignore query hints
#nohints = true
 
# Disable the HTTP interface (Defaults to port+1000).
nohttpinterface = true
 
# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting = true
 
# Turns off table scans.  Any query that would do a table scan fails.
#notablescan = true
 
# Disable data file preallocation.
#noprealloc = true
 
# Specify .ns file size for new databases.
# nssize = <size>
 
# Accout token for Mongo monitoring server.
#mms-token = <token>
 
# Server name for Mongo monitoring server.
#mms-name = <server-name>
 
# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>
 
# Replication Options
 
# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com
 
# Address of a server to pair with.
#pairwith = <server:port>
# Address of arbiter server.
#arbiter = <server:port>
# Automatically resync if slave data is stale
#autoresync
# Custom size for replication operation log.
#oplogSize = <MB>
# Size limit for in-memory storage of op ids.
#opIdMem = <bytes>

配置好后第一次启动后会生成相应的数据文件

[root@localhost mongodb]# /etc/init.d/mongod start
正在启动 mongod:[确定]
[root@localhost mongodb]# mongo#Mongodb客户端启动
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2
Server has startup warnings: 
2017-02-15T10:42:11.888+0800 I STORAGE  [initandlisten] 
2017-02-15T10:42:11.888+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-02-15T10:42:11.888+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] 
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] 
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] 
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] 
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-02-15T10:42:12.850+0800 I CONTROL  [initandlisten] 
> show dbsshow dbs
admin  0.000GB
local  0.000GB
#有2个默认库

如此Mongodb的初步介绍使用结束。

更多MongoDB相关教程见以下内容

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

相关推荐