I'm using Laravel with Sail/Docker, on MacOs.
I'd like to change my DB user and password, just to try and test.
If I change them in .env
config (sail2/password2), then shut down sail and then sail up -d
(or even completely rebuild the containers), I see that:
Laravel sees the new credentials (sail2), since if I tinker this:
DB::select("select * from users");
It's going to complain like this:
SQLSTATE[HY000] [1045] Access denied for user 'sail2'@'172.20.0.7'
So Laravel reads the new credentials from the environment variables, but the user has no access to the database.
- Docker itself does sees the new environment varialbes, since by inspecting the mysql container I can see this:
Therefore, what I can say is that it all works, except the fact that the new user seems not to be assigned to the current database.
Please note I also tried to connect with Sequel Ace and Table Plus, but doesn't work. Instead, they will connect with the original credentials (sail/password).
You cannot use the
MYSQL_*
environment variables to change the credentials of an existing database.Laravel Sail is just a wrapper, and underneath it simply uses the MySQL official docker image (or the MariaDB one, maybe, but it's the same to these effects).
From the readme on that image:
(emphasis mine)
If you are reusing an existing data directory by mounting it within the image, even rebuilding the image from scratch will end you up with an existing database, and the environment variables will do nothing.
To change the credentials (or add new credentials), you need to use the regular methods (e.g.
GRANT
statements, executed via your client of choice).