MySQLMariaDBDatabase6 min read

Install MySQL/MariaDB

Install MySQL or MariaDB, secure the installation, and create databases and users for your apps.


MySQL and MariaDB are the most popular relational databases for web applications. This guide covers installation and initial security setup.

Option A — Install MySQL 8

bash
sudo apt update
sudo apt install mysql-server -y

Option B — Install MariaDB

bash
sudo apt update
sudo apt install mariadb-server -y

Step 2 — Secure the installation

Run the security script:

bash
sudo mysql_secure_installation

Recommended answers:

  • Set root password: Yes
  • Remove anonymous users: Yes
  • Disallow root login remotely: Yes
  • Remove test database: Yes
  • Reload privilege tables: Yes

Step 3 — Access the MySQL console

bash
sudo mysql -u root -p

Step 4 — Create a database and user

sql
CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'SecurePassword123!';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 5 — Verify the connection

bash
mysql -u myapp_user -p myapp_db

Enable remote access (optional)

Edit the configuration:

bash
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Change:

terminal
bind-address = 0.0.0.0

Restart and allow through firewall:

bash
sudo systemctl restart mysql
sudo ufw allow 3306/tcp

Management commands

bash
sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl restart mysql
sudo systemctl status mysql

Performance tip

For production, adjust innodb_buffer_pool_size in the MySQL config to approximately 70% of available RAM for database-heavy workloads.


Was this guide helpful?