How to Install MySQL on Ubuntu 22.10

thelinuxterminal

Author - Sanjay

Introduction

MySQL is a free and open source RDBMS (Relational Database Management System ). Its used by many organization and many applications such as Wordpress . Its fast , secure , reliable and most important cost effective .

In this post we will follow step by step guide to install Mysql in Ubuntu 22.10 .

Table of Contents

Pre-Requisistes

A Ubuntu server or a Desktop with root privilages or you can get a free vps hosting from Vultr or DigitalOcean

Get Free Mysql Hosting and VPS Hosting

Steps for Installing MySQL on Ubuntu

We are going to install latest version of mysql , as you know apt is the repository in ubuntu .All the packages are instaled via apt repository .

Lets follow the steps of installing Ubuntu

  1. Update the Ubuntu Package manager
$linuxterminal sudo apt-get update 
  1. As a next step let's install the MySQL server with the below command
sudo apt install mysql-server

This will by default install the latest mysql server 8.0 on your system .

  1. Now that we have mysql instaled , next is we need to check the mysql service is running or not . You can verify this using below command
sudo systemctl status mysql

Check mysql Status

if mysql is installed successfully then the mysql service status will be shown as below .

Mysql Secure Installation

Now that we have installed My SQL and it is properly installed . As a next step we need to do a secure installation of MySQL.In order to perform a secure installation mysql comes up with a inbuilt shell script called as mysql_secure_installation . We can perform a variety of security related operations on our my school server using this script. The main purpose of the script to enable security on our mysql installation.

Here are the following operations that we can perform using this script.

  1. We can set passwords for user accounts
  2. We can remove root accounts that are accessible outside or within the local host.
  3. We can remove unknown user accounts
  4. We can remove or add databases we can

We can run the script by running

thelinuxterminal$ sudo mysql_secure_installation

If you check the output below , we get an option for VALIDATE PASSWORD COMPONENT

[sudo] password for linuxterminal: 

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: 

The main purpose of this component used to check the strength of the password that we enter while doing a secure installation. Various types of password policies that are present are medium , strong and low. You can skip this as well if you enter no in the output step above .

Have a look at the options we get if we press yes

Mysql Secure Yes Option

Login to Mysql

There are various options with which you can login to my SQL, one of the option is using a mysql client that is shift as a package with mysql installation.

Other Option is you can use mysqlworkbench or any kind of MySQL client to login to your my SQL server installed locally. We will login using built in mysql client .

thelinuxterminal$ sudo mysql
Output
linuxterminal@linuxterminal-QEMU-Virtual-Machine:~$ sudo mysql
[sudo] password for linuxterminal: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.31-0ubuntu2 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

Now , this login wont work if you try login using phpmyadmin or any external program running remotely. If you want to login using any external program you need to perform following step .

$sudo mysql

then perform following command

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
mysql> FLUSH PRIVILEGES;

Now what happens here is the default authentication mechanism for MySQL is auth_socket , now if you want to login as a root you need to change the authentication mechanism . so how do I change it ? We change it by setting the password mechanism as mysql_ native_password.

Conclusion

We have done mysql installation on our Ubuntu 22.10 . We have also seen how we can connect via mysql client and login to ubuntu 22.10. If you face and difficulty or any problem , let us know on comments.