Docker-compose and Unknown MySQL server host

In case you encounter this error with mysql and your docker-compose setup:

Unknown MySQL server host <mysql-service-name>
Code language: HTML, XML (xml)

… and you dont know why because everything seems to be correct.

Then you might have an upgrade problem with mysql because you are reusing an old volume that was created for another mysql version.
This can happen when you use a unspecified tag as mysql:latest (not recommended anyway) and there was a version bump in the official mysql image.
Or you upgraded the mysql container yourself, f.e. from mysql:5.6 to mysql:5.7 and you are reusing the data volume with the mysql files.

When you check the startup logs you might see that mysql container is shutting down after startup because of an error like this:

Can't open the mysql.plugin table. Please run mysql_upgrade to create it.

You can run the upgrade command in the container:

docker exec -it mysql_container_name bash -c "mysql_upgrade -uroot -proot"
Code language: JavaScript (javascript)

Or simply delete the volume and recreate it.
Note: the data will unfortunatly be lost. So better dump the data before deleting the volume and reimport it.
In my case its no problem because im starting a new project anyway.

To delete the volume you can:

docker volume rm <volume id>
Code language: HTML, XML (xml)

Or just delete the data dir:

rm -rf ./data/mysql

Then restart your setup so the mysql container recreates the databases:

docker-compose up

and mysql is working again and the application is able to connect. :)