MySQL is one of the most popular open-source relational database management systems, widely used for building and managing databases in various applications. MySQL is a powerful database management system that can handle various data-driven applications. If you're an Ubuntu 22 user and want to install MySQL, this article will provide you with a detailed step-by-step guide to help you through the process.

mysql in ubuntu

Step 1: Update System Packages.

Before installing any software, it's essential to update the system packages to ensure you have the latest versions. Open a  ssh terminal and run the following commands:

sudo apt update -y
sudo apt upgrade -y

 

Step 2: Install MySQL Server

To install MySQL on Ubuntu 22, use the following command:

sudo apt install mysql-server -y

During the installation, you will be prompted to set a root password. Enter a strong password and keep it safe. Once the installation is complete, MySQL will start automatically.

 

Step 3: Configure MySQL Security.

For security purposes, it's recommended to run the MySQL secure installation script. This script allows you to set several security-related options. Run the following command in the terminal:

sudo mysql_secure_installation

You'll be prompted to enter the root password you set during installation. Follow the prompts and answer the questions based on your security preferences.

 

Step 4: Verify MySQL Installation.

To check if MySQL is running correctly, you can use the following command to start the MySQL command-line interface:

sudo mysql

If you successfully enter the MySQL shell, the installation was successful.

Exit the MySQL shell typing:

> exit

 

Step 5: Basic MySQL Configuration.

By default, MySQL listens only on the localhost (127.0.0.1) IP address. If you want to allow connections from remote machines, you need to edit the MySQL configuration file.

Open the file using a text editor with root privileges:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Locate the line bind-address = 127.0.0.1 and replace it with bind-address = 0.0.0.0 to allow connections from any IP address.

Save the changes and exit the text editor.

 

Step 6: Restart MySQL Service.

To apply the changes made in the configuration file, restart the MySQL service with the following command:

sudo systemctl mysql restart

If you want the MySQL Server to start autoamtically each time the Ubuntu Machine reboots then do:

sudo systemcl mysql enable

 

Step 7: Test Remote Connection (Optional)

If you modified the MySQL configuration to allow remote connections, you can test it from another machine.

Use a MySQL client software such as MySQL Workbench or the command-line tool to connect to the MySQL server using the IP address of your Ubuntu machine.

Further reading: