LINUX, FOSS AND LIBRARY TECHNOLOGY ENTHUSIAST

Saturday, November 18, 2023

Automating MySQL/MariaDB Query Cache Clearance and BleachBit

0 comments
Maintaining a robust and efficient Linux system involves periodic tasks such as clearing the MySQL/MariaDB query cache and optimizing disk space. In this blog post, we'll guide you through the process of creating a bash script, setting up cron jobs, and integrating BleachBit to automate these essential maintenance routines. By the end of this guide, you'll have a seamlessly automated system that ensures optimal performance and disk space management.

Creating the Bash Script:

Let's start by crafting a bash script to handle the MySQL query cache clearance. Open your preferred text editor and create a new file named `clear_query_cache.sh`:

sudo vim /usr/local/bin/clear_query_cache.sh

Insert the following content into the script:

#!/bin/bash

# MySQL connection parameters
MYSQL_USER="root"
MYSQL_PASSWORD="mysqlroot"
MYSQL_HOST="localhost"

# Clear the query cache
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e "RESET QUERY CACHE;"

Remember to replace "root" "mysqlroot" and "localhost" with your actual MySQL username, password, and host.

Making the Script Executable:

Save the script and make it executable by running the following command in your terminal:

sudo chmod +x /usr/local/bin/clear_query_cache.sh 

Configuring Cron Jobs:

Now, let's set up a cron job to automate the execution of the script at regular intervals. Open your crontab file for editing:

crontab -e

Add the following cron job entry to schedule the script to run every day at midnight:

# Run the script every day at midnight
0 0 * * * /usr/local/bin/clear_query_cache.sh

Automating System Cleanup with BleachBit:

BleachBit is a powerful disk space cleaner and system optimizer. Install it using the following command:

sudo apt install bleachbit

Now, configure a cron job to run BleachBit every day at 3 AM:

crontab -e

# Run BleachBit every day at 3 AM
0 3 * * * /usr/bin/bleachbit --clean all

You've created a robust Linux system maintenance routine by automating MySQL query cache clearance and using BleachBit for overall system cleanup. Scheduled via cron jobs, this streamlined process optimizes your MySQL database and enhances system performance, freeing you to focus on critical administration tasks.

NB: Adjust your cron time

References: 

No comments:

Post a Comment