Home>

install mongodb server on ubuntu

Get the latest version

wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.2.tgz

Unzip and run

tar zxvf mongodb-linux-x86_64-2.0.2.tgz
cd /usr/mongodb-linux-x86_64-2.0.2/bin

But before running,You need to create the directory where mongodb needs to store data and logs:

sudo mkdir -p/data/db/journal
sudo chmod -r 777/data/db /

Start mongodb server

./mongod -journal -maxconns=2400 -rest

-journal means to write logs,-maxconns=2400 means that mongodb can accept 2400 TCP connections, and -rest means that clients can access the mongdb server through the rest API.

You can also use the parameter --quiet startup to specify quiet mode to reduce the number of items recorded,Note that you must also specify the log path when using this parameter.such as:

—Quiet —logpath /data/db/journal/mongdb.log

Modify the maximum number of connections allowed by the system

The reason for the above maximum number of connections is that the default maximum number of open files for a process in the Linux system is 1024. Use the ulimit -a command to check.You can see this line:

open files (-n) 1024

Modify the /etc/security/limits.conf configuration file.

Use the command:sudo gedit /etc/security/limits.conf

Add to file

* soft nofile 3000
* hard nofile 20000
root soft nofile 3000
root hard nofile 20000

* Means the configuration is valid for all users,The root user needs to add two special lines.

The hard limit is usually calculated based on the system's hardware resource conditions (mainly system memory).Soft limits are further restrictions on this basis.Therefore, the number of soft limits is lower than the hard limit.

nofile means max number of open files

Restart your computer,Then check it with the ulimit -a command:

open files (-n) 3000

It has taken effect.Now restart mongodb server, the problem is solved

Set up boot

Create a new script file mongodb in the /etc/init.d/directory

#!/bin/sh
###begin init info
#provides:mongodb
#required-start:
#required-stop:
#default-start:2 3 4 5
#default-stop:0 1 6
#short-description:mongodb
#description:mongo db server
###end init info
./lib/lsb/init-functions
program =/usr/mongodb-linux-x86_64-2.0.2/bin/mongod
mongopid=`ps -ef | grep" mongod "| grep -v grep | awk" {print $2} "`
test -x $program || exit 0
case "$1" in
 start)
   ulimit -n 3000
   log_begin_msg "starting mongodb server"
   $program --fork --quiet -journal -maxconns=2400 -rest --logpath /data/db/journal/mongdb.log
   log_end_msg 0
   ;;
 stop)
   log_begin_msg "stopping mongodb server"
   if [! -z "$mongopid"];then
    kill -15 $mongopid
   fi
   log_end_msg 0
   ;;
 status)
   ;;
 *)
   log_success_msg "usage:/etc/init.d/mongodb {start | stop | status}"
   exit 1
esac
exit 0

Please note that using sudo chmod + x /etc/init.d/mongodb allows the script to be executed

Then run the following command to register the startup script:

update-rc.d mongodb defaults
 adding system startup for /etc/init.d/mongodb ...
  /etc/rc0.d/k20mongodb->../init.d/mongodb
  /etc/rc1.d/k20mongodb->../init.d/mongodb
  /etc/rc6.d/k20mongodb->../init.d/mongodb
  /etc/rc2.d/s20mongodb->../init.d/mongodb
  /etc/rc3.d/s20mongodb->../init.d/mongodb
  /etc/rc4.d/s20mongodb->../init.d/mongodb
  /etc/rc5.d/s20mongodb->../init.d/mongodb

You can also remove it with update-rc.d -f mongodb remove

Restart,Through ps -def | grep mongod, you can view the self-starting service process.Then you can stop/start the service with the following command

sudo service mongodb stop
sudo service mongodb start

Client login server

The startup log is as above.The server is started as above.Now we test the server on another terminal.

Enter /usr/local/mongodb-linux-x86_64-2.0.2/bin and execute ./mongo

appear

mongodb shell version:2.0.2
connecting to:test

carried out

db.foo.save ({1:"hello world"})

Then find

db.foo.find ();

see

{"_id":objectid ("4e4b395986738efa2d0718b9"), "1":"hello world"}

Congratulations here,Successfully installed mongodb

You can also connect to a remote mongodb server in the following way.The default port is 27017, such as

