How to Install MySQL in Linux – Ubuntu 18.04

MySQL is a popular open-source relational database management system (RDBMS) used by many developers and system administrators. In this tutorial, we will guide you through the process of installing MySQL on Ubuntu 18.04.

Step 1: Updating the System
Before installing any new software, it is recommended to update the system to ensure you have the latest packages and security updates. Open a terminal and execute the following commands:

sudo apt update -y

The first command (sudo apt update -y) updates the package lists.

Step 2: Installing MySQL
To install MySQL on Ubuntu 18.04, you can use the apt package manager. Open a terminal and run the following command:

sudo apt install mysql-server -y

This command will install the MySQL server package along with its dependencies. During the installation process, you will be prompted to set a password for the MySQL root user. Enter a strong password and remember it, as you will need it later.

Step 3: Starting MySQL Service
Once the installation is complete, you can start the MySQL service by running the following command:

sudo systemctl start mysql

This command starts the MySQL service, allowing you to connect to the database server.

Step 4: Configuring MySQL
To configure MySQL and perform administrative tasks, such as creating databases and managing users, we can use the MySQL command-line client. Start the client by running the following command:

sudo mysql

This command opens the MySQL shell, where you can execute SQL statements and interact with the database server.

Step 5: Updating MySQL Root Password
By default, MySQL uses the auth_socket plugin to authenticate the root user. However, this authentication method may not work with certain applications or scripts. To change the authentication method to a password-based one, follow the steps below.

Switch to the mysql database:

use mysql;

Update the authentication method and set a new password for the root user (replace ‘1234’ with your desired password):

UPDATE user SET authentication_string='1234' WHERE user='root';

Flush the privileges to reload the updated user table:

flush privileges;

Create a new MySQL user with root privileges and a password (replace ‘1234’ with your desired password):

CREATE USER 'root'@'%' IDENTIFIED BY '1234';

Grant all privileges to the new user:

GRANT ALL PRIVILEGES ON . TO 'root'@'%';

Verify the changes by executing the following command:

SELECT User,Host FROM mysql.user;

This command displays a list of MySQL users and their corresponding hostnames. You should see a new user named ‘root’ with the hostname ‘%’.

Step 6: Cleaning Up
For security reasons, it is recommended to remove the root user’s access from the localhost host. Run the following command to delete the user (this assumes you have successfully created the new ‘root’@’%’ user in the previous step):

DROP USER 'root'@'localhost';

Step 7: Creating a Database
To demonstrate the database creation process, let’s create a sample database named 99faults. Run the following commands:

CREATE DATABASE 99faults;

Step 8: Verifying the Changes
To verify the changes made to the MySQL users, execute the following command:

SELECT User,Host FROM mysql.user;

This command displays the MySQL users and their corresponding hostnames. You should see the newly created database and the updated user information.

Congratulations! You have successfully installed and configured MySQL on Ubuntu 18.04. You can now use MySQL for your applications and manage databases using the MySQL command-line client.

Please note that this tutorial provides a basic setup for MySQL on Ubuntu 18.04. Depending on your specific requirements, you may need to configure additional settings or secure your MySQL installation further.

Leave a comment