Installing and Securing MySQL on Ubuntu Server

Installing MySQL on Ubuntu is simple, but the default installation leaves a few things unsecured that are worth fixing immediately, not eventually.

sudo apt update
sudo apt install mysql-server

# Run the security wizard: sets root password, removes test DB,
# disables remote root login, removes anonymous users
sudo mysql_secure_installation

sudo systemctl enable --now mysql
sudo systemctl status mysql
-- Log in and create a database + scoped user for your application
sudo mysql

CREATE DATABASE myapp;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong-password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;

Never skip mysql_secure_installation — a fresh MySQL install without it can be left with an unauthenticated root account or test database accessible to anyone, both classic and entirely avoidable security holes.