Techioz Blog

Rails と MongoDB Atlas の間の認可の問題

概要

公式ドキュメントに従って、Rails アプリケーションで mongoDB アトラスを使用しようとしています。

https://translate.google.com/translate?hl=ja&sl=en&tl=ja&u=https://www.mongodb.com/docs/mongoid/current/reference/rails-integration/

しかし、作成または読み取りを行うと、次のエラーが表示されます。

# Mongo::Error::OperationFailure in PostsController#create
\[8000:AtlasError\]: (Unauthorized) not authorized on admin to execute command { insert: "posts", $db: "admin", ordered: true, writeConcern: { w: "majority" }, txnNumber: 1.000000, lsid: { id: {4 \[129 103 129 204 83 248 73 51 140 2 11 71 126 35 223 247\]} } } (on ac-xjgsx9t-shard-00-02.whzyr5q.mongodb.net:27017, modern retry, attempt 1)

手動で保存しようとしても:

3.0.0 :003 \> post = Post.new(title: "Please", body: "Help me stack overflow")

=\> #\<Post \_id: 63f590426e92a40ae84042bb, created_at: nil, updated_at: nil, title: "Please", body: "Help me stack overf...

3\.0.0 :004 \> post.save

/Users/myuser/.rvm/gems/ruby-3.0.0/gems/mongo-2.18.2/lib/mongo/operation/result.rb:364:in \`raise_operation_failure': \[8000:AtlasError\]: (Unauthorized) not authorized on admin to execute command { insert: "posts", $db: "admin", ordered: true, writeConcern: { w: "majority" }, txnNumber: 1.000000, lsid: { id: {4 \[235 127 224 68 69 252 76 207 177 72 92 1 135 84 190 63\]} } } (on ac-xjgsx9t-shard-00-02.whzyr5q.mongodb.net:27017, modern retry, attempt 1) (Mongo::Error::OperationFailure)

これは私の mongoid.yml です。

development:
  clients:
    default:
      uri: mongodb+srv://hiddenuser:[email protected]/?retryWrites=true&w=majority
      options:
        server_selection_timeout: 5

なぜ機能しないのかわかりません…

私はすべてを試しました…古いバージョンのMMRで試しました(対応するドキュメントに従って)。何も機能しません。

Atlas 上にクラスターを作成し、IP を認証し、ユーザーがすべての権限を持っていることを確認しました…ステータス atlasAdmin を持つ別のユーザーも追加しました…

間違ったパスワードを入力すると、次のエラーが表示されます。

Mongo::Auth::Unauthorized in PostsController#create
User admin (mechanism: scram) is not authorized to access admin (auth source: admin, used mechanism: SCRAM-SHA-1, used server: ac-xjgsx9t-shard-00-02.whzyr5q.mongodb.net:27017 (PRIMARY)): [8000:AtlasError]: bad auth : authentication failed (modern retry, attempt 1)

つまり、私のパスワードは最初から正しいのです…

何か見逃したものはありますか?最初に Atlas でコレクションを作成する必要がありますか?

ご協力いただきありがとうございます!

問題が解決しました

間違いはここから来ました:

mongodb+srv://hiddenuser:[email protected]/?retryWrites=true&w=majority

?retry の前にコレクションの名前を置く必要があります。

mongodb+srv://hiddenuser:[email protected]/NAMEOFYOURCOLLECTION?retryWrites=true&w=majority

解決策