Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Statement

Open the client as an administrator, with sudo mysql on Ubuntu, sudo mariadb on MariaDB, or mysql -u root -p with the password typed at the prompt. Then:

CREATE DATABASE app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

The database is created at once and is empty. CREATE SCHEMA is the same statement under another name.

Character Set and Collation

  • utf8mb4: full Unicode, including emoji and all scripts. Use it for every new database. The older utf8 (an alias for utf8mb3) stores at most three bytes per character and cannot hold all of Unicode.
  • Collation: the rules for sorting and comparing text. utf8mb4_unicode_ci works on both MySQL and MariaDB. MySQL 8.0 and later use utf8mb4_0900_ai_ci as the default for utf8mb4, which MariaDB does not have.
  • Name it explicitly: the server default depends on the version and on the distribution's configuration, so writing both values gives the same result everywhere.

Check what a database got:

SHOW CREATE DATABASE app;

ALTER DATABASE app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; changes the default for tables created later; existing tables keep their own settings.

Only If It Does Not Exist

CREATE DATABASE IF NOT EXISTS app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Without IF NOT EXISTS, a second run fails with error 1007, database exists. With it, the statement gives a warning and changes nothing, which suits scripts. Note that it does not alter an existing database to match.

Naming Rules

  • Use letters, digits, and underscores: shop_db works anywhere without quoting.
  • Avoid hyphens and spaces: a name such as my-app must be written in backticks, as `my-app`, in every query.
  • Treat names as case-sensitive: on Linux, database names map to directories, and App and app are different databases.

Give One User Access

CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON app.* TO 'app_user'@'localhost';

The account works in app only and connects from the server itself only. Type the real password inside the SQL shell, never on a command line. Narrower grants and how to check them are in how to create a MySQL user.

From the Shell

sudo mysqladmin create app
sudo mysql -e "CREATE DATABASE app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

mysqladmin create uses the server's default character set, so the second form is the more predictable one. On MariaDB the tool is also called mariadb-admin. To confirm the database is there, see how to list databases in MySQL.

Load Existing Data

mysql -u app_user -p app < app.sql

The dump is read into the new database. If the dump contains its own CREATE DATABASE and USE lines, it may load into a different name; look at the first lines of the file with head before you run it.

Dropping a Database

DROP DATABASE removes the database and every table in it immediately. There is no confirmation and no undo. Before you run it, take a dump and check that the file is complete:

sudo mysqldump --single-transaction app > app-before-drop.sql
DROP DATABASE app;

The first line runs in the shell, the second inside the client. On MariaDB the dump tool is mariadb-dump. Also check whether any application still uses the database. A server snapshot is a second safety net, as described in snapshot vs backup.

Was this answer helpful?
Loading...