Menu
in ,

Optimize WordPress Database Daily Using Linux Crontab

Optimize WordPress Database

Optimize WordPress Database is an important task because we need to make sure our web application keeps smoothly running.

But not every task needed for a WordPress website will do using a free or commercial plugin, we love automatic tasks, but we need to keep our website light too.

So every task you can do without adding extra code or plugins to your application will be the better thing to do.

Here is a simple crontab command to do this Optimization task daily, and run automatically for you using  crontab  .

Optimize WordPress Database Using Linux Crontab

Midnight is the best time for most websites to do this optimization task. but you can adjust your preferred time using crontab.guru.

We can add a new cron task to the crontab command uses

# crontab -e

And append the following mysqlcheck maintenance command-line tool at the end.

0 0 * * * /usr/bin/mysqlcheck -uUSER -pPASSWORD --auto-repair --optimize DataBaseName > /dev/null 2>&1

Replace the DataBaseName, USER, and PASSWORD with your database and user credentials.

But If you have root credentials and want to optimize all databases you can pass --all-databases flag.

0 0 * * * /usr/bin/mysqlcheck -uUSER -pPASSWORD --auto-repair --optimize --all-databases > /dev/null 2>&1

And if you want to optimize specific databases you can pass the  --databases   flag with the database's name.

0 0 * * * /usr/bin/mysqlcheck -uUSER -pPASSWORD --auto-repair --optimize --databases DB1 DB2 DB3  > /dev/null 2>&1

If you using a remote database server and it is not localhost, you will need to pass -h with the Hostname or IP-Address of the database server.

And so the mysqlcheck can be installed by installing the mariadb-client or mysql-client package

# apt install mariadb-client

Leave a Reply

Exit mobile version