SoFunction
Updated on 2025-03-09

Methods to batch insert data in mysql table using shell script

Many times, you need to insert a large amount of test data into the mysql table. Here is a method to use a shell script to batch generate mysql test data through a while loop. You only need to generate SQL statements based on your own table structure.

Copy the codeThe code is as follows:

#!/bin/bash

i=1;
MAX_INSERT_ROW_COUNT=$1;
while [ $i -le $MAX_INSERT_ROW_COUNT ]
do
    mysql -uroot -proot afs -e "insert into afs_test (name,age,createTime) values ('HELLO$i',$i % 99,NOW());"
    d=$(date +%M-%d\ %H\:%m\:%S)
    echo "INSERT HELLO $i @@ $d"   
    i=$(($i+1))
    sleep 0.05
done

exit 0


Assuming the above shell script is saved as, the following commands can be used to generate data:
Copy the codeThe code is as follows:
sh 10000

*Note: Parameter 10000 is the number of data strips to be generated.