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?

More than 1 year has passed since last update.

[Day1] セットアップ - MongoDB Atlas

Last updated at Posted at 2024-01-03

概要

(本記事は個人用メモ)
クラウドDB MongoDB Atlas のデータベース部分をセットアップし動作確認

Atlasにユーザ登録・組織の作成(略)

公式文書参照

CLIツール準備とAtlasへのログイン

ローカルPC上で操作

  • OS: Ubuntu 22.04 on WSL2
atlascli-install.sh (sudoで実行)
apt update
apt install -y apt-utils gnupg wget
wget -qO - https://pgp.mongodb.com/server-7.0.asc | apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list
apt update
apt install -y mongodb-atlas-cli
Dockerでコマンドを用意する場合
atlasコマンド用Dockerfile
FROM ubuntu:22.04
COPY atlascli-install.sh .
RUN sh atlascli-install.sh
ENTRYPOINT ["/usr/bin/atlas"]
build&run
sudo build -t mongodbAtlas .
alias atlas='sudo docker run --rm -u `id -u`:`id -g` -v $HOME:$HOME -e HOME=$HOME -it mongodbAtlas'
Atlasにログイン・組織ID取得
atlas auth login
ORGID=`atlas organizations list | grep -oE '[0-9a-f]{24}'`

構成

プロジェクト・DB準備

プロジェクト作成・ID取得
PROJECT=project1
atlas projects create "$PROJECT" --orgId="$ORGID"
atlas projects list  # to confirm
PROJECTID=`atlas projects list | grep "$PROJECT" | grep -oE '[0-9a-f]{24}'`
クラスタ作成
CLUSTER=cluster1
PROVIDER=AZURE    # AZURE | AWS | GCP
REGION=US_WEST    # see: atlas clusters create --help
atlas clusters create $CLUSTER --projectId $PROJECTID --provider $PROVIDER -r $REGION --tier M0
atlas clusters list --projectId $PROJECTID   # to confirm
  • 無料プラン(M0)クラスタは1つのみ作成可
  • M0を作成できないREGIONがある

試しにDBやCollectionを作成

DBアクセス元として自身(操作中のPC)のIPを登録
atlas accessList create --projectId $PROJECTID --currentIp  # 自身以外のIPを登録する場合はIPを記述
atlas accessList list # to confirm
DBアクセス用ユーザ作成
PASSWORD='secretString'
DBUSER='user1'
atlas dbusers create atlasAdmin --username "$DBUSER" --projectId $PROJECTID -p "$PASSWORD"
atlas dbusers list # to confirm
接続文字列取得
CONNSTR=`atlas clusters connectionStrings describe "$CLUSTER" --projectId $PROJECTID | grep -oE 'mongodb.*'`
mongoshインストール

参考

mongoshインストール
wget -qO - https://pgp.mongodb.com/server-7.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt install -y mongodb-mongosh
mongoshをシェルとして起動
mongosh "$CONNSTR" --apiVersion 1 --username $DBUSER -p "$PASSWORD"
mongosh上で実行: データベースtodoのコレクションItemにデータ挿入と列挙
use todo
db.Item.insertOne({
  "_id": "659dd3c54b9dc33e99f0f999",
  "summary": "Test1",
  "isComplete": false,
  "owner_id": "xxx"
})
db.Item.find({ "isComplete": false })

参照

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?