Tutorial for installing MongoDB and its launch command mongod on Ubuntu systems

  • 2020-06-03 08:40:56
  • OfStack

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 execute

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

Before running, however, you need to create the directories that mongodb needs to hold the data and logs:

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

Start the mongodb server

./mongod -journal -maxConns=2400 -rest

-ES25en means to log, -ES26en =2400 means mongodb can accept 2400 tcp connections, -ES29en means to allow clients to access mongdb server via rest API.
You can also use the parameter - quiet startup can specify quiet mode to reduce the number of logged items, note that this parameter must also specify the log path, such as:
- quiet - logpath/data/db/journal/mongdb log

Modify the maximum number of connections allowed by the system
The limit of the above maximum number of connections is due to the default of Linux system, the maximum number of files open for 1 process is 1024. Check with the ES48en-ES49en command, you can see the following line:

open files           (-n) 1024

Modify/etc/security/limits conf configuration file.
Using 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

* indicates that this configuration is valid for all users, with root users adding two lines in particular.
The hard limit is usually calculated based on the system's hardware resource status (mainly system memory), and the soft limit is a one-step limit on this basis. So the number of soft limits is lower than the number of hard limits.
nofile stands for max number of open files
Restart the computer and then use the ES80en-ES81en command to see:

open files           (-n) 3000

It's already in effect. Now restart mongodb server again and the problem is resolved

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

# Provides:   mongodb 
# Required-Start: 
# Required-Stop: 
# Default-Start:    2 3 4 5 
# Default-Stop:     0 1 6 
# Short-Description: mongodb 
# Description: mongo db server 
. /lib/lsb/init-functions 
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'` 
test -x $PROGRAM || exit 0 
case "$1" in 
   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 
   log_begin_msg "Stopping MongoDB server"  
   if [ ! -z "$MONGOPID" ]; then  
    kill -15 $MONGOPID 
   log_end_msg 0 
   log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}"  
   exit 1 
exit 0 

Please note that use sudo chmod + x/etc/init d/mongodb command allows the script can be executed
Then run the following command to register the boot 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 delete it via ES111en-ES112en. d-ES114en mongodb remove
Restart with ps-def | grep mongod to view the self-started service process and then close/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, and now we are testing the server on another terminal.
Enter /usr/local/ ES129en-ES130en-ES131en86_64-2.0.2 /bin, execute./mongo

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


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

And then find

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


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

Congratulations on the successful installation of mongodb

You can also connect to remote mongodb server using the default port of 27017, for example

Creating a database
If you do not have an mydb database, use the command in the client:

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

The mydb database is created, and the current database is switched to mydb.
show dbs does not display the database name at this point. Use the db. stats() command to check the current database state.

Standard inspection procedure
1. First check ES173en-a
Check to see if open files (-ES178en) is the value set

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

Check to see if the service starts

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

Check to see if the server is correct
4. Enter the http: / /
See if the server starts up properly
5. Enter /usr/ mongodb-ES195en-x86_64-2.0.2 /bin and execute
See if you can log in

Ann � PHP MongoDB � exhibition

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

Add to ES208en.ini


Mongodb start command mongod parameter description
The main parameters of mongod are:

quiet # quiet output --port arg # specifies the service port number, default port 27017 --bind_ip arg # bind to service IP, if bound to, then only local access, not specifying default local all IP --logpath arg # specify the MongoDB log file, note that the specified file is not a directory logappend # writes to the log using appends The full path to pidfilepath arg # PID File, if not set, no PID file The full path of the private key of the keyFile arg # cluster is valid only for the Replica Set schema --unixSocketPrefix arg # UNIX Domain Socket replacement directory,(default: /tmp) --fork # runs MongoDB as a daemon, creating the server process --auth # enables validation --cpu # regularly displays CPU utilization and iowait for CPU --dbpath arg # specifies the database path --diaglog arg # diaglog option 0=off 1=W 2=R 3=both 7=W+some reads --directoryperdb # set up that each database will be stored in a separate directory --journal # enables logging and MongoDB's data operations will be written to files in the journal folder --journalOptions arg # enable logging diagnostics option --ipv6 # enables the IPv6 option --jsonp # Allows JSONP form access via HTTP (with security implications) The maximum number of simultaneous connections is 2000 by default --noauth # does not enable validation --nohttpinterface # closes the http interface, turning off port 27018 access by default --noprealloc # disables preallocation of data files (often affecting performance) --noscripting # disable scripting engines notablescan # does not allow table scans --nounixsocket # disable Unix socket listening --nssize arg (=16) # Setup message database.ns file size (MB) --objcheck # After receiving customer data, check the validity, --profile arg # file parameter 0=off 1=slow, 2=all --quota # limits the number of files per database, setting the default to 8 --quotaFiles arg # number of files allower per db, requires --quota Turn on the simple rest API --repair # Fixes all databases run repair on all dbs --repairpath arg # fixes the directory of files generated by the library, default to the directory name dbpath --slowms arg (=100) # value of slow for profile and console log --smallfiles # USES a smaller default file --syncdelay arg (=60) # Number of seconds for data to be written to disk (0=never, not recommended) --sysinfo # print 1 some diagnostic system information --upgrade # if you need to upgrade the database * Replicaton parameter --fastsync # enables slave library replication from an dbpath whose database is a snapshot of the master library and can be used to quickly enable synchronization If the data synchronization between the slave library and the master library is much worse, automatically resynchronize, --oplogSize arg # sets the size of oplog (MB) * master/slave parameter --master # main library pattern --slave # From library pattern --source arg # slave port number --only arg # specifies database replication for single 1 --slavedelay arg # Set the latency for slave libraries to synchronize the master library * Replica set(replica set) option: --replSet arg # sets the replica set name * Sharding(sharding) option --configsvr # states that this is an config service for one cluster, default port 27019, default directory /data/configdb --shardsvr # declares that this is a sharding of a cluster and the default port is 27018 --noMoveParanoia # Turn off the paranoia to save moveChunk data

# The above parameters can be written to mongod.conf configuration document for example:

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

e. g:

./mongod -shardsvr -replSet shard1 -port 16161 -dbpath /data/mongodb/data/shard1a -oplogSize 100 -logpath /data/mongodb/logs/shard1a.log -logappend -fork -rest

Related articles: