Ubuntu 21.04への最新版MongoDBのインストール

サーバー

現時点(2021年9月末)では、最新版MongoDBはUbuntu 21.04に対応していません。
そもそも、MongoDB 5系ではUbuntu 21.04がサポート対象外です。

しかし、最新版のMongoDBをUbuntu 21.04にインストールできます。
この記事では、その方法を解説していきます。

本記事の内容

  • OS標準のMongoDB
  • aptコマンドによる最新版MongoDBのインストール
  • インストールしたMongoDBの動作確認

それでは、上記に沿って解説していきます。

OS標準のMongoDB

Ubuntuには、OS標準のパッケージが存在しています。
ApacheやMySQLなどであれば、ほぼ確実にOS標準が存在します。

MongoDBに関しても、OS標準のパッケージが存在していました。
少なくともUbuntu 20.04 LTSでは、存在しています。

しかし、Ubuntu 21.04ではOS標準のMongoDBが存在していません。
なお、今回は以下のUbuntuで検証しています。

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu 
DISTRIB_RELEASE=21.04 
DISTRIB_CODENAME=hirsute 
DISTRIB_DESCRIPTION="Ubuntu 21.04"

Ubuntu 21.04上で次のコマンドを実行。

$ sudo apt info mongodb 
Package: mongodb 
State: 実際のパッケージではありません (仮想) 
N: 候補が存在しないので、パッケージ mongodb の候補バージョンを選べません 
N: 純粋な仮想パッケージのため、パッケージ 'mongodb' のバージョンを選べません 
N: パッケージが見つかりません

なんと、上記のUbuntuにはまだOS標準のMongoDBが存在していません。
これは、タイミングだけの問題なのでしょうかね。

ただ、Ubuntu 21.04にOS標準のMongoDBがあろうとなかろうと基本的には関係ありません。
どのみち、OS標準は最新版でもありません。

そのため、最新版MongoDBをインストールする必要があります。

以上、OS標準のMongoDBについて説明しました。
次は、aptコマンドで最新版MongoDBをインストールしていきます。

aptコマンドによる最新版MongoDBのインストール

公式のインストール手順
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

現時点(2021年9月末)では、MongoDB 5.0.3が最新バージョンとなります。
MongoDB 5.0.3は、2021年9月21日にリリースされています。

ただ、ここで残念なお知らせがあります。
なんと、MongoDBはまだUbuntu 21.04に対応していません。
OS標準のMongoDBがなかったのも、これが理由かもしれませんね。

しかし、安心してください。
安心していいのかどうかは微妙ですが、Ubuntu 21.04にMongoDB 5.0.3をインストールできます。

Ubuntu 20.04 LTS用の手順に従えば、問題なくインストール可能です。
aptによる管理であれば、最悪アンインストールも簡単にできます。

よって、神経質にならずにインストールしましょう。
では、まずはそのリポジトリ(パッケージ)のGPGキーをインストールします。

$ wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK

すんなり「OK」ではなく、警告付きです。
問題ありませんので、進めましょう。

リポジトリ定義を保存します。

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

念のため、定義を確認します。

$ sudo cat /etc/apt/sources.list.d/mongodb-org-5.0.list
deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse

確認できたら、パッケージリストの更新を行います。

$ sudo apt-get update

ここで、再びMongoDBのパッケージを確認します。

$ sudo apt show mongodb
Package: mongodb
State: 実際のパッケージではありません (仮想)
N: 候補が存在しないので、パッケージ mongodb の候補バージョンを選べません
N: 純粋な仮想パッケージのため、パッケージ 'mongodb' のバージョンを選べません
N: パッケージが見つかりません

「さっきと同じ!?」
「Ubuntu 20.04 LTS用をリポジトリに登録したから?」

パッケージが確認できないため、いろいろと疑問が湧いてくるかもしれません。
でも、これで問題ありません。

そもそも、公式の手順に従うと、「mongodb」というパッケージ名では認識されません。
その代わりに、「mongodb-org」という名前で認識されます。
これは、Ubuntu 18.04でも20.40でも同じことです。

では、「mongodb-org」で確認してみましょう。

