Install MongoDB (version 6.0) under Ubuntu 22.04 and perform related database operations

foreword

Yesterday, I used ubuntu22.04 to install the redis-5.0.5 service. Because the version is not compatible, it caused frequent problems, and finally solved it with the help of the teacher. This reminds me again of the importance of version compatibility

MongoDB installation and deployment

Because the ubuntu version used is 22.04, it cannot be installed and deployed according to the documents given by the teacher. Only by referring to a few blog posts can the problem to be solved be solved

First of all, we must pay attention to the compatibility between the ubuntu version and the database MongoDB version to be installed

Version display

insert image description here

formal process

Install the curl tool

Related commands:

apt install curl

insert image description here

Import the public key of MongoDB version 6.0 and check whether the public key is imported successfully

Related commands:

curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -

apt-key list

insert image description here

Import the resource link of the MongoDB 6.0 software package to APT

Related commands:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

sudo apt update

insert image description here

Install MongoDB

Before installing MongoDB, you must first install MongoDB's dependency libssl1.1 (use the curl command to install after installing curl)

The relevant commands are as follows:

curl -LO http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1-1ubuntu2.1~18.04.20_amd64.deb

sudo dpkg -i ./libssl1.1_1.1.1-1ubuntu2.1~18.04.20_amd64.deb

sudo apt install mongodb-org
insert image description here
insert image description here

Start the MongoDB service and check the status of the MongoDB service

The relevant commands are as follows:

sudo systemctl start mongod

sudo systemctl status mongod

Add service to start mongodb at system startup:sudo systemctl enable mongod
insert image description here

Start MongoDB

It will be found that if the version is not considered, mongo --eval 'db.runCommand({connectionStatus: 1})'an error will be reported directly

Because the version is 6.0, you need to enter it in the terminal mongosh. This command is equivalent to the mongo command before version 6.0

At this time, you can enter the command line, entershow dbs

Related commands:

mongosh

View all databases in the MongoDB database:show dbs

Using the database that needs to be operated
can be understood as opening a database, or creating a new database (MongoDB does not need to create documents in advance, it is automatically created when used):use School

Add collection: db.createCollection(‘teacher’)(a collection is equivalent to a table in a mysql database)

View all collections in the current database:show collections

Insert data:db.teacher.insert({_id:1,sname:’张三’,sage:20})

Query all records:db.teacher.find()

insert image description here

insert image description here

insert image description here

Mongo database related operations

The version is 6.0, and its update operation has also changed, so I tried several times in the groping

Related commands:

Update operation:db.teacher.update({_id:1},{$set:{sname:'李四'}})

Query records with sname='Lisi':db.teacher.find({sname:'李四'})

Query the specified column sname data:db.teacher.find({},{sname:1})

AND condition query:db.teacher.find({sname:'李四',sage:21})

OR condition query:db.teacher.find({$or:[{sage:20},{sage:21}]})

Formatted output:db.teacher.find().pretty()

delete data:db.teacher.remove({sname:’李四’})

Delete collection:db.teacher.drop()

insert image description here
insert image description here

reference article

  • https://blog.csdn.net/Lenhart001/article/details/127335370
  • https://blog.csdn.net/majiayu000/article/details/126491116
  • https://blog.csdn.net/dongkeai/article/details/127330013

summary

This is an experience article, for reference, don’t step on the pit~

Guess you like

Origin blog.csdn.net/lion_no_back/article/details/128513143