brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: mariadb-dump with the databases option writes CREATE DATABASE into the dump for you

Database
~/tips/tip-database-mariadb-dump-databases-create

mariadb-dump --databases shop blog > shop-blog.sql

Makes a dump that creates and selects its own database when you restore it.

A plain mariadb-dump shop only holds tables. Feed it to a fresh server and you first have to create the database by hand and name it on the command line.

With --databases the dump carries that part itself:

mariadb-dump --databases shop blog > shop-blog.sql
grep -E '^(CREATE DATABASE|USE)' shop-blog.sql

The grep shows a CREATE DATABASE IF NOT EXISTS `shop` and a USE `shop`; line before each database’s tables. Restoring is then one command with no database name:

mariadb < shop-blog.sql

I like it even for a single database, because the file says where it belongs. Without the flag, every name after the first one is read as a table name, so mariadb-dump shop blog means “table blog in database shop”, which is a confusing error to get at 2 a.m.

That self-contained style has a downside when you want to restore into a different name, for example shop_test next to the real one. The USE line sends everything straight back into shop. For a copy under another name, dump without --databases and pass the target name to mariadb yourself. mysqldump on MySQL has the same option.

Further reading: mariadb-dump documentation

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link