Back to articles list Articles Cookbook
9 minutes read

How to Install MySQL on Ubuntu

Looking to install MySQL on Ubuntu? Delve into the world of efficient data management with this comprehensive guide. From updating your package list to securing your installation and ensuring smooth configurations, these step-by-step instructions will help you seamlessly set up MySQL on your Ubuntu system.

If you’re reading this article, you’re seriously interested in working with data. What you need is a suitable tool to make it happen. If you’re considering installing MySQL on an Ubuntu Linux system, you may wonder if this combination is a good choice. It is!

This article walks you through installing MySQL on Ubuntu – perfect for data enthusiasts looking to tap into the combined power of Ubuntu and MySQL. We'll discuss why MySQL is so popular, how Ubuntu pairs with it seamlessly, and how you can install MySQL on this popular Linux distro.

Let's dive in together and get your Ubuntu system ready for efficient data management with MySQL.

Why MySQL?

MySQL is an open-source relational database management system (RDBMS) that is widely used for managing and organizing data. Its reliability, scalability, and ease of use make it a popular choice for building and maintaining databases among developers and businesses.

MySQL is commonly used for web applications, content management systems, e-commerce platforms, and various other types of software that require a robust and reliable database management system. It is also frequently integrated with programming languages like PHP, Python, Java, and others to build dynamic and data-driven applications.

Sounds good to you? If you want to learn and work with MySQL, you should start with reliable basic knowledge. If you are not sure which course to choose, our article about the Best MySQL Courses For Beginners can help you.

Why is MySQL used so widely? The list of advantages is long. To mention only a few here, MySQL can handle databases of varying sizes, from small projects to large enterprise-level applications. It supports scalability through features such as replication, clustering, and sharding. It is released under an open-source license, which means that its source code is freely available and users can modify, distribute, and use it.

MySQL is known for its performance and speed in processing database queries. It employs various optimization techniques to ensure efficient data retrieval and manipulation. It also provides security features (such as user authentication, encryption, and access controls) to protect data and ensure that only authorized users have access to the database.

This RDBMS has a large and active community of developers and users. This community contributes to ongoing development, provides support, and shares knowledge through forums, documentation, and other resources.

MySQL is designed to run on various operating systems, including Linux, Windows, and macOS, making it versatile and compatible with different environments. In this article we will focus on Linux, showing you how to install it specifically on Ubuntu. But maybe you also need a quick refresh on how to install MySQL on Windows. If so, read our article with instructions to it.

Why Run MySQL on Ubuntu?

Ubuntu is a popular and user-friendly Linux distribution (operating system) based on Debian. It is open-source, free to use, and has both a desktop and server version. The operating system is developed by the British company Canonical and a community of other developers. Interestingly, it is led under a meritocratic governance model. As of October 2023, the most recent release is 23.10 ("Mantic Minotaur") and the current long-term support release is 22.04 ("Jammy Jellyfish").

You may have already guessed that Ubuntu is a preferred platform for running MySQL. It is known for its ease of use, regular release cycles, and strong community support. Its source code is freely available; users can modify, distribute, and use it without licensing fees.

Ubuntu is known for its stability and reliability, especially the Long-Term Support (LTS) releases. LTS versions receive updates and support for an extended period (usually five years), making them suitable for servers and mission-critical environments. It also benefits from a strong security model. The distribution provides regular security updates, and its repository includes a wide range of secure software packages.

Ubuntu is designed to be user-friendly, even for individuals new to Linux. The installation process is straightforward, and its desktop environment provides an intuitive user interface. The Software Center allows users to easily install, update, and manage applications. It uses the Advanced Package Tool (APT) for package management, making it easy to install, update, and remove software packages. The APT system automatically resolves dependencies, simplifying the software installation process.

Like MySQL, Ubuntu has a large and active community of users and developers. This community support is invaluable for troubleshooting, getting help with issues, and sharing knowledge. What is more, Ubuntu follows a predictable release schedule, providing users with regular updates and new features. This helps users stay current with the latest software advancements.

Ubuntu provides regular security updates for its packages, including MySQL. This helps in maintaining a secure and up-to-date environment, reducing the risk of vulnerabilities and ensuring the safety of the data stored in MySQL databases.

Before you go on to the instructions, check the latest Ubuntu release. Here you will find the description of installing Ubuntu 22.04, which will be supported until April 2027. Interested in other Linux flavors? Check out our Top 5 Linux Distributions for SQL.

Installing MySQL on Ubuntu

Before you start, ensure your internet connection is stable – you’ll need it. Then, remember about the preparation steps. A best practice is to ensure your package list is up to date.

These instructions may change over time, so it's always a good idea to check the official documentation for the most up-to-date information. Use reliable sources. You can find useful instructions on Ubuntu’s site.

There are various editions of MySQL provided by Oracle. The Community edition is available for free. You can download it from the MySQL website.

How to Install MySQL on Ubuntu

Step 1: Update the Package

Let’s get started. Open a terminal prompt to start the installation process using the Ubuntu operating system. Check the updates by typing in:

sudo apt-get update

This command fetches the latest information about available packages from the repositories configured on your system.

Step 2: Install MySQL

After updating the package list, you can proceed with the installation of MySQL. Type in:

sudo apt-get install mysql-server

The installer installs MySQL and all dependencies. After the installation, it's recommended to run a security script that comes with MySQL. This script will help you secure your installation by setting a root password, removing anonymous users, disallowing remote root login, and more. The secure installation utility should launch automatically after the installation completes. If it doesn’t, enter the following command:

sudo mysql_secure_installation utility

Step 3: Set a Strong Password

This utility prompts you to define the MySQL root password and other security-related options, including removing remote access to the root user and setting the root password. You can choose 'Y' or 'N' based on your preferences.

Make sure to replace your_password with a strong and secure password.

Step 4: Enable Remote Access (Optional)

If you have iptables enabled and want to connect to the MySQL database from another machine, you must open a port in your server's firewall (the default port is 3306). You don't need to do this if the application that uses MySQL is running on the same server.

To allow remote access to the MySQL server, run the following command:

sudo ufw enable
sudo ufw allow mysql

Step 5: Restart

After you have set the root password and made other changes, restart the MySQL service to apply the new configurations:

sudo systemctl restart mysql

If the service has already started, you will get a message informing you that it is already running:

sudo systemctl start mysql

Step 6: Basic Configuration

Performing basic configuration tasks in MySQL on Ubuntu involves adjusting character sets and collations and ensuring the MySQL service starts automatically during system boots.

To adjust character sets and collations, open the MySQL configuration file in a text editor. The configuration file is usually located at /etc/mysql or /etc/mysql/my.cnf. At this point, it’s a good idea to make a backup of the file before making changes.

If the files are not there, you can use mysqld to find the configuration. Type in the following command:

$ /usr/sbin/mysqld --help --verbose

The configuration file contains lines with a single word in square brackets, like [client] or [mysqld]. Those sections are configuration groups. They make important configuration elements more visible to the programs that read the configuration file.

MySQL should start automatically during system boot. However, if it's not set up to do that, you can enable it. First, check the status of the MySQL service:

sudo systemctl status mysql

If it's not enabled, you can use this command, which will configure MySQL to start automatically during system boot:

sudo systemctl enable mysql

Step 7: Restart (Again)

After making changes to the configuration, you can restart the MySQL service with the command you already know:

sudo systemctl restart mysql

You can also verify that MySQL is running without any issues:

sudo systemctl status mysql

Look for any error messages to ensure that the service started successfully. After making all the needed changes, your MySQL server should be configured with the specified character set and collation. It should start automatically during system boot.

And that's it! You've installed and secured MySQL on your Ubuntu system. Remember to refer to the official MySQL documentation for any additional configuration or troubleshooting.

Now that you have the database ready to use, test it. Connect to MySQL and execute basic queries to confirm that the database server is operational.

Your First Steps with MySQL on Ubuntu

As you can see, the Ubuntu MySQL installation is not a very complicated process. When you start using it, the most important thing is to practice. Don’t worry if everything isn’t easy at the beginning. Continue learning – our SQL From A to Z in MySQL pathway is an excellent choice if you want to learn all MySQL has to offer. It contains seven interactive courses that start from the very beginning and guide you to MySQL proficiency.

How to Install MySQL on Ubuntu

If you know basic MySQL and are looking for more advanced knowledge, you can focus on building your skills in SQL JOINs, aggregation, grouping, and subqueries. Our SQL Practice in MySQL has almost 200 coding challenges to help you progress.

Finally, check out our list of Top 10 MySQL Interview Questions. Do you know all the answers? Don’t worry if you still have something to work on. Install MySQL and continue improving your skills. If you want, share your impression on MySQL setup and Ubuntu database setup. We’d like to hear about it!