[MongoDB] install and deploy MongoDB for Ubuntu

Posted by iamchris on Tue, 18 Jan 2022 19:51:46 +0100

Installing and deploying MongoDB for Ubuntu

I Introduction to MongoDB

MongoDB is a database based on distributed file storage. Written in C + +. It aims to provide scalable high-performance data storage solutions for Web applications.

MongoDB is a product between relational database and non relational database. It is the most functional and relational database among non relational databases. The data structure it supports is very loose. It is a json like bson format, so it can store more complex data types. Mongo's biggest feature is that the query language it supports is very powerful. Its syntax is a little similar to the object-oriented query language. It can almost realize most of the functions similar to single table query in relational database, and it also supports indexing of data. [application]

II MongoDB installation

Tips
This paper mainly refers to MongoDB rookie tutorial Carry out operation, practice, record some pits and optimize some details.

1. Download MongoDB compressed package

open MongoDB official website Select the version of MongoDB to Download and the installed platform, and select tgz package. Click Download to Download to this computer. Or click the Copy Link on the right side of Download to obtain the Download link, which can be downloaded on the remote host using the wget command.

Download the compressed package through the link using the following instructions:

$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-5.0.5.tgz

Unzip the package using the following instructions:

$ tar -zxvf mongodb-linux-x86_64-ubuntu2004-5.0.5.tgz

pit
Here's a question. Why not choose to download the shell(tgz) instead of the server? The author chose server to download from the beginning deb file and install. As a result, the location of relevant folders after automatic installation is not clear, and the startup is not successful. Therefore, it is recommended to use compressed package files for installation.

2. Start MongoDB service

First, create some directories to be used by MongoDB service.

$ sudo mkdir -p /var/lib/mongo     # Create data store directory
$ sudo mkdir -p /var/log/mongodb   # Create log file directory

# Set permissions
$ sudo chown `whoami` /var/lib/mongo
$ sudo chown `whoami` /var/log/mongodb

pit
The author did not create the above two folders at the beginning of the installation and installed them directly. As a result, it is impossible to start, so readers are advised to create these two directories in advance during installation.

Switch to the extracted directory of MongoDB and enter the bin directory. There are four executable files in the bin directory. mongod is used to start the MongoDB service. mongo is equivalent to the client, which will also be used in Section 3.

$ ls -l
 Total consumption 236516
drwxrwxr-x 2 fxtack fxtack      4096 1 July 17-21:10 ./
drwxrwxr-x 3 fxtack fxtack      4096 1 July 17-21:00 ../
-rwxr-xr-x 1 fxtack fxtack     15205 12 February 23:33 install_compass*
-rwxr-xr-x 2 fxtack fxtack  58236608 12 March 00:21 mongo*
-rwxr-xr-x 1 fxtack fxtack 108660928 12 March 00:22 mongod*
-rwxr-xr-x 1 fxtack fxtack  75253096 12 February 23:59 mongos*

Use the following command to start the MongoDB service. After the instruction is executed, MongoDB will run in the background without using nohup to suspend to the background.

$ ./mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

You can check the MongoDB log to confirm the successful startup.

$ cat /var/log/mongodb/mongod.log # The output of the service background is displayed

III MongoDB Shell test

After the MongoDB service is started normally, you can run mongo for link testing. The mongo executable is placed in the extracted bin directory. For convenience, you can establish a hard link to / usr/local/bin to add mongo to the environment variable.

$ sudo ln mongo /usr/local/bin/mongo

Use the following command to enter the MongoDB background, which is an interactive command line.

$ mongo

Simple operation:

> db.test.insert({x: 1024})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("61e57acf9edfcdc1e2c93117"), "value" : 1024 }

You can exit the interactive command line by typing exit or pressing ctrl+c.

Related links

Topics: Database MongoDB Ubuntu