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
#!/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 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
appear
tar zxvf mongodb-linux-x86_64-2.0.2.tgz
cd /usr/mongodb-linux-x86_64-2.0.2/bin
0
perform
tar zxvf mongodb-linux-x86_64-2.0.2.tgz
cd /usr/mongodb-linux-x86_64-2.0.2/bin
1
And then find
tar zxvf mongodb-linux-x86_64-2.0.2.tgz
cd /usr/mongodb-linux-x86_64-2.0.2/bin
2
see
{ "_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
./mongo 192.168.30.25
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
4
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
2.
tar zxvf mongodb-linux-x86_64-2.0.2.tgz
cd /usr/mongodb-linux-x86_64-2.0.2/bin
5
Check to see if the service starts
3.
tar zxvf mongodb-linux-x86_64-2.0.2.tgz
cd /usr/mongodb-linux-x86_64-2.0.2/bin
6
Check to see if the server is correct
4. Enter the http: / / 192.168.1.199:28017
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
7
Add to ES208en.ini
extension=mongo.so
Mongodb start command mongod parameter description
The main parameters of mongod are:
# 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
9
e. g:
./mongod -shardsvr -replSet shard1 -port 16161 -dbpath /data/mongodb/data/shard1a -oplogSize 100 -logpath /data/mongodb/logs/shard1a.log -logappend -fork -rest