Install MySQL/MariaDB and Secure It

Debian: Install MySQL/MariaDB and Secure It

Databases are essential for dynamic websites and applications. Debian supports both MySQL and MariaDB, two of the most popular relational database systems. This guide explains how to install and secure them on HostPalace servers.


Step 1: Update Your Server

sudo apt update && sudo apt upgrade -y


Step 2: Install MySQL Server

sudo apt install mysql-server -y

Enable MySQL at boot:

sudo systemctl enable mysql

Check status:

sudo systemctl status mysql


Step 3: Install MariaDB (Alternative)

sudo apt install mariadb-server -y

Enable MariaDB at boot:

sudo systemctl enable mariadb

Check status:

sudo systemctl status mariadb


Step 4: Secure Database Installation

Run the security script:

sudo mysql_secure_installation

This allows you to:

  • Set a root password
  • Remove anonymous users
  • Disallow remote root login
  • Remove test databases

Step 5: Connect to Database

mysql -u root -p

Enter the root password to access the MySQL/MariaDB shell.


Step 6: Create a Database and User

CREATE DATABASE mydb;

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';

GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';

FLUSH PRIVILEGES;


Best Practices

  • Always run mysql_secure_installation after setup
  • Use strong passwords for database users
  • Restrict remote access unless necessary
  • Regularly back up databases

Note: HostPalace Debian servers are optimized for MySQL and MariaDB, ensuring secure and reliable database hosting for client applications.