Resetting a MySQL database means dropping the database, recreating it, and reloading the schema—returning it to a clean state.
Resetting deletes the existing database, recreates it with the same name, and reloads the schema and seed data. All prior data is wiped, giving you a fresh environment.
Use resets for automated test runs, QA refreshes, or major schema overhauls. Avoid in production unless you have an airtight backup and a maintenance window.
Run mysqldump -u admin -p ecommerce > ecommerce_$(date +%F).sql
. Store the dump in version control or encrypted cloud storage for quick rollback.
DROP DATABASE [IF EXISTS] db_name;
removes all objects. CREATE DATABASE db_name [DEFAULT CHARACTER SET charset] [DEFAULT COLLATE collation]
recreates it. Reload the schema with mysql -u admin -p db_name < schema.sql
.
1. Terminate active sessions (SHOW PROCESSLIST
). 2. Run DROP DATABASE. 3. Run CREATE DATABASE. 4. Re-import schema/seed data. 5. Reapply GRANTs if needed.
-- Disconnect users
SELECT CONCAT('KILL ',id,';') AS kill_cmd FROM information_schema.processlist WHERE db='ecommerce';
-- Drop and recreate
drop database if exists ecommerce;
create database ecommerce;
-- Reload schema and samples
source /var/backups/ecommerce_schema.sql;
source /var/backups/sample_seed.sql;
Automate dumps before every reset, version your schema files, and run resets only from CI/CD or dedicated admin hosts. Tag dumps with Git SHA for traceability.
The executing user needs DROP
, CREATE
, and FILE
privileges plus RELOAD
if using FLUSH commands.
Time equals drop + create + import. Small schemas finish in seconds; multi-GB imports may take minutes. Optimize imports with --single-transaction
and parallel loads.
Yes. mysql -u admin -p -e "DROP DATABASE IF EXISTS ecommerce; CREATE DATABASE ecommerce;"
runs both commands in one call. Reload the schema afterward.
Database resets delete only objects inside the database. MySQL user accounts live in the mysql
system database and stay intact.
Absolutely. Add a script step that runs the DROP/CREATE commands and imports a schema dump, ensuring each test run starts from a clean slate.