
Install MySQL on Ubuntu 20.04: Step-by-Step Guide for Beginners
Introduction
Installing MySQL on Ubuntu 20.04 is a straightforward process, but getting it right requires some attention to detail. MySQL, a powerful and widely-used relational database management system, runs seamlessly on Ubuntu, offering flexibility and reliability for both beginners and seasoned developers. This guide takes you through the step-by-step process of installing MySQL 8.0 on an Ubuntu 20.04 server, from setting it up and securing it to creating users and testing your installation. Along the way, we’ll also compare MySQL with MariaDB, address common installation issues, and offer performance tuning tips to optimize your database setup.
What is MySQL?
MySQL is an open-source database management system used to store and manage data in a structured way. It helps organize and retrieve data for various applications like websites and services. This system works by allowing users to interact with the data using a programming language called SQL. MySQL is widely used due to its reliability, scalability, and strong community support.
Step 1 — Installing MySQL
Alright, let’s get MySQL running on your Ubuntu system. Here’s the thing: MySQL is available directly in the Ubuntu APT package repository, which means you don’t have to go searching for installation files. The repository has everything you need, making the installation process for MySQL pretty straightforward. At the time I’m writing this, the version of MySQL you’ll get is 8.0.27, which is a solid, stable version right off the bat.
First, let’s update the package index on your server. This just means making sure your system knows about the most up-to-date software versions available. You can update the system’s package list by running this simple command:
$ sudo apt update
Once your system is updated, the next step is to install the MySQL server package. This package contains all the necessary files to get MySQL running. To install it, run:
$ sudo apt install mysql-server
Once that command is finished, MySQL will be installed. But hang on, we’re not done yet! We need to make sure MySQL is running properly, right? To do that, start the MySQL service with the systemctl command like this:
$ sudo systemctl start mysql.service
This will start the service and ensure it’s running in the background, ready to handle your databases.
Now, at this point, your MySQL installation is technically up and running. But here’s the catch: it’s still insecure. The installation process doesn’t ask you to set a root password or configure any security settings. So, while everything seems good, your MySQL server is like an open door—no locks, no security. Don’t worry, we’ll fix these security settings in the next step. But just keep in mind that we’re not done securing it yet.
For further guidance on installation, refer to the official MySQL documentation.
MySQL Installation Guide (2025)
Step 2 — Configuring MySQL
So now that MySQL is up and running on your Ubuntu system, it’s time to make sure it’s locked down and as secure as possible. You see, by default, MySQL comes with some settings that are a little too loose for comfort. But don’t worry, we’ve got a built-in tool called mysql_secure_installation to help us fix that.
This tool works like your personal security guard, tightening up those less secure default settings. It disables remote root logins (you definitely don’t want someone sneaking in remotely) and removes sample users that could be exploited. It’s a crucial step to make sure your installation isn’t an easy target for hackers.
But here’s the catch: as of July 2022, there’s a small issue with running this script on Ubuntu systems. If you try running it right after installation, you might get an error related to the root user’s authentication method.
The Error: A Sticky Situation
When you run the mysql_secure_installation script, it tries to set a password for the root user. But, by default, Ubuntu doesn’t set up the root account to use a password. So, what happens next? The script tries to set that password, fails, and leaves you with an error message. If you’ve run into this, you’ve probably seen something like this:
Failed! Error: SET PASSWORD has no significance for user ‘root’@’localhost’ as the authentication method used doesn’t store authentication data in the MySQL server.
This error basically causes the script to throw its hands up and enter a loop, which is pretty frustrating. But don’t worry—it’s not the end of the world. The error just means we need to tweak the authentication method before we can run the security script successfully. Let’s fix this.
Fixing the Authentication Method
First things first, let’s open the MySQL prompt and adjust the root user’s authentication method. Open your terminal and run this command:
sudo mysql
This takes you into the MySQL shell, where we can make the change. Now, let’s tell MySQL to switch to a more secure password-based authentication method. We’ll use the mysql_native_password plugin to make sure we’re good to go. Run the following command:
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘your_secure_password’;
Make sure to replace ‘your_secure_password’ with something strong that only you know. Once that’s done, exit the MySQL shell by typing:
exit
Now that we’ve set up password authentication for the root user, we can move on to running the security script.
Running the Security Script
Let’s run the mysql_secure_installation script again. This time, it should work perfectly:
sudo mysql_secure_installation
You’ll be greeted by a series of prompts aimed at locking down your MySQL installation. The first thing the script will ask is whether you want to enable the Validate Password Plugin. Think of this plugin as a bouncer at a nightclub, making sure every password is strong enough to get in. If you say yes, you’ll be asked to choose a password policy. You have three options:
- LOW: Requires passwords to be at least 8 characters.
- MEDIUM: Requires passwords to be at least 8 characters, with a mix of numbers, uppercase and lowercase letters, and special characters.
- STRONG: Requires passwords to be at least 8 characters, with everything mentioned above, plus a dictionary file to check for weak or common passwords.
If you want the strongest security, choose STRONG (Option 2).
Next, the script will ask you to set a new password for the MySQL root user. Go ahead and enter the password you just chose:
Please set the password for root here. New password: Re-enter new password:
Once the script checks that your password meets the selected policy, it will confirm it’s strong enough. You’ll then be asked if you want to continue with the password you entered or try another one. If you’re happy with it, press Y to continue.
Securing the Rest
The script doesn’t stop there—it also does some extra security clean-up. It’ll remove anonymous users, disable remote root logins (we don’t want those), and remove the test database. These steps help reduce potential vulnerabilities. Once the script finishes, your MySQL installation will be locked down and much safer.
Restoring the Root Authentication Method
Now that your MySQL installation is secure, you can switch the root user’s authentication method back to the default. This method is based on auth_socket , which lets you authenticate as root using your system’s user credentials (like sudo). So, let’s switch it back.
Reconnect to MySQL with:
mysql -u root -p
Enter the root password you just set, and then run this command to restore the default authentication method:
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH auth_socket;
Now, you can use the
sudo mysql
Wrapping It Up
With these steps, your MySQL installation is now properly secured and ready to go. You’ve updated the root user’s authentication method, run the security script to tighten everything up, and restored the authentication method to a secure, convenient setting. Now you can move on to creating dedicated MySQL users with the necessary privileges for your applications—ensuring that your system is both secure and efficient. You’ve got this!
For further details, refer to the MySQL Secure Installation Guide.
Step 3 — Creating a Dedicated MySQL User and Granting Privileges
After you’ve installed MySQL on your Ubuntu system, there’s something important happening behind the scenes: MySQL automatically creates a root user account. Now, the root user is pretty powerful—it has complete control over everything in your MySQL server. It can manage databases, tables, users, and pretty much all the important stuff. But here’s the thing: because the root user has all that power, it’s not the best idea to use it for everyday tasks. Think of it like driving a sports car—you wouldn’t use it just for a quick trip to the store every day, right? Instead, you create a dedicated user with just the right amount of privileges for the task at hand. In this step, I’ll walk you through how to create a new MySQL user and assign it the privileges it needs. Trust me, it’s an important step to keep things organized and secure.
Now, on Ubuntu systems running MySQL 5.7 or later, the root user by default uses the auth_socket plugin for authentication. This means you can only log in as root if you’re using the same username as your operating system username and have sudo privileges. It’s like a VIP club where the bouncer checks your ID before letting you in. If you’re trying to log in with the root user, you’ll need to run MySQL with sudo privileges, like this:
$ sudo mysql
But here’s something important to note: if you’ve followed a different guide and set up password authentication for the root user, you’ll need to log in a little differently. Instead of using sudo, just run:
$ mysql -u root -p
This will prompt you to enter your root password. Once you’re in, you’re ready to create a new MySQL user.
Creating the New User
To create a new user, we’ll use the CREATE USER statement. Here’s how you do it:
CREATE USER ‘username’@’host’ IDENTIFIED WITH authentication_plugin BY ‘password’;
In this command:
- ‘username’ is the name of the new MySQL user you want to create.
- ‘host’ specifies the server from which the user will connect. If you only want the user to connect from the local server, just use ‘localhost’ .
- authentication_plugin is how the user will authenticate (think of it like the type of lock they need to open the door). MySQL’s default plugin is mysql_native_password , which is used for password-based authentication.
- ‘password’ is where you specify a secure password for this new user.
For example, if I wanted to create a user called ‘sammy’ who will connect from the local machine, I would run:
CREATE USER ‘sammy’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘your_secure_password’;
Make sure you replace ‘your_secure_password’ with a strong password. Don’t use the same old “password123,” okay? That’s a big no-no.
Choosing the Right Authentication Plugin
Now, when creating the user, you’ll need to choose the right authentication plugin. The default auth_socket plugin works great for local connections but, it doesn’t allow remote connections. If you ever need to connect from outside the server, then the mysql_native_password plugin is a better choice.
If you’re aiming for a more secure connection (and who wouldn’t want that?), you could opt for the caching_sha2_password plugin. It’s considered pretty solid in terms of security, and MySQL even recommends it for password-based authentication.
If you want to create a user with caching_sha2_password , here’s how you do it:
CREATE USER ‘sammy’@’localhost’ IDENTIFIED BY ‘your_secure_password’;
This will set up your user with the caching_sha2_password plugin. But, if you’re planning to use PHP-based tools like phpMyAdmin, you might run into compatibility issues with this plugin. No worries though! You can always switch to the more widely supported mysql_native_password plugin later on with the following command:
ALTER USER ‘sammy’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘your_secure_password’;
Granting Privileges to the New User
Once the new user is set up, the next step is to give them the right privileges. This is like assigning them access to certain rooms in the MySQL building—based on what you need them to do. You grant privileges using the GRANT statement:
GRANT PRIVILEGE ON database.table TO ‘username’@’host’;
Here, PRIVILEGE refers to what actions the user can take, like selecting data, inserting data, updating tables, etc. You can grant multiple privileges in a single statement by separating each privilege with commas.
For example, let’s say you want to give ‘sammy’ the ability to create, alter, drop, insert, update, and delete data across all databases. You would run this:
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE ON *.* TO ‘sammy’@’localhost’ WITH GRANT OPTION;
The *.* part means “all databases and tables.” The WITH GRANT OPTION part means that ‘sammy’ can also give these same privileges to other users if needed.
But hold on, a quick word of caution: it might be tempting to give the user ALL PRIVILEGES. While that sounds like the ultimate access, it essentially makes them a superuser, much like the root account. So, be careful with that, and only grant it if absolutely necessary. If you’re feeling risky, you can do this:
GRANT ALL PRIVILEGES ON *.* TO ‘sammy’@’localhost’ WITH GRANT OPTION;
But again, use this sparingly—giving someone complete control over your MySQL server is not a decision to take lightly.
Finalizing the Privileges
Once you’ve granted the necessary privileges, it’s a good idea to run this command:
FLUSH PRIVILEGES;
This makes sure MySQL refreshes its cache and immediately applies the privileges you’ve just set. Now, you’re good to go!
Logging In as the New User
Finally, now that your user has been created and the privileges have been set, you can log in as your new user with:
$ mysql -u sammy -p
When you run this, it’ll prompt you for the password of the ‘sammy’ user, which you just set. And boom! You’re in, ready to start using MySQL with a dedicated user account that’s secure and tailored to your specific needs.
Now that your MySQL installation is set up properly, you’ve taken the right steps toward keeping your system both secure and efficient. You’ve created a user with just the right privileges for the job—no more, no less! Pretty smart, huh?
Remember to always create dedicated users with appropriate privileges for security and efficiency.
MySQL Grant Privileges Documentation
Step 4 — Testing MySQL
Alright, now that MySQL is installed, we need to make sure it’s doing its job properly. Here’s the thing: when you install MySQL, it should automatically start running. But sometimes, you just want to double-check that it’s really up and running the way it should. And that’s where you come in—by checking its status.
To check if MySQL is running, just run this command:
$ systemctl status mysql.service
When everything is working fine, the system will give you a nice report confirming that MySQL is indeed active and functioning. Here’s an example of what that might look like:
● mysql.service – MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-04-21 12:56:48 UTC; 6min ago
Main PID: 10382 (mysqld)
Status: “Server is operational”
Tasks: 39 (limit: 1137)
Memory: 370.0M
CGroup: /system.slice/mysql.service
└─10382 /usr/sbin/mysqld
What this tells you is that MySQL is alive and kicking, running with a good amount of memory, processing tasks, and keeping your database in check. If for some reason it’s not running, no worries—you can get it back on track by manually starting MySQL with this command:
$ sudo systemctl start mysql
Now, we’re not quite done yet. While you’ve confirmed that MySQL is running, it’s also a good idea to double-check its functionality. Think of it like taking a car for a test drive after checking that the engine’s running—just to make sure everything’s working smoothly.
For this, we use the mysqladmin tool, which is a handy command-line client that lets you do things like check the server’s status or see the version. To do this, run:
$ sudo mysqladmin -p -u sammy version
Make sure to replace “sammy” with the username of your MySQL user. The -p flag will prompt you to enter the password for that user, and after you type it in, you’ll see some detailed info about your MySQL installation. You should expect to see something like this:
mysqladmin Ver 8.0.19-0ubuntu5 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Server version 8.0.19-0ubuntu5
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 10 min 44 sec
Threads: 2
Questions: 25
Slow queries: 0
Opens: 149
Flush tables: 3
Open tables: 69
Queries per second avg: 0.038
If this looks like the output you’re getting, congratulations! You’ve just confirmed that MySQL is up, running, and performing well on your Ubuntu system. All the numbers and stats are just a bonus—they give you insight into how MySQL is performing, including uptime, number of queries, and how many tables are open. So, if your output is similar, you’re good to go! Your MySQL installation is correctly configured and operational. You’re all set to start diving deeper into your database management tasks.
For more details, you can check the official MySQL Admin Documentation.
MySQL vs MariaDB Installation on Ubuntu
Imagine you’re setting off on a mission to build a high-performance database for your web application. You have two trusted companions by your side—MySQL and MariaDB—each with its own superpowers. As you prepare to install and set up your database on Ubuntu, it’s important to know the differences between these two popular open-source relational database management systems (RDBMS). Both MySQL and MariaDB are known for being reliable and scalable, and they serve similar purposes. But just like two superheroes, each has its own strengths that might make one more suited to your project than the other. Let’s dive into their basic features and figure out how each one might work for your project on Ubuntu.
The License That Sets Them Free
Both MySQL and MariaDB are licensed under the GPL (General Public License), meaning they’re open-source and free for anyone to use, modify, and share. So you don’t have to worry about surprise licensing fees later. But here’s where things start to get interesting—each one brings its own unique set of features to the table.
Storage Engines: The Backbone of Your Data
When it comes to storing data, MySQL offers a few options like InnoDB, MyISAM, and Memory. Each one is designed with different performance and transaction support in mind. Think of them like the gears on your bike—each suited for a different kind of ride. MariaDB goes the extra mile, adding some unique options like Aria and TokuDB. Aria is made for high-performance tasks, while TokuDB is great for large databases and write-heavy operations. It’s like upgrading your bike with turbochargers—if you need more power for complex tasks, MariaDB has you covered.
Performance: Speed on the Road
MySQL has always been known for its high-performance optimizations. With features like query caching and thread pooling, it’s built to handle large-scale environments effortlessly. But here’s the twist—while MySQL is fast, MariaDB adds a few extra tweaks to the engine, like improved query optimization. If your application involves complex queries or heavy write operations, MariaDB could zip ahead of MySQL in performance, especially in those specific cases.
Security: Locking Down Your Data
When it comes to security, both MySQL and MariaDB have their bases covered. MySQL brings in SSL/TLS encryption to secure data while it’s being transferred, making sure your information stays safe. MariaDB doesn’t fall short either, with enhanced password hashing and encryption features to further safeguard your data.
Replication: Keeping Your Data in Sync
Whether you’re running a small app or managing a massive enterprise, both MySQL and MariaDB have you covered with Master-Slave and Master-Master replication setups. These allow for high availability and load balancing. But MariaDB has a bit of an edge when it comes to replication. With more advanced features, it shines in complex environments, adding an extra layer of reliability to your system.
Forked from the Same Code, But with Different Paths
Now, the story behind MariaDB is a bit of a fork in the road. MariaDB is a community-driven fork of MySQL, created when concerns about Oracle’s ownership of MySQL led developers to create an entirely open-source alternative. MySQL, on the other hand, is now commercially focused, with some proprietary features in its MySQL Enterprise Edition. If you’re someone who values open-source principles, MariaDB might be your hero.
Storage Engine Default: InnoDB vs. Aria
By default, MySQL uses InnoDB, which is great for transactional workloads and supports ACID properties (Atomicity, Consistency, Isolation, Durability). On the other hand, MariaDB uses Aria, which is designed for crash-safe, high-performance operations. Both are reliable, but Aria tends to perform better when it comes to read-heavy workloads. It’s like having two strong engines, but one’s better suited for certain types of journeys.
Charset: Supporting Global Applications
Both MySQL and MariaDB use utf8mb4 as the default character set. Whether you’re building a local app or serving a global audience, both databases can handle multi-byte characters, like emojis or different language scripts. It’s all about ensuring compatibility across the world.
SQL Syntax: A Common Language
If you’re already familiar with SQL, you won’t have to worry much about the syntax in either MySQL or MariaDB. They’re almost identical. MariaDB even extends MySQL’s functionality with new features, so if you’re used to MySQL, switching to MariaDB is pretty easy. Think of it like switching to a new toolkit—you can keep using the same tools, but MariaDB gives you a few extra.
Community Support: A Helping Hand
MySQL benefits from Oracle’s extensive documentation and a large community of developers. However, some of MySQL’s support and development are commercially driven, especially for the enterprise edition. On the other hand, MariaDB thrives on community-driven development, which means it’s built and supported by a passionate group of contributors. This makes it a great choice if you value open-source collaboration.
Compatibility: No Compatibility Issues Here
Both MySQL and MariaDB are compatible with a wide range of platforms and tools. If you’re already using MySQL’s tools, switching to MariaDB won’t be a hassle at all. It’s like changing cars, but you’re still driving in the same comfortable seat.
The Verdict: Which One Should You Choose?
Ultimately, the choice between MySQL and MariaDB comes down to your specific needs. If you need a reliable database with commercial support, MySQL is a solid option. But if you’re into open-source and want enhanced performance and security features, MariaDB might be a better fit. Both databases are strong contenders, and either one will work well for your Ubuntu server. It’s all about understanding what you need and picking the one that fits your project best. Whether you go with MySQL or MariaDB, you’ve got the right tools to build a strong and efficient database environment.
Common Errors and Debugging
You’ve just installed MySQL on your Ubuntu server, all ready to go, but then you hit a bump—MySQL won’t start. It’s a frustrating roadblock, but don’t worry, with some troubleshooting, you’ll be up and running again in no time. Let’s go through some of the common problems you might come across and how to fix them.
MySQL Service Not Starting
When MySQL won’t start, it’s usually because of something small that went wrong. First things first, let’s check the MySQL error log. Think of this log as your detective’s notebook—it’s full of clues. MySQL keeps an error log that can show us why it’s not starting. To check these clues, run this command:
$ sudo grep ‘error’ /var/log/mysql/error.log
This command will search through the MySQL error log for any entries labeled “error,” so you can spot the problem quickly. It’s like looking for a red flag in a sea of green!
Ensure Correct MySQL Configuration
Sometimes, the issue is with the MySQL configuration file, my.cnf. If something’s off here, MySQL might not start. Let’s open the file to make sure everything is in order:
$ sudo cat /etc/mysql/my.cnf
This command will open up the configuration file. Take a quick look to make sure it’s formatted properly and there are no unexpected syntax errors. If anything’s wrong, you’ll need to fix it before trying again.
Check for Port Conflicts
MySQL usually runs on port 3306. But, if something else is already using that port, MySQL won’t be able to start. To check for conflicts, run this command:
$ sudo netstat -tlnp | grep 3306
This will show if another process is already using the default MySQL port. If you find a conflict, you can either stop the other service or change MySQL’s port. It’s like trying to park two cars in the same spot—it just won’t work!
Manually Start MySQL
Okay, so you’ve checked everything, but MySQL still refuses to start. Don’t worry, just start it manually with one of these commands:
$ sudo service mysql start
or
$ sudo systemctl start mysql
Once it starts, you can check its status with:
systemctl status mysql
This will confirm that MySQL is up and running!
Authentication Plugin Errors
Now let’s talk about authentication errors. These happen when there’s a mismatch between the MySQL client and server versions. This can block you from logging in. Here’s how to fix it:
Verify Version Compatibility
If the MySQL client and server versions are different, they might not be compatible. To check the server version, run:
$ sudo mysql -V
Then check the client version with:
mysql -V
If the versions don’t match, updating either the client or server will solve the problem.
Check Authentication Plugin Configuration
Another potential issue is the authentication plugin. To see which one MySQL is using, run this command inside MySQL:
SELECT @@default_authentication_plugin;
This will show the current authentication plugin. If this is causing issues, you can change it.
Update or Change the Authentication Plugin
If the plugin is the problem, you can switch it to a more compatible one. A common choice is mysql_native_password , which works with almost anything. To change it, run:
ALTER USER ‘username’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;
Just replace username with your actual username and set a secure password. If authentication errors were causing you trouble, this should fix it!
MySQL Installation Failed: Missing Dependencies
If MySQL’s installation failed because of missing dependencies, don’t panic. Let’s figure out what’s missing.
Check Installation Logs
MySQL will give you some error logs that point out exactly what dependencies are missing. To check the logs, run:
$ sudo apt update && sudo apt install mysql-server
Look carefully at the error messages—they’ll tell you what’s missing.
Install Missing Dependencies
Once you know what’s missing, you can install it manually. For example, if libssl1.1 is missing, you can install it like this:
$ sudo apt install libssl1.1
Do the same for any other missing dependencies.
Retry MySQL Installation
Now that the missing dependencies are installed, try installing MySQL again with:
$ sudo apt update && sudo apt install mysql-server
This should complete the installation without issues.
Ensure Package Manager is Up-to-Date
If you keep running into dependency problems, make sure your package manager is up to date. You can do this by running:
$ sudo apt update && sudo apt full-upgrade
This updates all installed packages and might fix compatibility issues preventing MySQL from installing properly.
And that’s it! By following these steps, you should be able to solve common MySQL issues like service startup problems, authentication errors, or installation failures due to missing dependencies. Each step gives you a clear way to figure out and fix what’s wrong, so your MySQL installation should be running smoothly in no time.
For more detailed troubleshooting, refer to the official MySQL Troubleshooting Guide.
System Requirements for MySQL Installation
Before you dive into installing MySQL on your Ubuntu machine, it’s a good idea to make sure your system is ready for the task. Think of it like getting your car ready for a road trip—you want to make sure everything is working properly so you don’t run into problems along the way.
Operating System: Ubuntu 18.04 or Later
MySQL works best on Ubuntu, but not just any version. You’ll need Ubuntu 18.04 or a newer version. The most important thing here is that it needs to be the 64-bit version—this is a must. You might be tempted to use the 32-bit version, but the 64-bit version offers much better performance and scalability, especially when MySQL is busy handling databases and tons of data. Whether you’re using Ubuntu Server or Desktop, as long as it’s running a compatible Linux kernel, you’re good to go.
CPU: At Least a 2 GHz Dual-Core Processor
Next, we’re talking about your system’s brain—the CPU. You’ll need at least a 2 GHz dual-core processor to run MySQL smoothly. Why? Because MySQL doesn’t just sit around; it’s executing queries and managing all your data. A faster processor helps MySQL handle everything efficiently. However, if you’re planning on running more demanding applications or complex queries, you might want to go for a faster processor to keep things running smoothly.
Memory (RAM): 4 GB Minimum, 8 GB Recommended
When it comes to memory, 4 GB of RAM is the bare minimum to run MySQL without hiccups. But if you plan on running large databases, handling more users, or working with bigger, more complex applications, it’s a good idea to have at least 8 GB of RAM—or even more. Think of RAM as the space on your desk. The more space you have, the more tasks you can handle at once without everything getting messy and slow. So, the more RAM, the better your system will perform, especially when things start to get busy.
Storage: At Least 2 GB of Free Disk Space
Now, let’s talk about storage. You’ll need at least 2 GB of free disk space for MySQL to be installed. However, if you’re working with larger databases or handling massive queries, you’ll need a lot more space to grow. It’s like moving into a bigger house—you’ll need more storage space as your database grows over time. Don’t forget, MySQL also needs space for logs, database files, and other components, so plan ahead. Running out of space mid-operation? Not ideal.
Software: A Compatible Ubuntu Version
Lastly, you’ll need a version of Ubuntu Server or Desktop that’s compatible with MySQL. This ensures your system is stable, secure, and capable of handling everything MySQL needs. Also, make sure to keep your system updated—this isn’t just a nice-to-have; it’s essential for security patches and keeping everything running smoothly with the latest software versions.
By making sure your system meets these requirements, you’ll be ready to install MySQL without any issues. If your system doesn’t quite meet these specs, don’t worry—you might run into a few problems, but they’re not the end of the world. Just ensure you meet or exceed these requirements, and your MySQL experience on Ubuntu will be smooth sailing.
Make sure your system is updated with the latest patches for optimal performance and security.
Installing MySQL with Docker on Ubuntu
Imagine you’re setting up a new MySQL database, but you don’t want to mess with your system’s core settings. Here’s the perfect solution: Docker. It lets you run MySQL in its own isolated container, so you can keep it separate from your Ubuntu system. This way, MySQL runs smoothly without affecting anything else—great for testing or development. Let’s walk through the steps to get MySQL running with Docker on Ubuntu.
Step 1: Install Docker
First, we need to get Docker up and running. Docker is a tool that lets you create and manage containers, which are like mini virtual environments. Once you have Docker installed, it gives you a lot of flexibility and control, all while keeping things neatly contained.
If you don’t have Docker installed yet, it’s time to get it. Run these commands in your terminal:
$ sudo apt update
$ sudo apt install docker.io
Once that’s done, Docker will be installed and ready to go. It’s a straightforward process, no magic needed. Now, you’re ready to deploy MySQL in its own container.
Step 2: Pull the MySQL Image
Now for the fun part. To run MySQL in a container, you need to pull the official MySQL image from Docker Hub. This is where all the files you need to run MySQL are located.
Run this command to download the latest version of the MySQL image:
$ sudo docker pull mysql
This will get you the latest version of MySQL. If you need a specific version, like MySQL 5.7, just modify the command like this:
$ sudo docker pull mysql:5.7
Docker Hub has all the versions, config files, and binaries you need. Once the download’s done, you’re one step closer to running MySQL in its own containerized environment.
Step 3: Run the MySQL Container
Now it’s time to create your MySQL container. With just one simple command, you can get MySQL running in isolation with the ports and settings all set up.
Here’s the command you’ll need to run:
$ sudo docker run –name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql
Let’s break that down:
- –name mysql: This gives your container a name, making it easier to reference later. In this case, we’re calling it “mysql.”
- -p 3306:3306: This maps the default MySQL port (3306) inside the container to the same port on your system. It’s like opening a window from the container to the outside, so you can access MySQL.
- -e MYSQL_ROOT_PASSWORD=password: This sets the root password for MySQL. Be sure to replace “password” with something more secure.
- mysql: This tells Docker to use the official MySQL image we pulled earlier.
Once you run this command, Docker will take care of everything, spinning up the container and getting MySQL running inside it. Your MySQL instance is now isolated and secure.
Step 4: Verify the Installation
Now that MySQL is running inside its own container, let’s make sure everything’s working. You’ll need to log into the MySQL shell to confirm that it’s up and running.
Use this command to log in:
$ sudo docker exec -it mysql mysql -uroot -ppassword
Here’s what’s happening:
- $ sudo docker exec -it mysql: This tells Docker to run a command inside the running MySQL container (which we named “mysql”).
- mysql -uroot -ppassword: This is the MySQL command to log in as the root user using the password you set earlier.
If everything works as expected, you’ll be logged into the MySQL shell. Now, you’ve got MySQL running in a Docker container on Ubuntu, all set up and ready to manage your databases.
Conclusion
That’s it! By following these steps, you’ve successfully installed MySQL using Docker on your Ubuntu system. It’s all isolated, secure, and easy to manage. Now you can deploy, test, or develop without worrying about affecting the rest of your system. Docker really makes database management a breeze!
Performance Tuning MySQL After Installation
Alright, you’ve got MySQL installed on your Ubuntu system—nice job! But here’s the thing: getting MySQL up and running is just the start. To really make the most of it, you’ll need to tweak a few settings. It’s not just about getting things to work; it’s about making them work better. Think of it like tuning a car engine—you want to make sure it’s running at its best, not just getting it started. Let’s go over some steps that’ll have MySQL running smoothly.
- Optimize the MySQL Configuration File
First things first: MySQL’s configuration file, usually found at /etc/mysql/my.cnf , is where the magic happens. This is where you’ll change settings to make MySQL work better with your system. It’s like adjusting the gears on a bike—get it right, and everything runs smoother.
Here are some key settings to check:
- innodb_buffer_pool_size: This one’s important! It controls how much memory InnoDB uses to buffer data. Increasing this will help reduce disk I/O, speeding up your database.
- max_connections: This controls how many users can connect to MySQL at once. You don’t want too many if your server can’t handle it, but you also don’t want it too low if you’ve got a growing team of users.
- query_cache_size: If you run a lot of repetitive queries, enabling query caching could be a big win. This lets MySQL quickly retrieve results for repeated queries. But, just make sure to test it—it’s not always the best option for every workload.
By adjusting these settings, you’ll make MySQL work more efficiently and better suited to your server’s capabilities.
- Use a Suitable Storage Engine
Now that we’ve got the configuration file sorted, let’s talk about storage engines. Think of them like different types of roads your car (or database) can drive on. Some roads are smooth and fast, others are bumpier. MySQL offers several options, but let’s focus on the main ones:
- InnoDB: This is the default engine for MySQL, and it’s perfect for transactional workloads. It supports ACID (Atomicity, Consistency, Isolation, Durability), foreign keys, and crash recovery. If your application does a lot of transactions, this is your best bet.
- MyISAM: If your app is more about reading data than writing it (like a blog with mostly static content), MyISAM might be faster. It doesn’t have all the features of InnoDB, but it speeds up read-heavy workloads.
- Aria & TokuDB: For high-performance, large-scale applications, these engines offer great performance, especially with heavy writes or large data.
Choosing the right engine is key. Imagine trying to drive a sports car on a dirt road—it won’t run as efficiently. Pick the engine that fits your needs.
- Index Your Tables
Next up: indexes. Think of them like the table of contents in a book—they help MySQL find the information it needs without having to read every page. Creating indexes on frequently queried columns can speed up searches by a lot.
For example, if you often search for users by their user_id , creating an index on that column will speed things up:
CREATE INDEX user_id_index ON users (user_id);
But here’s the thing: don’t go overboard with indexes. Too many can actually slow down write operations. Just index the columns you use most often for queries.
- Regularly Update Statistics
Here’s something that’s often overlooked: keeping statistics up to date. MySQL uses stats to decide the best way to run a query. If those stats are outdated, it can make poor decisions and slow things down.
To keep stats fresh, run this command regularly:
$ ANALYZE TABLE table_name;
It’s a good idea to do this during off-peak hours if you’ve got a large database, especially if you update data frequently. Just like keeping your car’s oil changed, staying on top of this helps everything run smoothly.
- Monitor Performance
Lastly, you need to keep an eye on how MySQL is performing. You can’t just set it and forget it—MySQL is constantly changing as your application grows. Thankfully, there are tools that help you monitor performance.
- mysqladmin: This is a simple command-line tool that lets you check MySQL’s status. You can monitor things like uptime, thread count, and queries per second. For example:
- sysdig: For a deeper dive, sysdig helps you track MySQL’s resource usage like CPU, memory, and I/O, so you can catch potential performance issues before they get big.
$ sudo mysqladmin -u root -p status
By keeping track of these stats, you can identify any bottlenecks or resource issues before they become major problems.
The Bottom Line
Optimizing MySQL isn’t a one-time task—it’s something you’ll need to keep doing as your system grows. Just like keeping a car in shape, you’ll need to adjust things over time. By tweaking the configuration, choosing the right storage engine, indexing key columns, updating stats, and monitoring performance, you’ll make sure MySQL is running at its best. With regular adjustments, you’ll have a fast, reliable, and scalable database system.
MySQL Performance Optimization Guide
FAQs
How to install SQL in Ubuntu terminal?
So, you’ve got Ubuntu running and you’re ready to set up MySQL. To get started, open your terminal and run a couple of simple commands to update your package index and install MySQL. Here’s what you’ll need to do:
$ sudo apt update && sudo apt install mysql-server
This will grab the MySQL server and set it up on your system, so you’ll be ready to start creating databases and running queries. Pretty straightforward, right?
How to install MySQL Workbench in Ubuntu 20.04 using terminal?
Now, if you prefer a graphical interface to manage your MySQL databases, you’ll want MySQL Workbench. It’s super helpful for designing, managing, and running your queries. To install it, just run:
$ sudo apt update && sudo apt install mysql-workbench
This will install the Workbench on Ubuntu 20.04. It’s a neat tool that makes working with MySQL a lot more visual and user-friendly. You’ll thank yourself later!
How to set up a MySQL database?
Setting up a MySQL database is easier than you think. Here’s what you do:
- Make sure MySQL is running.
- Open your terminal and log in to MySQL using the root account:
- Enter the root password when prompted. Once you’re logged in, create a new database like this:
- Of course, replace “mydatabase” with whatever name you want to give your database. To use the newly created database, just run:
$ sudo mysql -u root -p
CREATE DATABASE mydatabase;
USE mydatabase;
Now you can start creating tables, inserting data, and querying away! Easy, right?
What is the default MySQL root password on Ubuntu?
Here’s something important to note: MySQL doesn’t set a root password during installation on Ubuntu. Instead, you’ll be prompted to set one during the installation process. If you don’t set a password at that time, don’t worry! You can log in as root with
$ sudo mysql
How do I start and stop MySQL on Ubuntu?
Starting and stopping MySQL is as simple as running a couple of commands. To start MySQL, just run:
$ sudo service mysql start
And if you need to stop MySQL, it’s just as easy:
$ sudo service mysql stop
These commands give you full control over the MySQL service, so you can start or stop it as needed.
Can I install multiple MySQL versions on Ubuntu?
Yes, absolutely! Docker is your friend here. Docker lets you run different versions of MySQL in isolated containers, so you can easily manage them without them stepping on each other’s toes. Here’s how you can set up two different versions—MySQL 5.7 and MySQL 8.0:
$ 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
This will spin up MySQL 5.7 and MySQL 8.0 in separate containers. You can use them side by side without any conflicts. It’s like having two different MySQL versions living peacefully on the same server.
How do I completely uninstall MySQL from Ubuntu?
If you’ve had enough of MySQL and want to completely uninstall it, you can run these commands to clean it out:
$ sudo apt purge mysql-server mysql-client mysql-common
$ sudo apt autoremove
$ sudo apt autoclean
This will remove MySQL server, client, and all common files from your system. The autoremove command ensures any unnecessary dependencies are also cleaned up, while autoclean helps tidy up any leftover files from the uninstallation.
What’s the difference between MariaDB and MySQL on Ubuntu?
Here’s a fun one! MariaDB is a fork of MySQL, created with the goal of providing a more open-source friendly alternative. The good news is, MariaDB is fully compatible with MySQL, so if you’re using MySQL in your application, it’ll likely work seamlessly with MariaDB.
The main differences come down to performance and features. MariaDB includes some optimizations that make it a better choice for high-performance applications, and it’s fully open-source. MySQL, on the other hand, is owned by Oracle and offers a commercial version with additional proprietary features.
If you want to switch to MariaDB, it’s easy to do so on Ubuntu with this command:
$ sudo apt update && sudo apt install mariadb-server
So, whether you go with MySQL or MariaDB, both are solid choices, but your decision might depend on your performance needs and how much you value the open-source nature of your database.
For further details on MySQL licensing, refer to MySQL Licensing Information.
Conclusion
In this guide, we’ve walked through every step needed to install MySQL 8.0 on an Ubuntu 20.04 server, from setting up the server to securing the installation and managing users. With MySQL’s flexibility and Ubuntu’s reliability, you now have a solid foundation for managing databases efficiently. Along the way, we also compared MySQL with MariaDB, pointed out common installation issues, and provided tips for tuning performance to ensure your MySQL server runs smoothly.As you move forward, remember that proper configuration and security setup are key to maximizing MySQL’s performance. Regularly updating and optimizing your MySQL setup will keep your database secure and efficient. If you’re new to MySQL, experimenting with different configurations and exploring advanced features will help you build a strong database environment for your applications.Looking ahead, with MySQL’s continual updates and new features, you’ll want to stay updated with the latest versions to ensure you’re always working with the most secure and efficient version of MySQL on Ubuntu.
How to Manage MySQL Users: Creating, Assigning Permissions, and Securing Access (2025)