
MySQL is a popular open-source relational database used to store and manage data efficiently. Installing it on Ubuntu 20.04 provides a stable setup for building and securing database-driven applications.
This article covers the steps to install and configure MySQL on Ubuntu 20.04. For a similar setup on a different system, check out our article on installing MySQL on Debian 12.
Before you begin:
Have an Ubuntu 20.04 server.
Access the server using SSH as a non-root user with sudo privileges.
MySQL is included in Ubuntu 20.04's default APT repositories. To install it, update your package index and install the latest MySQL server version using the APT package manager.
Update the server package index.
Install the MySQL server package.
View the installed MySQL version on your server.
Output:
MySQL runs as the mysql system service on Ubuntu 20.04. Enable it to start at boot and verify its status to manage the database processes.
Enable MySQL to automatically start at boot time.
Output:
Start the MySQL database server.
View the MySQL server status and verify that it's running.
Output:
Securing MySQL helps protect your databases from unauthorized access. Set a strong root password and remove insecure defaults using the steps below.
Run the following command to start the MySQL secure installation script.
Follow these steps to secure your MySQL database server:
y and press Enter to activate password validation.2 to enforce the use of strong passwords.y to confirm the new root password.y to revoke access from unauthorized users.y to prevent root user login from remote hosts.y to remove any test databases.y to apply the changes immediately.Log in to the MySQL database server as root.
Set a strong password for the root user.
Replace your_strong_password with a strong password depending on your password strength policy.
Reload the MySQL Privilege tables to apply changes.
Exit the MySQL database console.
Restart the MySQL service to apply your configuration changes.
Log in to the MySQL database server as root.
Enter the root user password you set earlier when prompted.
Create a new sample MySQL database. For example, my_database.
Create a new MySQL database user with a strong password. For example, my_user and replace my_password with your desired password depending on your password strength policy.
Grant the database user my_user full privileges to your sample database my_database.
Grant the user my_user permission to create databases on the server.
Grant the user my_user permission to perform CRUD operations (SELECT, INSERT, UPDATE, DELETE) on all databases.
Refresh the MySQL privilege tables to apply the new user changes.
Exit the MySQL database console.
Non-privileged MySQL users, like my_user, can create databases and tables. In this example, my_user creates a database, adds a table, inserts data, and queries the records.
Log in to the MySQL database console using the database user my_user you created earlier.
Enter the my_user password you set earlier when prompted.
List all databases available to the MySQL user
Output:
Create another sample MySQL database. For example, bookstore_db.
Switch to the new database.
Create a new sample table with the 3 columns to store different data types.
Insert sample data into the sample_table table.
Select all table records to verify that the new table data is available.
Output:
You have installed MySQL on your Ubuntu 20.04 server and secured it for authenticated access. MySQL integrates with modern web applications and can serve as a dedicated database server or backend for dynamic stacks like LAMP. For more details, visit the official documentation.
0 Comments
Be the first to comment and share your perspective with the community.