Databases 3 min read

Automate MySQL Monitoring with Shell Scripts and Cron: A Step‑by‑Step Guide

Learn how to use shell scripting combined with MySQL commands and cron jobs to create, query, update, and delete database records automatically for routine performance monitoring and system administration tasks.

ITPUB
ITPUB
ITPUB
Automate MySQL Monitoring with Shell Scripts and Cron: A Step‑by‑Step Guide

When performing routine operations and performance monitoring, shell scripts can be leveraged to interact with MySQL databases without entering the MySQL prompt. This guide shows how to configure a Bash script and schedule it with crontab to manage database objects.

Setup Variables

#!/bin/bash
HOSTNAME="192.168.111.84"   # database host
PORT="3306"
USERNAME="root"
PASSWORD="your_password"
DBNAME="test_db_test"        # database name
TABLENAME="test_table_test"   # table name

Create Database and Table

# Create database
create_db_sql="create database IF NOT EXISTS ${DBNAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"

# Create table
create_table_sql="create table IF NOT EXISTS ${TABLENAME} ( name varchar(20), id int(11) default 0 )"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${create_table_sql}"

Insert, Query, Update, and Delete Data

# Insert data
insert_sql="insert into ${TABLENAME} values('billchen',2)"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${insert_sql}"

# Query data
select_sql="select * from ${TABLENAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"

# Update data
update_sql="update ${TABLENAME} set id=3"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${update_sql}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"

# Delete data
delete_sql="delete from ${TABLENAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${delete_sql}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"

By placing the above script in a file and adding an entry to crontab, the commands can run automatically at desired intervals, providing continuous monitoring and management of MySQL data without manual intervention.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

monitoringAutomationShell
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.