mongodb installation and configuration under Linux

MongoDB is a database based on distributed file storage. Written in C++ language. It aims to provide scalable high-performance data storage solutions for WEB applications. The data structure supported by MongoDB is very loose and is a bson format similar to json, so it can store more complex data types.

 

1. Download mongodb

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.0.tgz

 

Second, decompress mongodb

tar zxvf mongodb-linux-x86_64-rhel70-3.4.0.tgz

mv mongodb-linux-x86_64-rhel70-3.4.0 /usr/local/mongodb

Create mongodb storage directory and log directory

mkdir -p /data/mongodb/data

mkdir -p /data/mongodb/log

 

3. Create a mongodb running script

vi / data / mongodb / mongodb

/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#!/bin/sh   

  

DBPORT=27017 

DBPATH="/data/mongodb/data"  

DBLOG="/data/mongodb/logs/mongo.log" 

DBSERVER="/usr/local/mongodb/bin/mongod" 

  

function_start_mongodb()   

{   

    printf "Starting Mongodb...\n"  

    $DBSERVER --dbpath=$DBPATH --logpath=$DBLOG --logappend --port=$DBPORT --fork

}   

  

function_stop_mongodb()   

{   

    printf "Stoping Mongodb...\n"  

    $DBSERVER --shutdown  --dbpath=$DBPATH

}   

  

function_restart_mongodb()   

{   

    printf "Restarting Mongodb...\n"  

    function_stop_mongodb   

    sleep 5   

    function_start_mongodb  

}   

  

  

if [ "$1" = "start" ]; then   

    function_start_mongodb

elif [ "$1" = "stop" ]; then   

    function_stop_mongodb

elif [ "$1" = "restart" ]; then   

    function_restart_mongodb

else  

    printf "Usage: /data/mongodb/mongodb {start|stop|restart}\n"  

be

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/

 

Give the shell script executable permission:

chmod +x /data/mongodb/mongodb

Start mongodb:

/data/mongodb/mongodb start

If the following information appears, the mongodb installation is successful

child process started successfully, parent exiting

 

Start mongodb on boot

vi /etc/rc.local

Enter the following

/data/mongodb/mongodb start

save and exit

 

Fourth, test mongodb and create a verification account

 

Enter the mongodb management interface

/usr/local/mongodb/bin/mongo

 

select system table

use admin;

Create super administrator

db.createUser({user:"root",pwd:"123456",roles:["userAdminAnyDatabase"]});

 

quit mongodb

exit;

 

Modify the mongodb startup file

vi / data / mongodb / mongodb

 

Will

$DBSERVER --dbpath=$DBPATH --logpath=$DBLOG --logappend --port=$DBPORT --fork

change into

$DBSERVER --dbpath=$DBPATH --logpath=$DBLOG --logappend --auth --port=$DBPORT --fork

 

After saving and exiting, restart mongodb

/data/mongodb/mongodb restart

 

Enter the mongodb management interface

/usr/local/mongodb/bin/mongo

 

select system table

use admin;

 

authenticating:

db.auth("root","123456");

 

Returns 1, indicating that the verification is successful!

 

Create new database and verify account

use testdb;

db.createUser({user:"test",pwd:"test",roles:[{"role":"readWrite","db":"testdb"}]});

 

Exit mongodb, and then perform the above verification. If the returned result is 1, the configuration is correct.

 

 

Five, install the php extension of mongodb

Download address wget http://pecl.php.net/get/mongo-1.6.14.tgz

tar zxvf mongo-1.6.14.tgz

cd mongo-1.6.14

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make

make install

cd ../

Six, configure php

1. Modify the php.ini file

vi /usr/local/php/etc/php.ini

找到extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20151012/"

add below

extension = "mongo.so"

Save and exit php.in

 

Finally restart php.ini

/usr/local/php/php-fpm restart

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326464487&siteId=291194637