Install and Secure MariaDB
This guide explains how to install and securely configure MariaDB on your VPS.
Prerequisites
- SSH root or sudo access
- Up-to-date Debian/Ubuntu system
Installation
-
Install MariaDB:
sudo apt update
sudo apt install mariadb-server -
Secure the installation:
sudo mysql_secure_installationFollow the security steps:
- Press Enter for the root password (empty by default)
- Answer security questions:
- Change the root password? [Y/n] :
Y(recommended) - Remove anonymous users? [Y/n] :
Y - Disallow root login remotely? [Y/n] :
Y - Remove test database? [Y/n] :
Y - Reload privilege tables? [Y/n] :
Y
- Change the root password? [Y/n] :
User configuration
-
Connect to MariaDB:
sudo mysql -
Create a new administrator user (replace 'your_user' and 'your_password'):
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'your_user'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
Installation verification
-
Test the connection with the new user:
mysql -u your_user -p -
Check the service status:
sudo systemctl status mariadb
Securing MariaDB port
-
Check the current listening port:
sudo netstat -tlnp | grep mysql -
Configure the firewall (UFW):
# Block default access to MySQL port (3306)
sudo ufw deny 3306
# If you need to allow remote access from a specific IP
sudo ufw allow from AUTHORIZED_IP to any port 3306 -
Modify the MariaDB configuration file:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnfFind and modify these lines:
# Limit listening to localhost only
bind-address = 127.0.0.1
# Or specify a specific IP address
# bind-address = YOUR_SERVER_IP -
Restart MariaDB to apply changes:
sudo systemctl restart mariadb -
Check authorized connections:
mysql -u root -p
SELECT user, host FROM mysql.user;
Security best practices
- Use strong passwords
- Limit database access
- Perform regular backups
- Regularly update MariaDB
- Limit connections to trusted IPs
- Regularly monitor connection attempts
Troubleshooting
- Check logs:
sudo tail -f /var/log/mysql/error.log - Verify that the service is active:
sudo systemctl status mariadb - Make sure ports are open:
sudo netstat -tulpn | grep mysql