Install MySQL on Ubuntu 20.04: The Definitive Guide
Want to install MySQL on Ubuntu 20.04 but struggling with outdated instructions? This step-by-step guide provides the latest, most accurate method for setting up MySQL 8.0, even tackling common installation pitfalls! Learn how to install MySQL, configure it securely, and create dedicated users for optimal database management.
Why Install MySQL on Ubuntu 20.04?
MySQL is a powerful, open-source database management system perfect for web development. Installing MySQL on Ubuntu 20.04 provides a robust foundation for applications requiring relational databases, offering benefits like:
- Data Integrity: Ensures data consistency and reliability.
- Scalability: Handles growing data needs as your application expands.
- Security: Protects sensitive information with robust security features.
- Flexibility: Compatible with various programming languages and frameworks.
Prerequisites: Getting Your Server Ready
Before diving into the installation, make sure you have this one critical thing:
- An Ubuntu 20.04 server with a non-root user with sudo privileges and a UFW firewall. Follow DigitalOcean's initial server setup guide for Ubuntu 20.04
Step 1: Installing the MySQL Server Package
This section covers installing the mysql-server
package using Ubuntu's apt
package manager.
-
Update Package Index: First, update your server's package list. This ensures you're retrieving the newest software versions from the repositories:
-
Install MySQL: Now, install the
mysql-server
package. This pulls all the necessary files and dependencies to set up your MySQL database: -
Start the MySQL Service: Verify the MySQL server is activated and running.
Step 2: Configuring MySQL for Enhanced Security
Securing your MySQL installation is paramount. This step focuses on running the mysql_secure_installation
script to harden your database server.
Important Warning: A known bug exists in recent mysql_secure_installation
versions regarding the root password. Follow these steps before running the script to prevent errors:
-
Access the MySQL Prompt: Open the MySQL command-line interface using
sudo
: -
Alter Root Authentication: Run this command to change the root user's authentication to a password-based method:
Replace
'your_strong_password'
with a strong, unique password. -
Exit: Leave the MySQL prompt by typing:
-
Run Security Script: Now execute the
mysql_secure_installation
script: -
Follow Prompts: The script will ask about:
- Validate Password Plugin: Improve password security by enforcing strength requirements.
- Root Password: Set (or reset) the root password.
- Remove Anonymous Users: Disable anonymous user accounts for added security.
- Disallow Remote Root Login: Prevent remote access to the root account, reducing vulnerability.
- Remove Test Database: Delete the default test database to minimize attack surface.
- Reload Privilege Tables: Apply the settings.
-
Revert Root Authentication (Recommended): After the script, revert back to the more secure default
auth_socket
:
Step 3: Creating a Dedicated MySQL User
Avoid using the root account for routine database operations. Create a dedicated user with specific privileges.
-
Access MySQL Prompt: Connect to MySQL as the root user:
-
Create a New User: Replace
sammy
with their desired new name andpassword
with well, a secure password:Consider using
mysql_native_password
if you're having PHP connectivity issues. -
Grant Privileges: Grant the user necessary permissions --here's an example:
-
Flush Privileges: Apply recent privilege changes:
-
Exit MySQL:
Step 4: Testing Your MySQL Installation
Confirm that MySQL is running correctly.
-
Check MySQL Status: Verify if the service is running:
-
Connect to MySQL as the New User: Log in using the created user credentials:
Enter the password that corresponds to
sammy
for authentication.
Congratulations! MySQL is now successfully installed and configured on your Ubuntu 20.04 server. You’re ready to create and manage databases for your web applications.