見出し画像

Prisma Migration のエラー (Mysql & Docker使用) 解決済み

エラー

schema.prismaを書き終え、”npx prisma migrate dev --name init” でmigrateしようとしたときにエラーが出た

Prisma Migrate could not create the shadow database. 
Please make sure the database user has permission to create databases. 


原因

作成したUserに、 Shadow databaseの権限を与えていなかった、、、

Shadow databaseに権限が必要であることに気付けず、ずっとmysqlのデータベースへの権限を確認しては、権限あるじゃんとなっていた。(エラー読めばわかるのに、、、馬鹿だ、、、)

参考にしたサイト

Prismaの導入

error 解決に使用

MYSQL_DATABASE

This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.

https://hub.docker.com/_/mysql

MYSQL_USER, MYSQL_PASSWORDを設定して作成した userは、MySQLのデータベースのへの権限はすべてもっていることが分かる


Shadow database user permissions

In order to create and delete the shadow database when using migrate dev, Prisma Migrate currently requires that the database user defined in your datasource has permission to create databases.

https://www.prisma.io/docs/concepts/components/prisma-migrate/shadow-database

Shadow databaseへの権限も必要だとわかる


解決策

作成した userではなく、全ての権限をもつrootユーザーに切り変えた。

docker-compose.ymlにあるMYSQL_USERとMYSQL_PASSWORDを削除し、代わりにMYSQL_ROOT_PASSWORD を指定して、docker-compose.ymlを再度立て直すとできました。

作成した userに権限を与える場合は、たぶん prisma の APIが必要になるのかな…?ちょっとめんどくさいので、rootユーザーに切り替えました。


この記事が気に入ったらサポートをしてみませんか?