How to install MySQL or MariaDB on Linux
Topic: Databases core
Summary
Install MySQL 8 or MariaDB on Debian, Ubuntu, or RHEL using the official or distro repository. Secure the installation with mysql_secure_installation; create a database and user. Use this when setting up a new database server or when you need MySQL-compatible storage.
Intent: How-to
Quick answer
- Debian/Ubuntu: apt install mysql-server or mariadb-server. RHEL: dnf install mysql-server or mariadb. Start and enable: systemctl start mysql; systemctl enable mysql (or mariadb).
- Run mysql_secure_installation: set root password, remove anonymous users, disallow root login remotely, remove test DB. Connect: mysql -u root -p. Create user and DB: CREATE USER 'app'@'localhost' IDENTIFIED BY 'x'; CREATE DATABASE mydb; GRANT ALL ON mydb.* TO 'app'@'localhost';
- Config: /etc/mysql/mysql.conf.d/mysqld.cnf (Debian) or /etc/my.cnf (RHEL). Set bind-address for remote access; use SSL and restrict users by host. Restart after config change.
Steps
-
Install and start
apt install mysql-server (or mariadb-server); systemctl start mysql; systemctl enable mysql. RHEL: dnf install mysql-server; systemctl start mysqld; systemctl enable mysqld.
-
Secure installation
mysql_secure_installation: set root password, remove anonymous users, remove remote root login, remove test database, reload privileges.
-
Create database and user
mysql -u root -p. CREATE DATABASE mydb; CREATE USER 'app'@'localhost' IDENTIFIED BY 'secret'; GRANT ALL PRIVILEGES ON mydb.* TO 'app'@'localhost'; FLUSH PRIVILEGES;
-
Configure for remote (optional)
Set bind-address in config; add user 'app'@'%' or 'app'@'10.0.0.%' with GRANT; use SSL. Restart MySQL. Restrict to required IPs; avoid 'app'@'%' with weak password.
Summary
Install MySQL or MariaDB from the distro or official repo; run mysql_secure_installation; create a database and user. Use this when standing up a new MySQL-compatible server.
Prerequisites
- Root or sudo on a Linux host.
Steps
Step 1: Install and start
Install the server package; start and enable the service.
Step 2: Secure installation
Run mysql_secure_installation; set root password and remove insecure defaults.
Step 3: Create database and user
Create a database and a user with limited host and privileges; grant access to the database.
Step 4: Configure for remote (optional)
Set bind-address and create remote user with restricted host; use SSL in production.
Verification
- Service is running; root can connect with password; app user can connect and use the database.
Troubleshooting
Access denied — Check user host (localhost vs %); check password; FLUSH PRIVILEGES. Cannot connect remotely — bind-address must allow remote; firewall and user host must allow the client.