Databases 7 min read

Automating MySQL Database Testing with Shell Scripts

This article explains how to use Bash shell scripts on Linux to automate MySQL database testing tasks such as creating tables, inserting, updating, and monitoring dynamic database states, providing step‑by‑step code examples and execution instructions for efficient test automation.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Automating MySQL Database Testing with Shell Scripts

Automation testing has become essential in the testing industry, and using simple, readily available tools like Bash shell scripts can effectively automate database testing tasks without requiring heavyweight frameworks.

In the context of the Qiyun project, many forms need to be generated and database dynamics must be monitored, prompting the use of shell scripts to interact with MySQL on Linux.

Shell scripting, particularly with bash , allows developers to write executable scripts; a script must start with the shebang line #!/bin/sh and can be edited with any text editor such as vi or vim .

After writing the script, grant execution permission with chmod +x filename and run it using /filename (or sh filename ).

The provided script performs the following operations:

1. Creates a table temp1 in MySQL.

2. Inserts multiple rows in a loop, updates, replaces, and selects data to simulate typical CRUD operations.

3. Creates temporary tables and executes a SELECT SLEEP(3) to simulate slow queries.

4. Executes the script with sh testtemp1.sh and monitors the resulting data.

Example code snippets (wrapped in tags) are:

#!/bin/sh

echo "Run the test script of mysql!"

for i in {1..1000}; do

mysql -h192.168.0.153 -P3306 -usuyujun -pSuyujun123456 -e "

use yujun;

insert into temp1 values($i,'Tom','dkdkkdkdkdksldfk',123456789);

insert into temp1 values($i+1,'Tomssl','dkdkkdkdkdksldfk',123456789);

insert into temp1 values($i+2,'Tomieie','dkdkkdkdkdksldfk',123456789);

insert into temp1 values($i+3,'Tom333','dkdkkdkdkdksldfk',123456789);

select * from temp1 where id=$i+3;

replace into temp1(id,name,info,mobile) values ($i,'Yujun','124324dsdfs',48484848);

update temp1 set name='Suyujun' where id=$i;

select * from temp1 where name='Yujun';

select sleep(3);

CREATE TEMPORARY TABLE tmp_table1 (id int,name varchar(10)) select * from temp;

quit;"

done

Running the script generates monitoring charts that reflect the database activity, demonstrating the effectiveness of shell‑based automation for both CRUD verification and dynamic performance monitoring.

In conclusion, automated testing does not require complex frameworks; leveraging built‑in tools like Bash scripts can achieve efficient database testing, and the full script is provided for readers to adapt to similar scenarios.

SQLautomationlinuxmysqlshell scriptingDatabase Testing
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login 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.