DEV Community

Omar faruk zihad
Omar faruk zihad

Posted on

How to Install Mysql on Ubuntu 22.04 LTS

To install MySQL, first log in to the server using terminal. I will log in using my SSH

ssh root@{ip}
Enter fullscreen mode Exit fullscreen mode

Then run following commands

sudo apt update
sudo apt install mysql-server
Enter fullscreen mode Exit fullscreen mode

Then start the service using

sudo systemctl start mysql.service
Enter fullscreen mode Exit fullscreen mode

Now, we need to install the security services

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

This will show the below output

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
 0
Enter fullscreen mode Exit fullscreen mode

For testing purposes, I chose 0. That is the bare minimum password for the users and the root password won't be set. Then some prompt will open, press Y or N depending on your choice.
After all the prompt is done,enter the MySQL console

sudo mysql
CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';
Enter fullscreen mode Exit fullscreen mode

Be sure to change the sammy to your user and password to your password.

Then set the privileges

GRANT CREATE, ALTER, DROP, INSERT, UPDATE, INDEX, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'@'localhost' WITH GRANT OPTION;
Enter fullscreen mode Exit fullscreen mode

You are all set. You can clear the cache using

FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Exit the MySQL console and check the status by running

systemctl status mysql.service
Enter fullscreen mode Exit fullscreen mode
Output:
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: active (running) since Thu 2023-11-16 02:44:55 UTC; 1h 8min ago
    Process: 10804 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=>
   Main PID: 10812 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 1116)
     Memory: 361.8M
        CPU: 30.669s
     CGroup: /system.slice/mysql.service
             └─10812 /usr/sbin/mysqld
lines 1-11/11 (END)
Enter fullscreen mode Exit fullscreen mode

If the above output shows, congratulations, Mysql is not installed.

Top comments (0)