- 1 Fastest Way to Install MariaDB on Ubuntu (20.04 / 22.04 / 24.04 Supported)
- 1.1 ① Update Package Information
- 1.2 ② Install MariaDB
- 1.3 ③ Verify That MariaDB Is Running
- 1.4 ④ Enable Auto-Start (Usually Not Required but Recommended to Confirm)
- 1.5 ⑤ Connect to MariaDB and Verify Operation
- 1.6 Mandatory Security Initialization After Installation
- 1.7 MariaDB Version Installed by Ubuntu Version
- 1.8 Quick Checks If Installation Fails or Service Does Not Start
- 2 Choosing the Right MariaDB Version for Your Ubuntu Version
- 3 If You Need the Latest Version (Add the Official MariaDB Repository)
- 4 How to Check the Currently Installed MariaDB Version
- 5 Complete Guide to Initial MariaDB Security Setup (mysql_secure_installation)
- 6 Why You Cannot Log In as root (Access Denied Error)
- 7 How to Change root to Password Authentication (Only If Necessary)
- 8 How to Check the Current Authentication Method
- 9 Common Errors and Solutions
- 10 Set Character Encoding to utf8mb4 (Prevent Garbled Text)
- 11 Specify utf8mb4 When Creating a Database (Recommended)
- 12 Create an Application User and Grant Permissions
- 13 Create a Remote Access User (Only If Needed)
- 14 How to Check Current Users and Privileges
- 15 Common Permission Errors and Solutions
- 16 How to Allow Remote Connections in MariaDB (Fix When External Access Fails)
- 17 Checklist If You Cannot Connect Externally
- 18 Important Security Notes
- 19 Troubleshooting When MariaDB Fails to Start (failed Error)
- 20 Summary of Access Denied Error Causes
- 21 Basic Performance Improvements (For Beginners)
- 22 Frequently Asked Questions (FAQ)
- 23 Conclusion: Using MariaDB Safely on Ubuntu
Fastest Way to Install MariaDB on Ubuntu (20.04 / 22.04 / 24.04 Supported)
If you want to start using MariaDB on Ubuntu immediately, simply follow the steps below to complete installation and startup verification. Unless you have a specific reason, installing from the Ubuntu default repository is the safest and most stable option.
① Update Package Information
sudo apt update
sudo apt upgrade -y② Install MariaDB
sudo apt install -y mariadb-server mariadb-clientNo special configuration is required during installation. After completion, the MariaDB service will start automatically.
③ Verify That MariaDB Is Running
sudo systemctl status mariadbIf active (running) is displayed, MariaDB is running properly.
④ Enable Auto-Start (Usually Not Required but Recommended to Confirm)
sudo systemctl enable mariadb⑤ Connect to MariaDB and Verify Operation
sudo mysqlIf you see the following prompt, the installation was successful:
MariaDB [(none)]>To exit, type exit;.
Mandatory Security Initialization After Installation
Immediately after installation, MariaDB only has minimal security settings applied. Be sure to run the following command:
sudo mysql_secure_installationGenerally, the following answers are recommended:
- Remove anonymous users → Y
- Disallow root login remotely → Y
- Remove test database → Y
- Reload privilege tables → Y
This completes the setup of a secure MariaDB environment on Ubuntu.
MariaDB Version Installed by Ubuntu Version
- Ubuntu 20.04 → MariaDB 10.3 series
- Ubuntu 22.04 → MariaDB 10.6 series
- Ubuntu 24.04 → MariaDB 10.11 series (LTS)
For most users, the Ubuntu default version is sufficient. Only consider the official repository if you need the latest features.
Quick Checks If Installation Fails or Service Does Not Start
- Check errors with
sudo systemctl status mariadb - Check detailed logs with
sudo journalctl -xe - Check disk space with
df -h
In most cases, the issue is either insufficient disk space or a configuration file typo.
Choosing the Right MariaDB Version for Your Ubuntu Version
When using MariaDB on Ubuntu, the safest option in most cases is the default repository version. Security updates and dependency compatibility are maintained automatically, making it ideal for beginners and intermediate users.
Ubuntu Default Repository Version (Recommended)
- Ubuntu 20.04 → MariaDB 10.3 series
- Ubuntu 22.04 → MariaDB 10.6 series
- Ubuntu 24.04 → MariaDB 10.11 series (LTS)
Unless you have a specific requirement, these versions are fully practical for production use.
If You Need the Latest Version (Add the Official MariaDB Repository)
If you need the newest features, you can add the official MariaDB repository. Since apt-key is deprecated, this uses the signed-by method.
① Save the GPG Key
sudo mkdir -p /etc/apt/keyrings
sudo curl -fsSL https://mariadb.org/mariadb_release_signing_key.asc \
| sudo gpg --dearmor -o /etc/apt/keyrings/mariadb.gpg② Check Your Ubuntu Codename
lsb_release -csExamples:
- 20.04 →
focal - 22.04 →
jammy - 24.04 →
noble
③ Add the Official Repository (Example: 10.11 Series)
echo "deb [signed-by=/etc/apt/keyrings/mariadb.gpg] \
https://mirror.mariadb.org/repo/10.11/ubuntu \
$(lsb_release -cs) main" \
| sudo tee /etc/apt/sources.list.d/mariadb.list④ Update Packages and Install
sudo apt update
sudo apt install mariadb-server mariadb-clientNotes When Using the Official Repository
- You can use newer features than the Ubuntu default version
- There is a future risk of dependency conflicts
- Thorough testing is required for production servers
For production, clearly decide whether you prioritize stability or the latest features.
How to Check the Currently Installed MariaDB Version
mysql --versionExample:
mysql Ver 15.1 Distrib 10.6.16-MariaDBBy explicitly confirming the version, troubleshooting and migration work become much easier.
Complete Guide to Initial MariaDB Security Setup (mysql_secure_installation)
Immediately after installing MariaDB, only minimal security settings are applied. To operate safely, be sure to run the following command:
sudo mysql_secure_installationWhen executed, several prompts will appear. In most cases, the following answers are recommended.
Recommended Answers
- Enter current password for root: → Press Enter (not set on first run)
- Switch to unix_socket authentication? → Y (Recommended on Ubuntu)
- Change the root password? → N (If using unix_socket authentication)
- Remove anonymous users? → Y
- Disallow root login remotely? → Y
- Remove test database and access to it? → Y
- Reload privilege tables now? → Y
This completes the basic security configuration.
Why You Cannot Log In as root (Access Denied Error)
On Ubuntu, the root user uses unix_socket authentication by default. Therefore, standard password-based login will not work.
Example Symptom
mysql -u root -p→ Access denied for user 'root'@'localhost'
Correct Login Method (Ubuntu Default)
sudo mysqlThis is the safest and recommended method.
How to Change root to Password Authentication (Only If Necessary)
Only change the authentication method if you must connect as root from external tools or applications.
① Connect to MariaDB
sudo mysql② Change the Authentication Plugin
ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password
BY 'StrongPasswordHere';
FLUSH PRIVILEGES;③ Confirm the Change
SELECT user, host, plugin FROM mysql.user;If mysql_native_password is displayed, the change was successful.
How to Check the Current Authentication Method
sudo mysql -e "SELECT user, host, plugin FROM mysql.user;"Main authentication methods:
- unix_socket → Ubuntu default, secure
- mysql_native_password → Password-based authentication
Unless there is a specific reason, it is safest to keep root using unix_socket authentication.
Common Errors and Solutions
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’
- Cause: Attempting password-based login
- Solution: Log in using
sudo mysql
mysql_secure_installation Not Found
- Check with
which mysql_secure_installation - If not installed, run
sudo apt install mariadb-server
Set Character Encoding to utf8mb4 (Prevent Garbled Text)
To safely handle multilingual text and emoji in MariaDB, it is recommended to use utf8mb4. Without this setting, you may encounter encoding issues or data loss.
① Edit the Configuration File
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnfAdd or modify the following lines in the [mysqld] section:
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci② Restart MariaDB
sudo systemctl restart mariadb③ Verify the Settings
sudo mysql -e "SHOW VARIABLES LIKE 'character_set%';"If utf8mb4 is displayed, the configuration is successful.
Specify utf8mb4 When Creating a Database (Recommended)
If you want to explicitly define the character set regardless of global settings, specify it during database creation.
CREATE DATABASE sample_db
CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci;All tables created within this database will inherit utf8mb4.
Create an Application User and Grant Permissions
In production environments, never use the root user for application connections. Create a dedicated user and grant only the minimum required privileges.
① Log in to MariaDB
sudo mysql② Create a User (Local Connection)
CREATE USER 'app_user'@'localhost'
IDENTIFIED BY 'StrongPasswordHere';③ Grant Database Privileges
GRANT ALL PRIVILEGES
ON sample_db.*
TO 'app_user'@'localhost';
FLUSH PRIVILEGES;Create a Remote Access User (Only If Needed)
Configure this only if you need to connect from an external server or cloud environment.
CREATE USER 'app_user'@'%'
IDENTIFIED BY 'StrongPasswordHere';
GRANT ALL PRIVILEGES
ON sample_db.*
TO 'app_user'@'%';
FLUSH PRIVILEGES;Note: The “%” wildcard allows connections from any host. In production environments, specifying a particular IP address is safer.
How to Check Current Users and Privileges
SELECT user, host FROM mysql.user;
SHOW GRANTS FOR 'app_user'@'localhost';Common Permission Errors and Solutions
Access denied for user ‘app_user’
- Incorrect host specification (difference between localhost and %)
- FLUSH PRIVILEGES was not executed
- Incorrect database name during connection
Unknown database
- Database name typo
- No privileges granted for the target database
How to Allow Remote Connections in MariaDB (Fix When External Access Fails)
By default, MariaDB allows local connections only. If you need to connect from an external server or cloud environment, configure the following settings.
① Change bind-address
Edit the configuration file:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnfFind the following line:
bind-address = 127.0.0.1To allow external connections, change it to:
bind-address = 0.0.0.0After saving, restart MariaDB:
sudo systemctl restart mariadb② Open Port 3306 in the Firewall
If you are using UFW on Ubuntu:
sudo ufw allow 3306
sudo ufw reloadFor improved security, specify the source IP address:
sudo ufw allow from 192.168.1.10 to any port 3306③ Create a Remote Access User
CREATE USER 'app_user'@'%'
IDENTIFIED BY 'StrongPasswordHere';
GRANT ALL PRIVILEGES
ON sample_db.*
TO 'app_user'@'%';
FLUSH PRIVILEGES;For better security:
CREATE USER 'app_user'@'192.168.1.10'
IDENTIFIED BY 'StrongPasswordHere';Checklist If You Cannot Connect Externally
1. Is MariaDB Running?
sudo systemctl status mariadb2. Is Port 3306 Listening?
sudo ss -tulnp | grep 3306If it shows 0.0.0.0:3306, it is correctly listening on all interfaces.
3. If Using a Cloud Environment (AWS / GCP / VPS)
- Ensure port 3306 is open in the security group
- Confirm that no external firewall is blocking the connection
4. Causes by Error Message
Can’t connect to MySQL server
- Port is closed
- bind-address not changed
- Firewall blocking the connection
Access denied for user
- Incorrect user host specification
- Incorrect password
- Privileges not granted
Connection timed out
- Cloud-side security settings
- Network routing issues
Important Security Notes
- Avoid using “%” in production environments
- Use VPN access whenever possible
- Consider enabling SSL/TLS connections
- Never allow remote root login
Troubleshooting When MariaDB Fails to Start (failed Error)
① Check Service Status
sudo systemctl status mariadb② Check Detailed Logs
sudo journalctl -xe
sudo cat /var/log/mysql/error.log③ Common Causes
- Configuration file syntax errors
- Insufficient disk space
- Port conflict (MySQL already running)
④ Check Disk Space
df -hSummary of Access Denied Error Causes
- Incorrect password
- Incorrect host specification (difference between localhost and %)
- FLUSH PRIVILEGES not executed
- Attempting standard login for root (requires
sudo mysql)
Basic Performance Improvements (For Beginners)
Advanced tuning is not required, but at minimum, confirm the following settings.
① Enable Slow Query Log
Add the following to 50-server.cnf:
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2Restart MariaDB:
sudo systemctl restart mariadb② Check Index Usage
Slow queries may be missing proper indexes.
EXPLAIN SELECT * FROM sample_table WHERE column_name = 'value';③ Check Server Resources
top
htopIf memory is insufficient, consider upgrading your server plan.
Frequently Asked Questions (FAQ)
How can I lock the MariaDB version on Ubuntu 22.04?
Use apt-mark hold mariadb-server.
How do I reset the root password if I forgot it?
You can reset it by starting in safe mode and reconfiguring the password. See our dedicated guide for detailed steps.
How do I change the MariaDB port number?
Modify 50-server.cnf to something like port = 3307, then restart the service.
Conclusion: Using MariaDB Safely on Ubuntu
- Using the default repository version is the safest option
- Always run mysql_secure_installation
- Never use root for application connections
- Enable remote access only when necessary
- When troubleshooting, checking logs is the top priority
By following these steps, you can build a secure and production-ready MariaDB environment on Ubuntu.


