The introduction

Recently, I learned about Node and mongodb. After doing some small projects locally, I had a lot of problems trying to install mongodb on a server and then on a Linux machine, so I recorded the pit encountered. Here I only say how to install mongodb.

Install the mongo

Official website download addressMy server is Ubuntu, so download the installation package from mongodb’s official website.

Install /usr/local/mongodb on the server and decompress the package

Sudo tar - ZXVF mongo - Linux - x86_64 - ubuntu1604-4.2.6Copy the code

After the decompression, create a directory /data/db in the root directory of the server to store data information, and create a directory /data/log to store database logs

sudo mkdir -p /data/db
sudo mkdir -p /data/log
Copy the code

Then Mongodba decompresses the completed bin directory, executes sudo./mongod to start the service, and executes./mongo to connect to the data server for database operation.


Configure the MongoDb environment variables

After installing mongodb, we also need to configure environment variables so that we don’t need to go to the bin directory to execute./momgo to connect to the database, just type mongo in any directory on the server to connect. The operation is as follows:

cd /etc
sudo vi profile
// Add the following text at the end of the fileexport PATH=/usr/local/ mongo/mongo - Linux - x86_64 - ubuntu1604-4.2.6 / bin:$PATH
Copy the code

After saving and exiting, execute source Profile for the configuration to take effect immediately, and the environment variables are configured. Enter mongo in any directory to connect to the database.

MongoDb keeps running in the background

–fork /mongod –fork/sudo./mongod –fork/sudo./mongod –fork/sudo. Sudo./mongod –logpath=/data/log –fork


Logpath = logpath = logpath = logpath = logpath = logpath = logpath = logpath = logpath /mongod –logpath=/data/log/mongod. Log –fork Have seen the prompt successfully. By the way, node’s tools to keep things started in the background can be implemented using PM2. See the pM2 documentation for details.

conclusion

It took me a lot of time to get to know this thing for the first time, but I felt a sense of accomplishment. The above is the problem I encountered when learning mongodb, if there is a supplement or some places to write wrong, please leave a message in the comment area, thank you!