$ sudo apt info mongodb-org
Package: mongodb-org
Version: 5.0.3
Priority: optional
Section: database
Maintainer: MongoDB Packaging <packaging@mongodb.com>
Installed-Size: 8,192 B
Depends: mongodb-org-database, mongodb-org-tools, mongodb-mongosh
Conflicts: mongo-10gen, mongo-10gen-enterprise, mongo-10gen-enterprise-server, mongo-10gen-server, mongo-10gen-unstable, mongo-10gen-unstable-enterprise, mongo-10gen-unstable-enterprise-mongos, mongo-10gen-unstable-enterprise-server, mongo-10gen-unstable-enterprise-shell, mongo-10gen-unstable-enterprise-tools, mongo-10gen-unstable-mongos, mongo-10gen-unstable-server, mongo-10gen-unstable-shell, mongo-10gen-unstable-tools, mongo18-10gen, mongo18-10gen-server, mongo20-10gen, mongo20-10gen-server, mongodb, mongodb-10gen, mongodb-10gen-enterprise, mongodb-10gen-unstable, mongodb-10gen-unstable-enterprise, mongodb-10gen-unstable-enterprise-mongos, mongodb-10gen-unstable-enterprise-server, mongodb-10gen-unstable-enterprise-shell, mongodb-10gen-unstable-enterprise-tools, mongodb-10gen-unstable-mongos, mongodb-10gen-unstable-server, mongodb-10gen-unstable-shell, mongodb-10gen-unstable-tools, mongodb-clients, mongodb-dev, mongodb-enterprise, mongodb-enterprise-mongos, mongodb-enterprise-server, mongodb-enterprise-shell, mongodb-enterprise-tools, mongodb-enterprise-unstable, mongodb-enterprise-unstable-mongos, mongodb-enterprise-unstable-server, mongodb-enterprise-unstable-shell, mongodb-enterprise-unstable-tools, mongodb-nightly, mongodb-org-unstable, mongodb-org-unstable-mongos, mongodb-org-unstable-server, mongodb-org-unstable-shell, mongodb-org-unstable-tools, mongodb-server, mongodb-stable, mongodb18-10gen, mongodb20-10gen
Homepage: http://www.mongodb.org
Download-Size: 2,932 B
APT-Manual-Installed: yes
APT-Sources: https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 Packages
Description: MongoDB open source document-oriented database system (metapackage)
 MongoDB is built for scalability, performance and high availability, scaling from single server deployments to large, complex multi-site architectures. By leveraging in-memory computing, MongoDB provides high performance for both reads and writes. MongoDB’s native replication and automated failover enable enterprise-grade reliability and operational flexibility.
 .
 MongoDB is an open-source database used by companies of all sizes, across all industries and for a wide variety of applications. It is an agile database that allows schemas to change quickly as applications evolve, while still providing the functionality developers expect from traditional databases, such as secondary indexes, a full query language and strict consistency.
 .
 MongoDB has a rich client ecosystem including hadoop integration, officially supported drivers for 10 programming languages and environments, as well as 40 drivers supported by the user community.
 .
 MongoDB features:
 * JSON Data Model with Dynamic Schemas
 * Auto-Sharding for Horizontal Scalability
 * Built-In Replication for High Availability
 * Rich Secondary Indexes, including geospatial
 * TTL indexes
 * Text Search
 * Aggregation Framework &amp; Native MapReduce
 .
 This metapackage will install the mongo shell, import/export tools, other client utilities, server software, default configuration, and init.d scripts.

MongoDB 5.0.3のパッケージが、認識されています。
このパッケージをインストールしていきます。

インストールは、以下のコマンドで行います。

$ sudo apt-get install -y mongodb-org

次のような表示で終れば、インストールは成功しています。

mongodb-org-shell (5.0.3) を設定しています ...
mongodb-database-tools (100.5.0) を設定しています ...
mongodb-org-mongos (5.0.3) を設定しています ...
mongodb-org-database-tools-extra (5.0.3) を設定しています ...
mongodb-org-database (5.0.3) を設定しています ...
mongodb-org-tools (5.0.3) を設定しています ...
mongodb-org (5.0.3) を設定しています ...
man-db (2.9.4-2) のトリガを処理しています ...
Scanning processes...
Scanning linux images...

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

以上、aptコマンドによる最新版MongoDBのインストールについての説明でした。
最後に、MongoDBの動作確認を行います。

インストールしたMongoDBの動作確認

一通り次の基本動作を行います。

  • MongoDBの起動
  • MongoDBの状況確認
  • MongoDBの停止
  • MongoDBの再起動

それぞれを下記で確認します。

MongoDBの起動

$ sudo systemctl start mongod

実行しても、何も表示されません。

MongoDBの状況確認

$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-09-30 12:02:20 JST; 2min 45s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 13177 (mongod)
     Memory: 62.8M
     CGroup: /system.slice/mongod.service
             └─13177 /usr/bin/mongod --config /etc/mongod.conf

 9月 30 12:02:20 ubuntu-hirsute systemd[1]: Started MongoDB Database Server.

Ubuntu 21.04「ubuntu-hirsute」上で、MongoDBが動いていることを確認できます。

MongoDBの停止

$ sudo systemctl stop mongod

起動の場合と同じく、実行しても何も表示されません。
状況を確認しておきましょう。

$ sudo systemctl status mongod
● 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

 9月 30 12:02:20 ubuntu-hirsute systemd[1]: Started MongoDB Database Server.
 9月 30 12:07:22 ubuntu-hirsute systemd[1]: Stopping MongoDB Database Server...
 9月 30 12:07:22 ubuntu-hirsute systemd[1]: mongod.service: Succeeded.
 9月 30 12:07:22 ubuntu-hirsute systemd[1]: Stopped MongoDB Database Server.

停止していることを確認できます。

MongoDBの再起動

$ sudo systemctl restart mongod

起動・停止と同じく、再起動でも何も表示されません。
起動状況が気になる場合は、「MongoDBの状況確認」を行いましょう。

以上、MongoDBの動作確認を説明しました。

タイトルとURLをコピーしました