MySQL is one of the most popular relational database management systems in the world. It’s widely used for everything from managing small-scale websites to supporting enterprise-level applications. If you’re working with Ubuntu, whether for development or deployment, knowing how to install and configure MySQL is an essential skill. In this guide, we’ll walk you through the process of install MySQL on Ubuntu, providing you with the necessary commands, tips, and troubleshooting advice to get your system up and running.
Why Choose MySQL for Your Ubuntu Server?
Before diving into the installation, let’s first understand why MySQL is a great choice for Ubuntu users.
- Popularity: MySQL is one of the most widely-used open-source databases. It’s reliable, fast, and easy to set up, making it an excellent choice for developers, system administrators, and businesses.
- Compatibility: MySQL runs smoothly on Ubuntu and is supported across most versions, including the long-term support releases like Ubuntu 20.04 and 22.04. The robust community and extensive documentation ensure you’ll have plenty of resources to troubleshoot and improve your setup.
- Scalability: MySQL is known for its high performance, making it scalable for both small applications and larger, high-traffic websites or services.
- Support for Complex Queries: MySQL supports a wide variety of complex queries and joins, making it ideal for applications that need structured data management.
Prerequisites for Install MySQL on Ubuntu
Before starting the installation, there are a few prerequisites:
- Ubuntu Version: MySQL is compatible with most versions of Ubuntu, including 20.04, 22.04, and newer. This guide focuses on these versions, but the commands will work for other Ubuntu releases as well.
- Sudo or Root Privileges: You’ll need sudo access to install software on your system. If you don’t have root privileges, contact your system administrator.
- Updated System: It’s always a good idea to ensure your system is up to date with the latest patches and security updates.

Step 1: Update Ubuntu Package Repository
Before installing any software, it’s essential to update the Ubuntu package repository to ensure that your system is aware of the latest available packages.
Open your terminal and run the following command:
bashCopysudo apt update
This command updates the list of available packages from the repository. If there are any updates to the system packages, this will ensure that the installation process runs smoothly.
Step 2: Install MySQL Server
Once your package repository is up to date, it’s time to install MySQL. You can do this easily using the following command:
bashCopysudo apt install mysql-server
This command installs MySQL and its dependencies. The installation process may take a few minutes, depending on your internet connection and system speed.
Step 3: Verify MySQL Installation
Once MySQL is installed, it’s important to verify that it’s running properly. You can check the status of the MySQL service with the following command:
bashCopysudo systemctl status mysql
You should see a message indicating that MySQL is active (running). If it’s not running, you can start the service using:
bashCopysudo systemctl start mysql
Additionally, you can enable MySQL to start automatically upon system boot with the following command:
bashCopysudo systemctl enable mysql
Step 4: Secure MySQL Installation
MySQL comes with some default settings that might not be secure. For example, there’s a root user that doesn’t have a password by default. To address these issues and improve the security of your installation, MySQL provides a built-in security script called mysql_secure_installation
.
Run the script with the following command:
bashCopysudo mysql_secure_installation
This script will guide you through several prompts to help secure your installation:
- Set a root password: You’ll be prompted to create a strong password for the MySQL root user. This password is essential for managing MySQL.
- Remove insecure default settings: You can disable symbolic-links and remove default test databases, which aren’t needed in a production environment.
- Disable remote root login: For additional security, it’s recommended to disable remote root login. This makes it harder for attackers to gain unauthorized access.
- Remove anonymous users: This step removes the default anonymous user, which can be a security risk.
After completing these prompts, your MySQL installation will be more secure.
Step 5: Verify MySQL Installation and Access
To confirm that MySQL is installed and configured correctly, you can log into the MySQL shell. Use the following command:
bashCopysudo mysql -u root -p
You’ll be prompted for the root password you created earlier. Once entered, you should see the MySQL prompt:
sqlCopymysql>
You can now execute MySQL commands. For example, you can list all databases with:
sqlCopySHOW DATABASES;
Step 6: Create Databases and Users
Now that MySQL is up and running, let’s move on to creating a database and user. Here’s how you can create a database and assign user privileges:
- Create a new database:
sqlCopyCREATE DATABASE my_database;
- Create a new user:
sqlCopyCREATE USER 'my_user'@'localhost' IDENTIFIED BY 'password';
- Grant privileges to the user:
sqlCopyGRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'localhost';
- Flush privileges to apply the changes:
sqlCopyFLUSH PRIVILEGES;
You can now exit MySQL with:
sqlCopyEXIT;
This user can access the my_database
database using the password you specified.
Step 7: Configuring MySQL for Remote Access (Optional)
If you need to access MySQL from a remote machine, you’ll need to configure MySQL to allow remote connections. By default, MySQL is configured to accept connections only from the local machine.
Here’s how to enable remote access:
- Edit the MySQL configuration file:
bashCopysudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Look for the bind-address
directive and change its value from 127.0.0.1
(local only) to 0.0.0.0
(all IP addresses) or your server’s IP address.
bashCopybind-address = 0.0.0.0
- Restart MySQL to apply the changes:
bashCopysudo systemctl restart mysql
- Allow remote access in the firewall (if applicable):
bashCopysudo ufw allow from any to any port 3306 proto tcp
After making these changes, MySQL should accept remote connections.
Troubleshooting Common MySQL Installation Issues
Here are a few common problems you might encounter during installation:
- Error:
mysql.service failed to start
This error can happen if there’s a problem with the configuration file or insufficient memory. Check the MySQL error logs (/var/log/mysql/error.log
) for more details. - Error:
Access denied for user
If you’re receiving this error, ensure that you’ve granted the correct privileges to the user and that you’re using the correct username and password. - Port 3306 is blocked
If you’re unable to connect remotely, ensure that your firewall isn’t blocking the default MySQL port (3306). You may need to adjust your firewall settings.
Conclusion
Install MySQL on Ubuntu is a straightforward process that involves updating your system, installing MySQL, securing your installation, and configuring it for your specific needs. Whether you’re setting up a local development environment or a production server, following these steps will ensure you have a secure and functional MySQL installation.
From creating databases to granting users access, the steps provided here give you the foundation you need to start working with MySQL on Ubuntu. For advanced setups, consider optimizing your MySQL configuration for performance or exploring advanced features like replication and backup strategies.
Check out our other guides on how to install!