Master MySQL Installation on Ubuntu with MariaDB and Docker

Master MySQL Installation on Ubuntu with MariaDB and Docker

Introduction

Installing and managing MySQL on Ubuntu has never been easier, especially when integrating MariaDB and Docker for enhanced flexibility. MySQL is a powerful open-source relational database management system designed to handle everything from small web projects to enterprise-grade applications. In this guide, you’ll learn how to install, configure, and secure MySQL 8.0 on Ubuntu 20.04, compare its performance with MariaDB, and even run it efficiently using Docker containers. By the end, you’ll have a fully optimized MySQL setup ready for modern web and application development.

What is MySQL?

MySQL is an open-source system that helps store, organize, and manage data for websites and applications. It allows users to create and control databases where information can be added, changed, or retrieved efficiently. This tutorial explains how to install, secure, and use MySQL on an Ubuntu server so users can manage their data safely and effectively without needing advanced technical knowledge.

Prerequisites

Alright, before jumping into this MySQL adventure on Ubuntu, let’s make sure your setup is good to go. Picture this, your Ubuntu server is like a car. You wouldn’t hit the road without checking your tires and filling up on fuel, right?

So first things first, you’ll need one Ubuntu server that’s set up with a non-root admin user and a firewall. It’s best if that firewall is managed using UFW , which stands for Uncomplicated Firewall. It’s basically your car’s alarm system, keeping unwanted visitors from sneaking into your system through open ports.

Now, having a non-root admin user is super important. Think of it as having a spare key, you can still do all the important stuff, but you’re not risking messing up the engine room by mistake. This helps keep your system safe and avoids accidental issues with core files. The firewall, meanwhile, works as your digital gatekeeper, managing what comes in and out of your setup.

Before moving ahead, make sure your Ubuntu system includes all of this. These aren’t just nice extras, they’re the basics for keeping your system both functional and secure. And if your Ubuntu server is fresh and untouched, go ahead and set it up now. That means creating a user with sudo privileges, turning on your firewall, and checking that you’ve got SSH access for remote use. It’s like putting your seatbelt on before you start driving.

Installing MySQL

Here’s where things get exciting, installing MySQL on Ubuntu. The great thing about Ubuntu is that it’s ready to grab software through its APT package system, kind of like a safe and reliable app store.

At this point, the version you’ll most likely get is MySQL 8.0.27, a trusted version that works well with modern setups and projects.

Let’s start by updating your package list so your system knows where to find the latest tools. Type in this command:

$ sudo apt update

Once your system is up to date, go ahead and install the MySQL server with this:

$ sudo apt install mysql-server

When that’s done, start your MySQL service with this command:

$ sudo systemctl start mysql.service

Now your MySQL server should be running. But here’s the catch, it’s like having a new safe with no lock on it yet. MySQL doesn’t ask you to set a password or security settings at this point. That means it works, but it’s still open to risk. No worries, we’ll fix that next.

Configuring MySQL

Once MySQL is up and running, it’s time to make it secure and steady. Luckily, MySQL comes with a built-in helper tool called mysql_secure_installation . Think of it like a setup wizard for your security guard. It helps you turn off remote root logins, remove test databases, and delete any users that shouldn’t be there.

But here’s the thing, back in July 2022, a small change was made. If you run the script right away, you might see an error. Why? Because by default, the root user in MySQL doesn’t use a password on Ubuntu. Instead, it uses socket authentication, which makes the script loop endlessly like it’s stuck in an elevator that won’t open.

You might see something like this:

Output
Error: SET PASSWORD has no significance for user ‘root’@’localhost’…

Don’t worry, it’s an easy fix. First, open the MySQL prompt:

$ sudo mysql

Then tell MySQL that the root user should use password-based login instead of socket login:

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;

Now, exit the prompt:

exit

Perfect. You’ve just taught MySQL to handle passwords again. Now you can run the security tool without getting stuck:

$ sudo mysql_secure_installation

This script will walk you through several questions to help make your MySQL setup safer.

  • LOW: At least 8 characters.
  • MEDIUM: Includes numbers, upper and lower case letters, and special characters.
  • STRONG: All of that plus a dictionary check.

If you’re serious about safety (and you probably are), go for option 2, the strong policy.

Next, you’ll set a password for the root user. MySQL will even show how strong your password is. If you like it, type “Y” and continue. The script will then clean out anonymous users, turn off remote root logins, and remove test databases. Basically, it locks things down tight.

If you’d rather switch back to socket login for convenience, just run:

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH auth_socket;

And that’s it. Your MySQL setup is now locked, loaded, and ready to go.

Creating a Dedicated MySQL User and Granting Privileges

Here’s a handy tip, even though MySQL gives you a root account, it’s best not to use it all the time. It’s like using a high-end sports car just to grab coffee, risky and unnecessary. Instead, create a new MySQL user with specific privileges.

To do that, open the MySQL shell as root:

$ sudo mysql

Now, make a new user. Replace “sammy” with the name you want and “password” with a strong one:

CREATE USER ‘sammy’@’localhost’ IDENTIFIED BY ‘password’;

You can also pick which authentication plugin to use:

CREATE USER ‘sammy’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;

Next, let’s give your new user some powers, but not too many:

GRANT CREATE, ALTER, DROP, INSERT, UPDATE, INDEX, DELETE, SELECT, REFERENCES, RELOAD ON *.* TO ‘sammy’@’localhost’ WITH GRANT OPTION;

To save the changes, run:

