
MySQL is a popular open-source relational database management system (RDBMS) that uses SQL to store, manage, and retrieve data efficiently. It’s widely used for web applications, data storage, and complex data management. Installing MySQL on Ubuntu 22.04 provides a stable environment for building and securing databases.
In this article, you’ll learn how to install and configure MySQL on Ubuntu 22.04, from system updates to securing your database. If you’re working with other Linux distributions, you can check out our article on installing MySQL on Debian 12 for a similar approach.
Before you begin:
Have an Ubuntu 22.04 server.
Access the server using SSH as a non-root user with sudo privileges.
MySQL is available in the default APT repositories on Ubuntu 22.04. You can install the latest version directly or use a PPA for a specific release. Follow the steps below to update your server and install MySQL.
Update the server package index.
Install the MySQL server package.
View the installed MySQL version on your server.
Output:
MySQL runs under the mysql system service to manage database processes on Ubuntu 22.04. Follow the steps below to enable the service to start at boot and verify its status.
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 the MySQL server is essential to protect databases from unauthorized access. By default, privileged users like root can access the MySQL console without a password. Follow the steps below to set a new root password and disable insecure defaults to secure your database server.
Run the following command to start the MySQL secure installation script.
Follow the script prompts below to set up a new root user password, remove anonymous users, disallow remote root login, and remove test databases on your MySQL database server.
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 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.
By default, non-privileged MySQL users have CREATE privileges on the database server. In the following steps, we’ll create an example database using the non-privileged user my_user and add new records to the database tables.
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:
In this article, you installed MySQL on your Ubuntu 22.04 server and secured the database to ensure authenticated access for valid users. MySQL integrates seamlessly with modern web applications, and you can use your server as a dedicated remote database server or as the database backend for dynamic application stacks such as LAMP.
0 Comments
Be the first to comment and share your perspective with the community.