How to install mongodb 7.0 to Ubuntu 22.04

How to install mongodb 7.0 to Ubuntu 22.04

1、安装

1.1、添加gpg

bash 复制代码
curl -fsSL https://pgp.mongodb.com/server-7.0.asc |  sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

1.2、添加apt源

bash 复制代码
echo "deb [ arch=amd64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

1.3、更新

bash 复制代码
apt update

1.4、安装

bash 复制代码
apt install -y mongodb-org 

2、管理

2.1、服务管理

2.1.1、查看服务状态

安装完成以后,服务是否启动,我们是不知道的,这个时候怎么查看呢,一般来说,mongodb安装完成以后,都会安装成系统服务。通常来说,对于系统服务,我们可以借助以下命令来查看服务是否启动。

bash 复制代码
systemctl status mongod.service

这里,我们来检测一下:

bash 复制代码
lwk@qwfys:~$ sudo systemctl status mongod.service 
○ mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://docs.mongodb.org/manual

Oct 21 00:16:33 qwfys systemd[1]: Stopping MongoDB Database Server...
Oct 21 00:16:33 qwfys systemd[1]: mongod.service: Deactivated successfully.
Oct 21 00:16:33 qwfys systemd[1]: Stopped MongoDB Database Server.
Oct 21 00:16:33 qwfys systemd[1]: mongod.service: Consumed 4.804s CPU time.
Oct 21 00:16:33 qwfys systemd[1]: Started MongoDB Database Server.
Oct 21 00:16:33 qwfys mongod[6093]: {"t":{"$date":"2023-10-20T16:16:33.931Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK>
Oct 21 10:39:19 qwfys systemd[1]: Stopping MongoDB Database Server...
Oct 21 10:39:21 qwfys systemd[1]: mongod.service: Deactivated successfully.
Oct 21 10:39:21 qwfys systemd[1]: Stopped MongoDB Database Server.
Oct 21 10:39:21 qwfys systemd[1]: mongod.service: Consumed 37.350s CPU time.
lwk@qwfys:~$ 

这里,我们看到服务没有启动。

2.1.2、启动服务

我们启动一下。

bash 复制代码
lwk@qwfys:~$ sudo systemctl start mongod.service 
lwk@qwfys:~$ sudo systemctl status mongod.service 
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-10-21 10:44:56 CST; 8s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 8924 (mongod)
     Memory: 169.4M
        CPU: 762ms
     CGroup: /system.slice/mongod.service
             └─8924 /usr/bin/mongod --config /etc/mongod.conf

Oct 21 10:44:56 qwfys systemd[1]: Started MongoDB Database Server.
Oct 21 10:44:57 qwfys mongod[8924]: {"t":{"$date":"2023-10-21T02:44:57.041Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK>
lwk@qwfys:~$ 

这里,我们看到,服务已经启动起来了。

2.1.3、 设置服务为开机启动

通常情况下,对于服务器程序,我可以将其调协为开机启动。

bash 复制代码
lwk@qwfys:~$ sudo systemctl enable mongod.service 
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.
lwk@qwfys:~$ 

2.1.4、取消服务开机启动

有的时候,我们又不需要让服务开机启动,这个要求是合理的,具体如下:

bash 复制代码
lwk@qwfys:~$ sudo systemctl disable mongod.service 
Removed /etc/systemd/system/multi-user.target.wants/mongod.service.
lwk@qwfys:~$ 

2.1.5、关闭服务

如果希望关闭服务,可以执行以下命令:

bash 复制代码
lwk@qwfys:~$ sudo systemctl stop mongod.service 
lwk@qwfys:~$ sudo systemctl status mongod.service 
○ mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://docs.mongodb.org/manual

Oct 21 10:39:19 qwfys systemd[1]: Stopping MongoDB Database Server...
Oct 21 10:39:21 qwfys systemd[1]: mongod.service: Deactivated successfully.
Oct 21 10:39:21 qwfys systemd[1]: Stopped MongoDB Database Server.
Oct 21 10:39:21 qwfys systemd[1]: mongod.service: Consumed 37.350s CPU time.
Oct 21 10:44:56 qwfys systemd[1]: Started MongoDB Database Server.
Oct 21 10:44:57 qwfys mongod[8924]: {"t":{"$date":"2023-10-21T02:44:57.041Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK>
Oct 21 10:53:39 qwfys systemd[1]: Stopping MongoDB Database Server...
Oct 21 10:53:39 qwfys systemd[1]: mongod.service: Deactivated successfully.
Oct 21 10:53:39 qwfys systemd[1]: Stopped MongoDB Database Server.
Oct 21 10:53:39 qwfys systemd[1]: mongod.service: Consumed 4.566s CPU time.
lwk@qwfys:~$

2.1.6、服务重启

如果需要重启服务,可以用以下命令:

bash 复制代码
lwk@qwfys:~$ sudo systemctl status mongod.service 
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-10-21 10:56:42 CST; 8s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 9271 (mongod)
     Memory: 169.5M
        CPU: 787ms
     CGroup: /system.slice/mongod.service
             └─9271 /usr/bin/mongod --config /etc/mongod.conf

Oct 21 10:56:42 qwfys systemd[1]: Started MongoDB Database Server.
Oct 21 10:56:42 qwfys mongod[9271]: {"t":{"$date":"2023-10-21T02:56:42.485Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK>
lwk@qwfys:~$ sudo systemctl restart mongod.service 
lwk@qwfys:~$ sudo systemctl status mongod.service 
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-10-21 10:57:21 CST; 2s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 9326 (mongod)
     Memory: 169.3M
        CPU: 717ms
     CGroup: /system.slice/mongod.service
             └─9326 /usr/bin/mongod --config /etc/mongod.conf

Oct 21 10:57:21 qwfys systemd[1]: Started MongoDB Database Server.
Oct 21 10:57:21 qwfys mongod[9326]: {"t":{"$date":"2023-10-21T02:57:21.090Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK>
lwk@qwfys:~$ 

2.2、mongosh

这里,我们借助mongosh命令来完成mongodb的相关管理工作。

2.2.1、进入mongosh

bash 复制代码
lwk@qwfys:~$ mongosh
Current Mongosh Log ID:	653297d8d2647c0396c5de9d
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.2
Using MongoDB:		7.0.2
Using Mongosh:		2.0.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2023-10-20T22:09:01.515+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2023-10-20T22:09:02.223+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2023-10-20T22:09:02.224+08:00: vm.max_map_count is too low
------

test> show dbs
admin   40.00 KiB
config  60.00 KiB
local   72.00 KiB
test>

这里,我们可以看到,系统安装成功以后,会自带三个库,分别是test、admin、local。

2.2.2、创建数据库

在mongosh中,如果要我们借助命令use可以完成数据库的创建与切换。下面,我们创建一个名称为emei的库。

bash 复制代码
test> use emei
switched to db emei
emei>

2.2.3、数据库用户

2.2.3.1、查看数据库用户

可以借助如下命令查看当前数据库有哪些用户。

bash 复制代码
emei> db.getUsers();
{ users: [], ok: 1 }
emei>

这里,我们看到,没有一个用户。

2.2.3.2、创建数据库用户

接下来,我们创建两个用户,其中一个用户名为admin,密码为Gah6kuP7ohfio4,另一个用户,用户名为admix,密码为Gah6kuP7ohfio4,并为它赋于读写权限。

bash 复制代码
emei> db.createUser(
...   {
...     user: "admin",
...     pwd: "Gah6kuP7ohfio4",
...     roles: [ { role: "readWrite", db: "emei" } ]
...   }
... )
{ ok: 1 }
emei> db.createUser(
...   {
...     user: "admix",
...     pwd: "Gah6kuP7ohfio4",
...     roles: [ { role: "readWrite", db: "emei" } ]
...   }
... )
{ ok: 1 }
emei> db.getUsers();
{
  users: [
    {
      _id: 'emei.admin',
      userId: new UUID("fedb3cb0-7850-4904-ba7a-5e182cd1e9c9"),
      user: 'admin',
      db: 'emei',
      roles: [ { role: 'readWrite', db: 'emei' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    },
    {
      _id: 'emei.admix',
      userId: new UUID("910386e6-81e2-4c5b-8eb0-6b273e5319a8"),
      user: 'admix',
      db: 'emei',
      roles: [ { role: 'readWrite', db: 'emei' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1
}
emei> 

我们看到,用户已经创建成功。

2.2.3.3、删除数据库用户

这里,我们删除用户名为admix的用户。

bash 复制代码
emei> db.dropUser("admix", {w: "majority", wtimeout: 4000});
{ ok: 1 }
emei> db.getUsers();
{
  users: [
    {
      _id: 'emei.admin',
      userId: new UUID("fedb3cb0-7850-4904-ba7a-5e182cd1e9c9"),
      user: 'admin',
      db: 'emei',
      roles: [ { role: 'readWrite', db: 'emei' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1
}
emei>
2.2.3.4、创建数据库管理员用户
bash 复制代码
emei> use admin
switched to db admin
admin> db.createUser(
...   {
...     user: "admin",
...     pwd: passwordPrompt(),
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
...  }
... )
Enter password
Gah6kuP7ohfio4
**************{ ok: 1 }
admin> exit
lwk@qwfys:~$

我们可以看到,管理员用户已经创建完成。

2.2.4、用管理账号管理mongodb

2.2.4.1、添加配置项

为了使用账号登录到mongodb数据库,这个时候,我们需要在mongodb数据库配置文件中添加启用权限配置项:

bash 复制代码
sudo tee -a /etc/mongod.conf <<-'EOF'

security:
    authorization: enabled

EOF

添加完成以后,要使配置生效,我们还需要重启mongodb服务。

2.2.4.2、基于账号连接

当然我们需要基于账号连接到mongodb数据库服务端的时候,需要采用如下的方式:

bash 复制代码
lwk@qwfys:~$ mongosh --username=admin --password=Gah6kuP7ohfio4
Current Mongosh Log ID:	6532a6ae0cdd8066876fefa0
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.2
Using MongoDB:		7.0.2
Using Mongosh:		2.0.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> show dbs;
admin   180.00 KiB
config   72.00 KiB
local    72.00 KiB
test> 

这里,我们看到,服务已经连接成功,而且可以正常使用了。

相关推荐
m0_748251521 小时前
Ubuntu介绍、与centos的区别、基于VMware安装Ubuntu Server 22.04、配置远程连接、安装jdk+Tomcat
java·ubuntu·centos
狂小虎1 小时前
Ubuntu下载zenodo文件Ubuntu download zenodo
ubuntu·数据集·zenodo
多恩Stone4 小时前
【ubuntu 连接显示器无法显示】可以通过 ssh 连接 ubuntu 服务器正常使用,但服务器连接显示器没有输出
服务器·ubuntu·计算机外设
张飞光5 小时前
MongoDB 创建集合
数据库·mongodb
张飞光5 小时前
MongoDB 创建数据库
数据库·mongodb·oracle
hunter20620611 小时前
ubuntu终端当一段时间内没有程序运行时,自动关闭终端。
linux·chrome·ubuntu
文杰一米八17 小时前
在Ubuntu上安装RabbitMQ教程
ubuntu·rabbitmq
中东大鹅18 小时前
MongoDB的索引与聚合
数据库·hadoop·分布式·mongodb
GIS小小研究僧1 天前
数据库基础知识:理论、E-R图、事务、原则
数据库·ubuntu·postgresql
中东大鹅1 天前
MongoDB实训:电子商务日志存储任务
大数据·数据库·hadoop·分布式·mongodb