FLUSH PRIVILEGES;

Then leave MySQL:

exit

You can test your new user right away:

$ mysql -u sammy -p

Enter your password and you’re good to go. Your new MySQL user is ready for work.

Testing MySQL

Now for the fun part, testing if everything’s working.

Check if MySQL is running:

$ systemctl status mysql.service

If it’s not active, just start it manually:

$ sudo systemctl start mysql

To double-check that everything’s working fine, use this tool:

$ sudo mysqladmin -p -u sammy version

If you see version details and uptime, congrats. MySQL is up and running perfectly.

MySQL vs MariaDB Installation on Ubuntu

Let’s chat about MySQL’s close relative, MariaDB. Both are open-source relational database systems that keep your data tidy and easy to access.

Feature MySQL MariaDB
License GPL GPL
Storage Engines InnoDB, MyISAM, Memory InnoDB, Aria, TokuDB
Performance Reliable and scalable Faster queries and optimization
Security Strong encryption and SSL/TLS Better password hashing and encryption
Replication Master-Slave and Master-Master Same setup but with faster syncing
Charset utf8mb4 utf8mb4
Support Large global community Active open-source developers
Compatibility Works with most tools Fully compatible with MySQL

Common Errors and Debugging

If MySQL doesn’t start, check the error log for clues:

$ sudo grep ‘error’ /var/log/mysql/error.log

Check your configuration file:

$ sudo cat /etc/mysql/my.cnf

Check for port conflicts:

$ sudo netstat -tlnp | grep 3306

If all else fails, start MySQL manually:

$ sudo service mysql start

Check versions:

$ sudo mysql -V
$ mysql -V

If they don’t match, adjust the plugin:

SELECT @@default_authentication_plugin;

If installation fails due to missing dependencies:

$ sudo apt update && sudo apt install mysql-server
$ sudo apt install libssl1.1
$ sudo apt update && sudo apt install mysql-server

System Requirements for MySQL Installation

  • Operating System: Ubuntu 18.04 or newer (64-bit)
  • Processor: Dual-core 2 GHz or faster
  • Memory: 4 GB minimum (8 GB recommended)
  • Storage: At least 2 GB free

Installing MySQL with Docker on Ubuntu

To install MySQL with Docker, run these commands:

$ sudo apt update && sudo apt install docker.io
$ sudo docker pull mysql
$ sudo docker run –name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql
$ sudo docker exec -it mysql mysql -uroot -ppassword

Performance Tuning MySQL After Installation

Once MySQL is running smoothly, you might want to boost performance. Open /etc/mysql/my.cnf and adjust cache and buffer settings for your system. Add indexes to frequently searched columns. Run ANALYZE TABLE periodically and use tools like mysqladmin or sysdig to monitor performance.

FAQs

How to install SQL in Ubuntu terminal?

$ sudo apt update && sudo apt install mysql-server

How to install MySQL Workbench in Ubuntu 20.04 using terminal?

$ sudo apt update && sudo apt install mysql-workbench

How to set up a MySQL database?

$ sudo mysql -u root -p

CREATE DATABASE mydatabase;
USE mydatabase;

How to start and stop MySQL on Ubuntu?

$ sudo service mysql start
$ sudo service mysql stop

Can I install multiple MySQL versions on Ubuntu?

$ sudo docker run –name mysql57 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql:5.7
$ sudo docker run –name mysql80 -p 3308:3306 -e MYSQL_ROOT_PASSWORD=password mysql:8.0

How to completely uninstall MySQL from Ubuntu?

$ sudo apt purge mysql-server mysql-client mysql-common
$ sudo apt autoremove
$ sudo apt autoclean

What’s the difference between MariaDB and MySQL on Ubuntu?

MariaDB is like MySQL’s open-source sibling, built for flexibility and performance. It’s fully compatible and easy to tweak for developers.

$ sudo apt update && sudo apt install mariadb-server

Ubuntu MySQL Server Documentation

Conclusion

Mastering MySQL installation on Ubuntu opens the door to building reliable and high-performing database environments. By combining MariaDB’s flexibility and Docker’s containerization, you can achieve scalability, security, and efficiency in managing databases for modern web applications. This guide walked you through setup, configuration, troubleshooting, and optimization, ensuring your MySQL 8.0 environment is both secure and production-ready.As database technologies continue to evolve, containerized deployments and hybrid setups will play an even larger role in DevOps workflows. Staying updated with the latest advancements in MySQL, MariaDB, and Docker will help you maintain peak performance and ensure your Ubuntu systems remain optimized for the next generation of data-driven development.

Install MySQL on Ubuntu 20.04: Step-by-Step Guide for Beginners (2025)

Alireza Pourmahdavi

I’m Alireza Pourmahdavi, a founder, CEO, and builder with a background that combines deep technical expertise with practical business leadership. I’ve launched and scaled companies like Caasify and AutoVM, focusing on cloud services, automation, and hosting infrastructure. I hold VMware certifications, including VCAP-DCV and VMware NSX. My work involves constructing multi-tenant cloud platforms on VMware, optimizing network virtualization through NSX, and integrating these systems into platforms using custom APIs and automation tools. I’m also skilled in Linux system administration, infrastructure security, and performance tuning. On the business side, I lead financial planning, strategy, budgeting, and team leadership while also driving marketing efforts, from positioning and go-to-market planning to customer acquisition and B2B growth.

Any Cloud Solution, Anywhere!

From small business to enterprise, we’ve got you covered!

Caasify
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.