Build Lnamp Environment (vi)-PHP7 source installation MongoDB and MongoDB expansion

Source: Internet
Author: User
Tags fpm install mongodb



Previous: building Lnamp Environment (v)-PHP7 source installation Redis and Redis expansion


First, install MongoDB


1. Creating MongoDB user groups and users


-r-g mongodb-s/sbin/nologin-m MongoDB


2. Download the MongoDB source package and put the source package in the/usr/local/src/directory
Download page:https://www.mongodb.com/download-center?jmp=nav
Mongodb-linux-x86_64-rhel62-3.2.10.tgz is used here.
:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.10.tgz



3. Enter the src/directory


cd/usr/local/src/


4. Unzip the source package


TAR-ZXF mongodb-linux-x86_64-rhel62-3.2.10.tgz


5. Create a MongoDB file directory


mkdir -p/usr/local/mongodb/datamkdir -p/usr/local/mongodb/confmkdir -P/var/run/mongodbmkdir -P/var/Log/mongodb


6. Copy the files to the mongodb/directory


Cp-r/usr/local/src/mongodb-linux-x86_64-rhel62-3.2.10/. /usr/local/mongodb


7. Create a MongoDB configuration file mongodb.conf


Vim/usr/local/mongodb/conf/mongodb.conf


8. Add the following to save the exit


Dbpath=/usr/local/mongodb/data #data directory exists
Logpath=/var/log/mongodb/mongodb.log #log file storage directory
Logappend=true #Write log mode: set to true for append
Fork=true #enabled as a daemon, ie running in the background
Verbose=true
Vvvv=true #Start verbose verbose information, its level has vv~vvvvv, the more v, the higher the level, the more detailed the information recorded in the log file
maxConns=20000 #Default value: Depends on the system (ie ulimit and file descriptor) limits. MongoDB does not limit its own connections
Pidfilepath=/var/run/mongodb/mongodb.pid
Directoryperdb=true #Data directory storage mode, if you directly modify the original data will disappear
Profile=0 #database analysis level setting, 0 off 2 on. Includes all operations. 1 open. Only slow operation
Slowms=200 # Record the slow query time of profile analysis, the default is 100 milliseconds
Quiet=true
Syncdelay=60 # Brush the frequency of writing data to the log, and manipulate the data through fsync. 60 seconds by default
#port=27017 #口
#bind_ip = 10.1.146.163 #IP
#auth=true #Starting authentication
#nohttpinterface=false #28017 The service opened by the port. Default false, support
#notablescan=false#Do not prohibit table scan operations
#cpu=true #Set to true to force mongodb to report cpu utilization and io wait every 4s, write log information to standard output or log file


9. Modify the MongoDB directory permissions


chown -R mongodb:mongodb/usr/local/mongodbchown -R mongodb:mongodb/var/run/ MongoDB chown -R mongodb:mongodb/var/Log/mongodb


10. Add the MongoDB command to the environment variable, modify the profile file


Vim/etc/profile


11. Change to the following, save exit


path=/usr/local/mysql/bin:/usr/local/php/bin:/usr/local/redis/bin:/usr/local/mongodb/bin:$PATH 


12. Make the configuration in/etc/profile effective immediately


Source/etc/profile


13. Add the MongoDB service script to the init.d/directory to create the Mongod file


Vim/etc/init.d/mongod


14. Add the following to save the exit


#!/bin/sh
# chkconfig: 2345 93 18
# description:MongoDB

#Default parameter setting
#mongodb Home Directory
MONGODB_HOME=/usr/local/mongodb

#mongodb Startup command
MONGODB_BIN=$MONGODB_HOME/bin/mongod

#mongodb configuration file
MONGODB_CONF=$MONGODB_HOME/conf/mongodb.conf

MONGODB_PID=/var/run/mongodb/mongodb.pid

#Maximum file open limit
SYSTEM_MAXFD=65535

#mongodb Name
MONGODB_NAME="mongodb"
. /etc/rc.d/init.d/functions

If [ ! -f $MONGODB_BIN ]
Then
    Echo "$MONGODB_NAME startup: $MONGODB_BIN not exists! "
    Exit
Fi

Start(){
    Ulimit -HSn $SYSTEM_MAXFD
    $MONGODB_BIN --config="$MONGODB_CONF"
    Ret=$?
    If [ $ret -eq 0 ]; then
        Action $"Starting $MONGODB_NAME: " /bin/true
    Else
        Action $"Starting $MONGODB_NAME: " /bin/false
    Fi
}

Stop(){
    PID=$(ps aux |grep "$MONGODB_NAME" |grep "$MONGODB_CONF" |grep -v grep |wc -l)
    If [[ $PID -eq 0 ]];then
        Action $"Stopping $MONGODB_NAME: " /bin/false
        Exit
    Fi
    Kill -HUP `cat $MONGODB_PID`
    Ret=$?
    If [ $ret -eq 0 ]; then
        Action $"Stopping $MONGODB_NAME: " /bin/true
        Rm -f $MONGODB_PID
    Else
        Action $"Stopping $MONGODB_NAME: " /bin/false
    Fi
}

Restart(){
    Stop
    Sleep 2
    Start
}

Case "$1" in
    Start)
        Start
        ;;
    Stop)
        Stop
        ;;
    Status)
    Status $prog
        ;;
    Restart)
        Restart
        ;;
    *)
        Echo $"Usage: $0 {start|stop|status|restart}"
Esac 
View Code


15. Add executable permissions for Mongod


chmod +x/etc/init.d/mongod


16. Adding MongoDB to System services


Chkconfig--add Mongod


17. Set Boot up


Chkconfig Mongod on


18. Start MongoDB


Service Mongod Start
Second, PHP7 installation MongoDB expansion


1. Download the PHP7 MongoDB expansion pack and place the source package in the/usr/local/src/directory



Download page:http://pecl.php.net/package/mongodb
Mongodb-1.1.9.tgz is used here.
:http://pecl.php.net/get/mongodb-1.1.9.tgz



2. Enter the src/directory


cd/usr/local/src/


3. Unpack the expansion pack


TAR-ZXF mongodb-1.1.9.tgz


4. Enter the MongoDB expansion directory, compile and install the extension


CD mongodb-1.1.9/phpize./configure--with-php-config=/usr/local/php/bin/php-&& make Install


5. Modify the php.ini file


Vim/usr/local/php/etc/php.ini


6. Add mongodb.so extension configuration, save exit


extension=mongodb.so;


7. Restart Apache or PHP-FPM


service httpd restartservice php-fpm restart


8. Add php files to the web directory, such as/usr/local/apache/htdocs/mongodb.php or/usr/local/nginx/html/mongodb.php


<?php
$manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert([‘x‘ => 1, ‘class‘=>‘toefl‘, ‘num‘ => ‘18‘]);
$bulk->insert([‘x‘ => 2, ‘class‘=>‘ielts‘, ‘num‘ => ‘26‘]);
$bulk->insert([‘x‘ => 3, ‘class‘=>‘sat‘, ‘num‘ => ‘35‘]);
$manager->executeBulkWrite(‘test.log‘, $bulk);
$filter = [‘x‘ => [‘$gt‘ => 1]];
$options = [
    ‘projection‘ => [‘_id‘ => 0],
    ‘sort‘ => [‘x‘ => -1],
];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery(‘test.log‘, $query);
foreach ($cursor as $document) {
    print_r($document);
}


Access URLs, such as: http://192.168.8.9/mongodb.php
The page is displayed correctly, the configuration is successful



MongoDB installation is complete!



Build Lnamp Environment (vi)-PHP7 source installation MongoDB and MongoDB expansion


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.