CentOS 6.9 install mongodb and PHP mongodb extensions

Keywords: PHP MongoDB Linux vim

Notes:
CentOS6.9yum install mongodb and PHP mongodb extensions
1. Installation dependency

yum install libcurl openssl

2. Download and unzip

wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz
tar -zxvf mongodb-linux-x86_64-4.0.5.tgz

3. Mobile mongodb

mkdir -p /usr/local/mongodb
mv mongodb-linux-x86_64-4.0.5/* /usr/local/mongodb

4. Add environment variables

vim /etc/profile
#Add the following code to the end of the file
PATH=$PATH:/usr/local/mongodb/bin
export PATH
#Effective immediately
source /etc/profile
#View version
[root@jmsite ~]# mongod --version
db version v4.0.5
git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
allocator: tcmalloc
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64

5. Create data directory, log directory, configuration file directory and process file directory

mkdir -p /usr/local/mongodb/data
mkdir -p /usr/local/mongodb/log
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/run/mongodb

6. Create profile

vim /usr/local/mongodb/conf/mongodb.conf

Write the following configuration

#Monitor port
port = 27017
#Database file directory
dbpath = /usr/local/mongodb/data
#Log file storage path
logpath = /usr/local/mongodb/log/mongodb.log
#Process file storage path
pidfilepath = /var/run/mongodb/mongodb.pid
#Write log using append
logappend = true
#Run as daemons, create server processes
fork = true
#Maximum simultaneous connections
maxConns = 100
#One operation log will be recorded for each write
journal = true
#No ip binding, so it can be accessed externally
bind_ip = 0.0.0.0
#Check permission
auth = true

7. Create a mongodb user who cannot log in and set permissions

useradd mongodb -M -s /sbin/nologin
chown -R mongodb.mongodb /usr/local/mongodb
chown -R mongodb.mongodb /var/run/mongodb

8. Create startup script

vim /etc/init.d/mongod

Write the following configuration

#!/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="/usr/local/mongodb/log/mongodb.log"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
OPTIONS="--quiet -f /usr/local/mongodb/conf/mongodb.conf"
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 $?

Setting permissions

chmod 755 /etc/init.d/mongod

Join service

chkconfig --add mongod

Boot up

chkconfig mongod on

9. Download and unzip PHP Mongo source code

wget -c http://pecl.php.net/get/mongodb-1.5.3.tgz
tar -zxvf mongodb-1.5.3.tgz

10. Configuration, compilation and installation

cd mongodb-1.5.3
/usr/local/php72/bin/phpize
./configure --with-php-config=/usr/local/php72/bin/php-config
make -j4
make install

11. Add mongodb extension to php.ini

echo "extension=mongodb.so" >> /usr/local/php72/etc/php.ini

12. Restart php and check phpinfo();

Original address: https://www.jmsite.cn/blog-266.html

Posted by icm on Sun, 01 Dec 2019 21:01:27 -0800