How to Install MySQL on Ubuntu – Step-by-Step Guide
MySQL is a popular open-source database management system that often comes as part of the LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. This guide will walk you through the process of installing MySQL version 8.0 on an Ubuntu 20.04 server.
Prerequisites
To follow this tutorial, you’ll need:
- An Ubuntu server with a non-root administrative user.
- A firewall configured with UFW.
Step 1 — Installing MySQL
You can install MySQL using the APT package repository. At the time of writing, the version available in the default Ubuntu repository is 8.0.27.
Begin by updating the package index:
sudo apt update
Next, install the mysql-server
package:
sudo apt install mysql-server
Ensure the server is running:
sudo systemctl start mysql.service
Step 2 — Configuring MySQL
Once MySQL is installed, run the mysql_secure_installation
script to improve security. This script allows you to change default settings regarding remote root logins and sample users.
Note: If you run into problems with the mysql_secure_installation
script due to the root user’s authentication method, adjust it first.
Open the MySQL prompt:
sudo mysql
Change the root user’s authentication method:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
Exit the MySQL prompt:
exit
Now, run the security script:
sudo mysql_secure_installation
You can choose to enable password validation and set a new password for the root user.
Step 3 — Creating a Dedicated MySQL User and Granting Privileges
It’s advisable to create a new user for regular database operations instead of using the root account.
Access the MySQL prompt again:
sudo mysql
Create a new user:
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
Grant privileges to your new user:
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT ON *.* TO 'your_username'@'localhost' WITH GRANT OPTION;
Use the FLUSH PRIVILEGES
command to update the privileges:
FLUSH PRIVILEGES;
Exit the MySQL client:
exit
Step 4 — Testing MySQL
Check whether MySQL is running:
systemctl status mysql.service
To test access with your new user, run:
mysql -u your_username -p
You will be prompted for your password. If you can log in successfully, the installation is successful.
Additional Information
If you’re considering alternatives to MySQL, MariaDB is a popular fork that’s compatible with MySQL and might offer features that are more aligned with your needs. You can easily swap out MySQL for MariaDB in your applications.
Conclusion
With MySQL installed and configured, you can now manage your databases effectively. Explore further by setting up a web stack or integrating MySQL into your applications. DigitalOcean also offers managed MySQL hosting if you prefer a fully managed solution. Leave the complexity to them while you focus on building your applications.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.