連接您的資料庫 (MongoDB)
若要連接您的資料庫,您需要在 Prisma Schema 中將 datasource
區塊的 url
欄位設定為您的資料庫連線網址
prisma/schema.prisma
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
在此範例中,url
是透過環境變數設定,該變數定義於 .env
中 (此範例使用 MongoDB Atlas 網址)
.env
DATABASE_URL="mongodb+srv://test:test@cluster0.ns1yp.mongodb.net/myFirstDatabase"
您現在需要調整連線網址以指向您自己的資料庫。
您的資料庫連線網址格式取決於您使用的資料庫。對於 MongoDB,格式如下 (全部大寫的部分是您特定連線詳細資訊的佔位符)
mongodb://USERNAME:PASSWORD@HOST:PORT/DATABASE
以下是每個組件的簡短說明
USERNAME
:您的資料庫使用者名稱PASSWORD
:您的資料庫使用者密碼HOST
:mongod
(或mongos
) 執行個體運行的主機PORT
:您的資料庫伺服器運行的端口 (MongoDB 通常為27017
)DATABASE
:資料庫名稱。請注意,如果您使用 MongoDB Atlas,您需要手動將資料庫名稱附加到連線網址,因為來自 MongoDB Atlas 的環境連結不包含資料庫名稱。
疑難排解
Error in connector: SCRAM failure: Authentication failed.
如果您看到 Error in connector: SCRAM failure: Authentication failed.
錯誤訊息,您可以透過在連線字串末尾新增 ?authSource=admin
來指定身份驗證的來源資料庫。
Raw query failed. Error code 8000 (AtlasError): empty database name not allowed.
如果您看到 Raw query failed. Code: unknown. Message: Kind: Command failed: Error code 8000 (AtlasError): empty database name not allowed.
錯誤訊息,請務必將資料庫名稱附加到資料庫網址。您可以在這個 GitHub issue 中找到更多資訊。