How to Set Up MySQL Database on Linux Server
Complete guide on installing, securing, and managing MySQL database server on Ubuntu and CentOS.

MySQL is one of the most popular open-source relational database management systems. This guide will help you install, secure, and configure MySQL on your Hiddence Linux server.
Installing MySQL
MySQL 8.0 is the current stable version as of 2026. The installation process varies slightly between Ubuntu/Debian and RHEL-based distributions. We'll cover both methods below.
Installing MySQL on Ubuntu/Debian
For Ubuntu 22.04 LTS and newer, MySQL 8.0 is available in the default repositories. Install it using apt:
sudo apt update
sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql
# Verify installation
sudo systemctl status mysql
mysql --versionInstalling MySQL on RHEL / CentOS / AlmaLinux / Rocky Linux
For RHEL-based distributions, you need to add the MySQL repository first. MySQL 8.0 is recommended for production use:
# Download MySQL repository package
sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
# Install repository
sudo rpm -ivh mysql80-community-release-el9-1.noarch.rpm
# For CentOS 8/Rocky Linux 8/AlmaLinux 8, use:
# sudo wget https://dev.mysql.com/get/mysql80-community-release-el8-7.noarch.rpm
# Install MySQL
sudo yum install mysql-server -y
# Start and enable MySQL
sudo systemctl start mysqld
sudo systemctl enable mysqld
# Verify installation
sudo systemctl status mysqld
mysql --versionVerifying Installation
After installation, verify that MySQL is running correctly:
# Check service status
sudo systemctl status mysql # Ubuntu/Debian
sudo systemctl status mysqld # RHEL / CentOS
# Check MySQL version
mysql --version
# Test connection (will prompt for root password)
sudo mysql -u root -pSecuring MySQL Installation
Run MySQL secure installation script:
sudo mysql_secure_installation
# Follow prompts to set root password, remove anonymous users, disable remote root login, etc.Creating Database and User
sudo mysql -u root -p
CREATE DATABASE myapp_db;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;Managing Users
sudo mysql -u root -p
# Create user
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
# Grant privileges
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';
# Revoke privileges
REVOKE ALL PRIVILEGES ON database_name.* FROM 'newuser'@'localhost';
# Delete user
DROP USER 'newuser'@'localhost';Enabling Remote Access (Optional)
To allow remote connections, modify bind-address:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
# Change bind-address = 127.0.0.1 to bind-address = 0.0.0.0
sudo systemctl restart mysql
# Also configure firewall to allow port 3306Database Backup and Restore
Create backup and restore:
# Backup
mysqldump -u root -p database_name > backup.sql
# Restore
mysql -u root -p database_name < backup.sqlBest Practices
- Use strong passwords for database users
- Grant only necessary privileges to users
- Regularly backup your databases
- Keep MySQL updated for security patches
- Monitor database performance and optimize queries