How to Install MySQL on Ubuntu 20.04: A Complete Guide
Looking to install MySQL on Ubuntu 20.04? This comprehensive guide will walk you through each step, ensuring a secure and functional database server for your applications. We'll cover everything from initial installation to creating users and granting appropriate privileges.
Why Install MySQL on Ubuntu?
MySQL is a popular, open-source relational database management system used in a wide range of applications. Installing it on Ubuntu provides a robust and reliable platform for storing and managing your data.
- Open Source: Benefit from a free and community-supported database solution.
- Reliability: Leverage a stable and widely-used database system.
- Flexibility: Easily integrate with various programming languages and frameworks.
Prerequisites: Server Setup
Before installing MySQL, ensure you have the following:
- An Ubuntu 20.04 server.
- A non-root administrative user.
- A firewall configured with UFW (Uncomplicated Firewall).
Refer to DigitalOcean's initial server setup guide for Ubuntu 20.04 for detailed instructions.
Step 1: Installing the MySQL Server Package
The easiest way to install MySQL on Ubuntu is using the APT package repository. Follow these steps:
-
Update Package Index: Keep your system up-to-date.
-
Install MySQL: Use apt to retrieve the mysql-server package.
-
Start MySQL Service: Start the database server.
This will download and install MySQL version 8.0.
Step 2: MySQL Secure Installation Configuration
After installation, configure MySQL for enhanced security. This involves running the mysql_secure_installation
script.
Important: Due to authentication changes, you may encounter an error. We will address this first.
-
Access MySQL Prompt: Open up the MySQL prompt.
-
Alter User Authentication: Change the MySQL root user's authentication method to
mysql_native_password
. Replace'password'
with a secure password. -
Exit MySQL Prompt: Exit the MySQL environment before running the script.
-
Run Security Script: Execute the secure installation script.
Follow the prompts:
- Validate Password Plugin: Consider setting this up for stronger passwords.
- Root Password: Set a strong password for the MySQL root user.
- Remove Anonymous Users: Recommended for security.
- Disallow Remote Root Login: Enhances security.
- Remove Test Database: Reduces potential vulnerabilities.
- Reload Privilege Tables: Ensures immediate application of changes.
- Change Authentication Back (Optional):
Once the security script completes, you can then reopen MySQL and change the root user’s authentication method back to the default, auth_socket
. To authenticate as the root MySQL user using a password, run this command:
mysql -u root -p
Then go back to using the default authentication method using this command:
ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
Step 3: Creating a Dedicated MySQL User
For security reasons, avoid using the root account for regular database operations. Create a dedicated user with specific privileges.
-
Access MySQL as Root: Log in to MySQL as the root user.
Or, if you've configured a password for root:
-
Create a New User: Replace
sammy
with your desired username andpassword
with a strong password.Note: If you encounter issues with PHP applications, use the
mysql_native_password
plugin: -
Grant Privileges: Grant the new user the necessary privileges. Adjust privileges based on your needs.
Warning: Avoid granting
ALL PRIVILEGES
unless absolutely necessary. -
Flush Privileges: Reload the grant tables.
-
Exit MySQL: Exit the MySQL client.
Step 4: Testing your MySQL Installation
Verify that MySQL is running correctly.
-
Check MySQL Status: Use systemctl to check the server.
-
Connect to MySQL: Log in as your newly created user.
You should be prompted for the user's password. After entering it successfully, you'll have access to the MySQL command-line interface.
Long-Tail Keywords Summary
Remember these long-tail keywords when optimizing your MySQL setups:
- Install MySQL on Ubuntu 20.04: The main goal covered in the guide.
- Secure MySQL installation Ubuntu: We covered best practices for securing your MySQL server.
Conclusion
Congratulations! You've successfully installed and configured MySQL on Ubuntu 20.04. This guide provides a solid foundation for building and deploying your data-driven applications. Remember to regularly update and maintain your MySQL installation for optimal performance and security.