mysql mysql-connector spring-boot spring-data-jpa

How to calculate free space in a MySQL datasource deployed dynamically via docker

I have a Spring Boot server that’s connected to a MySQL server running on a separate docker image. As such I can’t do something as simple as a df since the image is wired via a URI in the docker configuration and may change in production deployments. E.g. we might switch the DB to use clustering etc.

However, we might run out of space in the DB and need to detect that. Since there’s a lot of data we can delete when running out of space this is pretty crucial.

There are a lot of answers related to calculating the amount of space used by the DB, that isn’t what I want since:

  • It’s expensive in terms of CPU and I’d like to keep the implementation efficient
  • I don’t care how much space is taken, only how much is left, I can keep a variable of “total space” and use that to calculate but it would ignore potential increases in the disk size

I found the data_free query here, but it seems problematic based on the following answer.

Is there another way to calculate the disk size either via mySQL query or via similar API exposed through spring boot?