在Ubuntu 20.04 | 18.04 | 16.04上安装MongoDB 4.4

时间:2020-02-23 14:40:46  来源:igfitidea点击:

在本教程中,我们将介绍在Ubuntu 20.04 | 18.04 | 16.04 Linux系统上安装MongoDB 4的步骤.MongoDB是用C ++编写的开源NoSQL数据库系统,可提供可伸缩性,高性能/可用性.NoSQL数据库系统通常称为面向文档的数据库。

MongoDB的常见用例是存储和管理大数据大小的文字文档集合,例如文本文档,电子邮件,XML文档和许多其他文档。

要获得高度可用的MongoDB设置,请使用如何在Ubuntu LTS上设置MongoDB复制教程。

在Ubuntu 20.04 | 18.04 | 16.04上安装MongoDB 4.4

有两种在Ubuntu系统上安装MongoDB的方法:建议从apt仓库安装MongoDB从下载的debpackage中安装MongoDB

本教程将演示使用apt存储库方法在Ubuntu 18.04和Ubuntu 16.04系统上安装MongoDB 4. 现在让我们在Ubuntu 20.04/18.04/Ubuntu 16.04 Linux上安装MongoDB。

导入MongoDB公共GPG密钥:

我们必须先下载GPG密钥并将其导入系统,然后才能从MongoDB apt仓库安装任何软件包。

sudo apt update
sudo apt install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add 

第2步:将MongoDB 4.4 APT存储库添加到Ubuntu 20.04 | 18.04 | 16.04

导入GPG密钥后,继续添加存储库。

Ubuntu 20.04:

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

Ubuntu 18.04:

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

Ubuntu 16.04:

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

在Ubuntu 20.04 | 18.04 | 16.04上安装MongoDB 4.4

更新软件包数据库并安装MongoDB软件包:

$sudo apt update
Hit:1 http://repo.mysql.com/apt/ubuntu bionic InRelease
Hit:2 http://mirror.hetzner.de/ubuntu/packages focal InRelease
Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:4 http://mirror.hetzner.de/ubuntu/packages focal-updates InRelease
Hit:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:6 http://mirror.hetzner.de/ubuntu/packages focal-backports InRelease
Hit:7 http://mirror.hetzner.de/ubuntu/packages focal-security InRelease
Hit:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Ign:9 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease
Hit:10 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:11 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release [5,377 B]
Get:12 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release.gpg [801 B]
Get:13 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse amd64 Packages [5,058 B]
Get:14 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse arm64 Packages [3,962 B]
Fetched 15.2 kB in 2s (6,472 B/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done

在Ubuntu 20.04 | 18.04 | 16.04上安装MongoDB服务器软件包:

sudo apt install -y mongodb-org

要安装特定发行版,我们必须分别指定每个组件包以及版本号,如以下示例所示:

sudo apt-get install -y mongodb-org=4.4.1 mongodb-org-server=4.4.1 mongodb-org-shell=4.4.1 mongodb-org-mongos=4.4.1 mongodb-org-tools=4.4.1

服务名为" mongod",我们可以通过运行以下命令启动应用程序:

sudo systemctl enable --now mongod

使用以下方法检查状态:

$systemctl status mongod
● mongod.service - MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
    Active: active (<strong>running</strong>) since Thu 2019-10-17 05:57:30 UTC; 32s ago
      Docs: https://docs.mongodb.org/manual
  Main PID: 20493 (mongod)
    CGroup: /system.slice/mongod.service
            └─20493 /usr/bin/mongod --config /etc/mongod.conf
 Oct 17 05:57:30 ubuntu18 systemd[1]: Started MongoDB Database Server.

该服务应在27017端口上侦听

$netstat -tunelp | grep 27017
(Not all processes could be identified, non-owned process info
  will not be shown, you would have to be root to see it all.)
 tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      112        53728      

MongoDB的主要配置文件是/etc/mongod.conf。我们可以根据自己的喜好来调整设置,但是请记住,每当进行更改时,都要重新启动mongod服务。

测试连接:

$mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("615620f1-c987-4a1c-b9c2-c16cf962065f") }
MongoDB server version: 4.4.1
{
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	},
	"ok" : 1
}

我们可以从" ok"中确认一切正常:1.我们还可以尝试创建测试数据库并插入一些虚拟数据。

# mongo
...
To enable free monitoring, run the following command:
db.enableFreeMonitoring()
--

> use test_db # This will create database called test_db
switched to db test_db
> db # Show current database
test_db
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.files.insert({"name":"rap"}) # Insert data to db
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test_db 0.000GB
> db.dropDatabase() # Drop our test db
> exit
bye