How to Install and Configure MySQL on Ubuntu 23.04

Updated: January 27, 2024 By: Khue Post a comment

This step-by-step tutorial helps you with setting up and configuring MySQL on Ubuntu 23.04 LTS.

1. Update the package index on your server by running the following command:

sudo apt update

2. Install the mysql-server package by executing this one:

sudo apt install mysql-server

3. Start the MySQL service with the command below:

sudo systemctl start mysql.service

You can use the mysql --version command to see the version of MySQL installed on your server. If MySQL is not installed, you will get an error message like mysql: command not found.

4. Run the MySQL security script to change some of the default settings and make your installation more secure. You can do this as shown below:

sudo mysql_secure_installation

5. Follow the prompts to set a password for the root user, remove anonymous users, disable remote root login, remove the test database, and reload the privilege tables.

6. This step is optional. What we’re doing here is configuring MySQL to listen for connections from network hosts. By default, MySQL only listens for connections from the local host, which means that only applications running on the same machine can access the database. This is done by setting the bind-address directive to 127.0.0.1, which is a special IP address that refers to the local host.

However, if you want to allow remote connections to your MySQL server from other machines, you need to change the bind-address directive to either 0.0.0.0 or the IP address of your server. 0.0.0.0 means that MySQL will accept connections from any host, while a specific IP address means that MySQL will only accept connections from that host. For instance, if your server’s IP address is 192.168.0.5, you can set the bind-address directive to 192.168.0.5 to allow only connections from that address.

To change the bind-address directive, you need to edit the file /etc/mysql/mysql.conf.d/mysqld.cnf on your server using a text editor such as Vim or Nano. You need to find the line that says bind-address = 127.0.0.1 and replace it with bind-address = 0.0.0.0 or bind-address = 192.168.0.5 depending on your preference. You also need to save the file and restart the MySQL service for the changes to take effect.

Here is an example of how to edit the file using Vim:

# Open the file with sudo privileges
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

# Find the line that says bind-address = 127.0.0.1
# Press / and type bind-address to search for it
# Press n to go to the next occurrence if needed

# Replace 127.0.0.1 with 0.0.0.0 or 192.168.0.5
# Press i to enter insert mode
# Use the arrow keys to move the cursor and delete and type as needed
# Press Esc to exit insert mode

# Save and quit the file
# Press : and type wq and press Enter

# Restart the MySQL service
sudo systemctl restart mysql.service

Note that 192.168.0.5 is just a sample IP address used for demonstration purposes. Don’t forget to replace it with your own one.