I
I
Ilya2021-07-14 14:00:23
linux
Ilya, 2021-07-14 14:00:23

How to delete MYSQL databases that are more than 30 days old?

There is a mysql server5.6 on it, databases for tests are often created, but the creator does not always delete the database, so it is necessary to delete all databases that are older than 30 days. How to at least list all databases that are older than 30 days, based on the date of the oldest table in the potential database.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2021-07-14
@xby43

list of oldest tables from all databases

SELECT TABLE_SCHEMA, TABLE_NAME, MAX(CREATE_TIME) created FROM information_schema.tables
GROUP BY TABLE_SCHEMA
ORDER BY created

those older than a month:
SELECT TABLE_SCHEMA, TABLE_NAME, MAX(CREATE_TIME) created FROM information_schema.tables
GROUP BY TABLE_SCHEMA
HAVING created < (NOW() - INTERVAL 1 MONTH)
ORDER BY created

it remains to add DROP DATABASE;
And try not to accidentally kill any of the system tables.

B
BorLaze, 2021-07-14
@BorLaze

In fact, it's better to use some kind of embedded mysql for tests. Or run docker.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question