---
title: "Working with databases"
description: "You need to create the databases first. Copy the file names and make a little script which creates them in one go."
canonical_url: "https://codex.republic.se/development/articles/working-with-databases"
section: "Pages"
---

# Working with databases

---

## Export all databases to separate files

```bash
$ mysql -N -e 'show databases' | while read dbname; do mysqldump --complete-insert --routines --triggers --single-transaction "$dbname" > "$dbname".sql; done

```

---

## **Import all databases from files**

You need to create the databases first. Copy the file names and make a little script which creates them in one go.

```sql
CREATE DATABASE IF NOT EXISTS customer_a;
CREATE DATABASE IF NOT EXISTS customer_b;
CREATE DATABASE IF NOT EXISTS customer_c;
CREATE DATABASE IF NOT EXISTS customer_d;
CREATE DATABASE IF NOT EXISTS customer_e;
...

```

```bash
$ for sql in *.sql; do dbname=${sql/\.sql/}; echo -n "Now importing $dbname ... "; mysql $dbname < $sql; echo " done."; done

```

---

## Resources

 ![](https://www.google.com/s2/favicons?domain=https://ma.ttias.be/mysql-back-up-take-a-mysqldump-with-each-database-in-its-own-sql-file/&sz=128) [MySQL Back-up: Take a mysqldump with each database in its own SQL File](https://ma.ttias.be/mysql-back-up-take-a-mysqldump-with-each-database-in-its-own-sql-file/) // 2025-06-12
