Find us on social media
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 -yOption B — Install MariaDB
bash
sudo apt update
sudo apt install mariadb-server -yStep 2 — Secure the installation
Run the security script:
bash
sudo mysql_secure_installationRecommended 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 -pStep 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_dbEnable remote access (optional)
Edit the configuration:
bash
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnfChange:
terminal
bind-address = 0.0.0.0Restart and allow through firewall:
bash
sudo systemctl restart mysql
sudo ufw allow 3306/tcpManagement commands
bash
sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl restart mysql
sudo systemctl status mysqlPerformance 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?