0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【M1 Mac】『データベースをなぜつくるのか』12章 MongoDB Atlas接続エラーの解決方法

Posted at

問題

書籍「データベースをなぜつくるのか」第12章 NoSQLの学習中、
M1 MacからMongoDB Atlasへの接続時にエラーが発生しました。
本がWindows前提のため、macOS特有の設定が必要でした。

※ MongoDB Atlasのクラウド設定は完了済み

実行環境

  • MacBook Air M1 2020
  • macOS: 26.1
  • Python: 3.12.8
  • pymongo: 4.15.5

データベースをなぜつくるのか 知っておきたいE-R図とSQLの基礎
Amazonで本書を見る

エラー

sample.py 初期コード(修正前)

sample.py
# MongoDBに接続するクラスをインポート
from pymongo.mongo_client import MongoClient
# APIバージョンを指定するクラスをインポート
from pymongo.server_api import ServerApi

# MongoDB Atlasクラウドの接続先URI
uri = "mongodb+srv://USER:PASSWORD@YOUR_CLUSTER.mongodb.net/?appName=Cluster0"

client = MongoClient(uri, server_api=ServerApi('1')

try:
    client.admin.command('ping')
    print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
    print(e)

pymongo をインストール

windowsはpythonだが、macはpython3を使用します。
pymongoのインストールOK。

% python3 -m pip install "pymongo[srv]"
Collecting pymongo[srv]
  Downloading pymongo-4.15.5-cp312-cp312-macosx_11_0_arm64.whl.metadata (22 kB)
WARNING: pymongo 4.15.5 does not provide the extra 'srv'
Collecting dnspython<3.0.0,>=1.16.0 (from pymongo[srv])
  Downloading dnspython-2.8.0-py3-none-any.whl.metadata (5.7 kB)
Downloading dnspython-2.8.0-py3-none-any.whl (331 kB)
Downloading pymongo-4.15.5-cp312-cp312-macosx_11_0_arm64.whl (920 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 920.7/920.7 kB 36.9 MB/s eta 0:00:00
Installing collected packages: dnspython, pymongo
Successfully installed dnspython-2.8.0 pymongo-4.15.5

[notice] A new release of pip is available: 24.3.1 -> 25.3
[notice] To update, run: python3 -m pip install --upgrade pip

エラー1

CERTIFICATE_VERIFY_FAILEDエラーが発生します。

// sample.pyを実行しようとするとエラー

% python3 sample.py
// 省略
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
// 省略

エラー2

上記を修正後、ModuleNotFoundErrorが発生します。

% python3 sample.py
// 省略
ModuleNotFoundError: No module named 'certifi'
// 省略

解決方法

SSLエラーを解消するために、証明書インストール、certifiのインストールが必要

手順1: Python用SSL証明書のインストール

macOSにインストールされているPython(「/Applications/Python\ 3.*/Install\」)に、SSL証明書をインストールします。

% /Applications/Python\ 3.*/Install\ Certificates.command
                 ↓              ↓              ↓
                パス          ワイルドカード   実行ファイル  
部分 説明
/Applications/ macOSのアプリケーションフォルダ
Python\ 3.* Python 3.x(任意のバージョン)
\ はスペースのエスケープ
* は任意の数字(例: 3.10, 3.11, 3.12)
/Install\ Certificates.command 証明書をインストールするスクリプト

手順2: certifiパッケージのインストール

SSL/TLS通信に必要な証明書バンドルを提供するcertifiパッケージをインストールします。

% python3 -m pip install certifi
Collecting certifi
  Downloading certifi-2025.11.12-py3-none-any.whl.metadata (2.5 kB)
Downloading certifi-2025.11.12-py3-none-any.whl (159 kB)
Installing collected packages: certifi
Successfully installed certifi-2025.11.12

sample.py コード(修正後)
sslとcertifiをインポートし、MongoClientに証明書ファイルのパスを指定します。

sample.py
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi

# ssl, certifiの追加
import ssl
import certifi

uri = "mongodb+srv://USER:PASSWORD@YOUR_CLUSTER.mongodb.net/?appName=Cluster0"

# m1 macの場合、「, tlsCAFile=certifi.where())」の追加
client = MongoClient(uri, server_api=ServerApi('1'), tlsCAFile=certifi.where())

try:
    client.admin.command('ping')
    print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
    print(e)

接続できた

% python3 sample.py
Pinged your deployment. You successfully connected to MongoDB!

終わりに

M1 MacでMongoDB Atlasに接続する際のSSL証明書エラーは、macOS特有の問題です。
Windowsの本に従う際は、以下の点に注意してください:

  • コマンドはpythonではなくpython3を使用
  • SSL証明書の設定が必要(tlsCAFile=certifi.where())

参考

データベースをなぜつくるのか 知っておきたいE-R図とSQLの基礎
Amazonで本書を見る

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?