クイックスタート: データベースを作成して接続する
MongoDB 互換の Firestore データベースを作成し、mongosh
ツールを使用して接続する方法について説明します。
始める前に
-
In the Google Cloud console, go to the project selector page.
-
Select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Cloud Datastore Owner
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
IAM に移動 - プロジェクトを選択します。
- [ アクセスを許可] をクリックします。
-
[新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。
- [ロールを選択] リストでロールを選択します。
- 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
- [保存] をクリックします。
mongosh
ツールをインストールします。MongoDB 互換の Firestore データベースを作成して接続文字列を取得する
Google Cloud コンソールで、新しい Firestore Enterprise エディションのデータベースを作成します。MongoDB 互換の Firestore には Firestore Enterprise エディションが必要です。-
Google Cloud コンソールで、[データベース] ページに移動します。
- [Firestore データベースを作成] をクリックします。
- データベース ID を入力します。
- [Enterprise エディション] を選択します。
- データベースのロケーションを選択します。
[データベースを作成] をクリックします。
データベースの初期化が完了すると、コンソールでデータベースの Firestore Studio が開きます。
-
外部の MongoDB ツールを使用して Firestore に接続するセクションで、接続文字列をコピーします。
接続文字列は、データベースの UID(システム生成)とデータベースのロケーションによって異なります。
UID.LOCATION.firestore.goog
SCRAM 認証用のユーザーを作成する
Google Cloud コンソールで、新しいデータベース ユーザーを作成し、そのユーザーに Identity and Access Management 権限を割り当てます。
-
Google Cloud コンソールで、[データベース] ページに移動します。
- データベースのリストから、必要なデータベースを選択します。
- ナビゲーション メニューで [Auth] をクリックします。
- [ユーザーを追加] をクリックします。
- ユーザー名を入力します。
- ユーザーの Identity and Access Management ロールを選択します。
- [作成] をクリックします。ユーザーがデータベースで作成され、生成されたユーザーのパスワードが表示されます。このパスワードをコピーして保存してください。このパスワードは後で取得できません。
mongosh
を使用して接続接続文字列、ユーザー名、パスワードを使用してデータベースに接続し、次の構成オプションを使用して
mongosh
をローカルで実行します。mongosh 'mongodb://USERNAME:PASSWORD@CONNECTION_STRING:443/DATABASE_ID?loadBalanced=true&authMechanism=SCRAM-SHA-256&tls=true&retryWrites=false'
以下を置き換えます。
- USERNAME: 作成したデータベース ユーザーの名前。
- PASSWORD: 作成したデータベース ユーザーの生成されたパスワード。
- CONNECTION_STRING: データベース接続文字列。
- DATABASE_ID: データベース ID
接続が確立されると、次のようにデータを作成して読み取ることができます。
db.pages.insertOne({ message: "Hello World!"}) db.pages.find({}) exit
次のステップ
-