./mongo 192.168.30.25

Create database

If there is no mydb database,Use the command in the client:

use mydb

The mydb database will be created and the current database is switched to mydb.

At this time show dbs does not display the database name.Use the db.stats () command to check the current database status.

Standard inspection process

1. Check ulimit -a first

See if open files (-n) are set values

2.

ps -def | grep mongod

See if the service is started

3.

cd/data/db/journal /
cat mongdb.log

See if the server is correct

4.Enter http://192.168.1.199:28017

See if the server starts normally

5. Enter /usr/mongodb-linux-x86_64-2.0.2/bin and execute ./mongo

See if you can log in

Install the php mongodb extension

sudo apt-get install php5-dev php5-cli php-pear
sudo pecl install mongo

Add in php.ini

extension=mongo.so

mongodb start command mongod parameter descriptionThe main parameters of mongod are:

--quiet #Quiet output --port arg #specifies the service port number,Default port 27017 --bind_ip arg #Bind the service IP. If you bind 127.0.0.1, you can only access it locallyDo not specify the default local all IP --logpath arg #specifies the mongodb log file,Note that the specified file is not a directory --logappend #Write logs in append mode --pidfilepath arg #pid file full path,If not set,No pid file --keyfile arg #The full path of the cluster's private key,Only valid for replica set architecture --unixsocketprefix arg #unix domain socket alternative directory,(Default is/tmp) --fork #Run mongodb as a daemon and create a server process --auth #enable authentication --cpu #Periodically display the CPU usage and iowait of the CPU --dbpath arg #specify the database path --diaglog arg #diaglog option 0=off 1=w 2=r 3=both 7=w + some reads --directoryperdb #Set each database to be saved in a separate directory --journal #enable journaling options,MongoDB data operations will be written to the file in the journal folder --journaloptions arg #enable log diagnostic options --ipv6 #enable ipv6 option --jsonp #Allow jsonp to be accessed through http (with security impact) --maxconns arg #Maximum number of simultaneous connections Default 2000 --noauth #do not enable authentication --nohttpinterface #closes the http interface and closes port 27018 by default --noprealloc#Disable data file preallocation (often affecting performance) --noscripting #Disable scripting engine --notablescan #Do not allow table scans --nounixsocket #Disable Unix socket listening --nssize arg (= 16) #Set the database.ns file size (mb) --objcheck #Upon receiving customer data,Check validity, --profile arg #profile parameters 0=off 1=slow, 2=all --quota #limits the number of files per database,Setting default is 8 --quotafiles arg #number of files allower per db, requires --quota --rest #enable simple rest api --repair #run repair on all dbs --repairpath arg #Repair the directory of files generated by the library,Default is the directory name dbpath --slowms arg (= 100) #value of slow for profile and console log --smallfiles #use smaller default files --syncdelay arg (= 60) #Time in seconds that data is written to disk (0=never, not recommended) --sysinfo #print some diagnostic system information --upgrade #If you need to upgrade the database * replicaton parameter --fastsync#enable the copy service from the dbpath,The dbpath database is a snapshot of the main library,Can be used to quickly enable synchronization --autoresync#If synchronizing data from the slave to the master is much worse,Automatic resynchronization, --oplogsize arg #Set the size of oplog (mb) * master/slave parameter --master #master library mode --slave #from library mode --source arg #from library port number --only arg #specify a single database copy --slavedelay arg #Set the delay time to synchronize the master with the slave * replica set options: --replset arg #Set the replica set name * sharding (sharding) option --configsvr #declares that this is a cluster's config service, default port 27019, default directory/data/configdb --shardsvr #declares that this is a cluster shard,Default port 27018 --nomoveparanoia #Turn off paranoia for movingchunk data saving

#The above parameters can be written into the mongod.conf configuration file. For example:

dbpath =/data/mongodb
logpath=/data/mongodb/mongodb.log
logappend=true
port=27017
fork=true
auth=true

e.g:

./mongod -shardsvr -replset shard1 -port 16161 -dbpath/data/mongodb/data/shard1a -oplogsize 100 -logpath /data/mongodb/logs/shard1a.log -logappend -fork -rest
  • Previous How to solve android Toast duplicate display
  • Next Analysis of each and list usage in PHP