how-to-install-mysql-in-Ubuntu-19.04

How to install MySQL in Ubuntu 20.04 a Step by Step guide for beginners

MySQL is well known and an open-source database management system. It uses a relational database and SQL to manage data. It is easy to use, fast, and integrated with popular stacks Lamp, Xampp, Lemp, etc.

In this tutorial, I will cover how to install MySQL in Ubuntu 20.04, Secure MySQL, Check MySQL service, Connect and use MySQL server, Uninstall MySQL in Ubuntu and more.

I have created a video tutorial in Hindi on how to install MySQL in the Hindi language. If you are good in Hindi then watch the following video

Before starting to install MySQL in Ubuntu you must have access of system with root user or sudo user.

Article Contents

Command Table How to install MySQL in Ubuntu

Command Description
sudo apt-get updateUpdate Repository
sudo apt-get install mysql-serverInstall MySQL server on Ubuntu
mysql –versionCheck MySQL version after installation
sudo systemctl status mysql.serviceCheck the Status of MySQL service
sudo systemctl start mysql.serviceStart MySQL service
sudo systemctl stop mysql.serviceStop MySQL service
sudo mysql_secure_installationStart mysql secure installation on Ubuntu
sudo mysql -h localhost -u root -pConnect to mysql on localhost

How to install MySQL in Ubuntu from its Repositories

I can’t assure you that you will find the latest version of MySQL on Ubuntu repositories. If you will install MySQL by using ubuntu repository, you will find only the version available on ubuntu repository.

Run the following command to update repository list with update packages.

[email protected]:~/Desktop$ sudo apt-get update
[sudo] password for vijay: 
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [106 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]     
Hit:4 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease 
Get:5 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [13.4 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [35.5 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [15.0 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [14.6 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [1,232 B]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [10.5 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [12.3 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [5,676 B]
Get:13 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [212 B]
Get:14 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [788 B]
Get:15 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages [12.7 kB]
Get:16 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [29.5 kB]
Get:17 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [12.2 kB]
Get:18 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [14.6 kB]
Get:19 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [928 B]
Get:20 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [3,576 B]
Get:21 http://security.ubuntu.com/ubuntu focal-security/universe i386 Packages [3,112 B]
Get:22 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [3,328 B]
Get:23 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [208 B]
Get:24 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [492 B]
Fetched 403 kB in 5s (76.9 kB/s)  
Reading package lists... Done
[email protected]:~/Desktop$ 

Use the following command to start installation.

[email protected]:~$sudo apt-get install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7 libfcgi-perl libhtml-template-perl libmecab2 mecab-ipadic mecab-ipadic-utf8
  mecab-utils mysql-client-8.0 mysql-client-core-8.0 mysql-server-8.0 mysql-server-core-8.0
Suggested packages:
  libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7 libfcgi-perl libhtml-template-perl libmecab2 mecab-ipadic mecab-ipadic-utf8
  mecab-utils mysql-client-8.0 mysql-client-core-8.0 mysql-server mysql-server-8.0 mysql-server-core-8.0
0 upgraded, 15 newly installed, 0 to remove and 32 not upgraded.
Need to get 29.8 MB of archives.
After this operation, 245 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

Press Y to continue process.

Install MySQL in Ubuntu from Official Repositories

In the previous, section I have installed MySQL from ubuntu repository, then I see the installed version by running following command.

[email protected]:~$mysql --version
mysql  Ver 8.0.19-0ubuntu5 for Linux on x86_64 ((Ubuntu))
[email protected]:~$

I found installed MySQL version is 8.0.19. Then I visited official website Here and I found the latest version of MySQL is 8.13-1.

Now I want to install newer version. You can do the same Go to Download page and click on download then copy the link from hyperlink text is “No thanks, just start my download

[email protected]:~$wget https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
--2020-05-01 18:24:10--  https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql-apt-config_0.8.13-1_all.deb [following]
--2020-05-01 18:24:14--  https://repo.mysql.com//mysql-apt-config_0.8.13-1_all.deb
Resolving repo.mysql.com (repo.mysql.com)... 23.62.138.106
Connecting to repo.mysql.com (repo.mysql.com)|23.62.138.106|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 35560 (35K) [application/x-debian-package]
Saving to: ‘mysql-apt-config_0.8.13-1_all.deb’

mysql-apt-config_0.8.13-1_all.deb   100%[=================================================================>]  34.73K  --.-KB/s    in 0.1s    

2020-05-01 18:24:15 (243 KB/s) - ‘mysql-apt-config_0.8.13-1_all.deb’ saved [35560/35560]
[email protected]:~$

Next step to configure repository by running following command

$sudo dpkg -i mysql-apt-config*

[email protected]:~$udo dpkg -i mysql-apt-config_0.8.13-1_all.deb 
(Reading database ... 172110 files and directories currently installed.)
Preparing to unpack mysql-apt-config_0.8.13-1_all.deb ...
Unpacking mysql-apt-config (0.8.13-1) over (0.8.13-1) ...
Setting up mysql-apt-config (0.8.13-1) ...
Warning: apt-key should not be used in scripts (called from postinst maintainerscript of the package mysql-apt-config)
OK
[email protected]:~$
15-configure-apt
14-select-mysql-versioin
16-select-appropriate-mysql-version

Again update repository and install mysql-server by running same command as above.

$sudo apt-get update
$sudo apt-get install mysql-server

One windows will be prompt to select password strong and encryption then press ok.

17-use-strong-password
[email protected]:~$mysql --version
mysql  Ver 8.0.16 for Linux on x86_64 (MySQL Community Server - GPL)
[email protected]:~$

Check MySQL installation & Service

Once the install process will be complete, You can check the status of mysql service.

The following command will help:

[email protected]:~$sudo systemctl status mysql.service
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-06-21 08:59:45 IST; 6min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 8127 (mysqld)
   Status: "SERVER_OPERATING"
    Tasks: 37 (limit: 2331)
   Memory: 544.1M
   CGroup: /system.slice/mysql.service
           └─8127 /usr/sbin/mysqld

Jun 21 08:59:20 Ubuntu-19 systemd[1]: Starting MySQL Community Server...
Jun 21 08:59:45 Ubuntu-19 systemd[1]: Started MySQL Community Server.
[email protected]:~$

Secure MySQL by one command

Securing DBMS is very important for Database Administrator, You can secure MySQL in one command just like a charm.

Command has been given below.

[email protected]:~$sudo mysql_secure_installation

When the secure installation script will run lots of option will display on the screen give the answer one by one wisely.

In the middle of this process, you can set the strong password for the mysql root user. The MySQL root user is different from the system root user.

  • Press y|Y for Yes, any other key for No: Y
  • Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
  • Please set the password for root here.
    New password:
    Re-enter new password:
  • Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
  • Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
  • Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
  • Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

Connect to MySQL Server

I am going to connect using root user and password on localhost machine.

[email protected]:~$sudo mysql -h localhost -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> exit
Bye
[email protected]:~$

Uninstall MySQL in Ubuntu

This is very important section for the new user, In this section I will cover how to uninstall MySQL in Ubuntu. As you have seen, I have installed the latest MySQL 8.0 in the previous section.

First you need to stop and disable mysql service, So run the following command:

[email protected]:~$sudo systemctl stop mysql.service 
[sudo] password for vijay: 
[email protected]:~$
[email protected]:~$sudo systemctl disable mysql.service 
Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable mysql
Removed /etc/systemd/system/multi-user.target.wants/mysql.service.
[email protected]:~$

If you have installed from ubuntu repository then run the following command to uninstall Ubuntu.

$sudo apt-get purge mysql*

If you have installed from official repository, then you must follow the given instruction carefully.

Write command $sudo apt-get purge mysql- and press TAB key two times from your keyboard. you will see all available option. See in the example:

[email protected]:~$sudo apt-get purge mysql-
mysql-apt-config             mysql-common                 mysql-community-client-core  mysql-community-server-core
mysql-client                 mysql-community-client       mysql-community-server       mysql-server
[email protected]:~$

Complete your command with write all available options. See in Example and Hit enter.

Command will ask your confirmation, Press “Y” and hit enter.

[email protected]:~$sudo apt-get purge mysql-server mysql-client mysql-common mysql-community-client-core mysql-apt-config mysql-community-server-core
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  acl apg colord-data gnome-control-center-faces gnome-online-accounts hplip-data libaio1 libcolord-gtk1 libcolorhug2 libevent-core-2.1-6
  libgsound0 libgtop-2.0-11 libgtop2-common libieee1284-3 libimagequant0 libmecab2 libsane-common libsnmp-base libwebpmux3 mecab-ipadic
  mecab-ipadic-utf8 mecab-utils mobile-broadband-provider-info mysql-community-server-core network-manager-gnome
  printer-driver-postscript-hp python3-macaroonbakery python3-olefile python3-pexpect python3-pil python3-protobuf python3-ptyprocess
  python3-renderpm python3-reportlab python3-reportlab-accel python3-rfc3339 python3-tz ubuntu-system-service
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  colord* gnome-control-center* hplip* libhpmud0* libmysqlclient20* libsane* libsane-hpaio* libsnmp30* mysql-client* mysql-common*
  mysql-community-client* mysql-community-client-core* mysql-community-server* mysql-server* printer-driver-hpcups* sane-utils* simple-scan*
  ubuntu-desktop* ubuntu-desktop-minimal*
0 upgraded, 0 newly installed, 19 to remove and 108 not upgraded.
After this operation, 275 MB disk space will be freed.
Do you want to continue? [Y/n] 
18-remove-data-directory

Next command $sudo apt-get autoremove is used to remove all unused dependencies from your system.

[email protected]:~$sudo apt-get autoremove 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  acl apg colord-data gnome-control-center-faces gnome-online-accounts hplip-data libaio1 libcolord-gtk1 libcolorhug2 libevent-core-2.1-6
  libgsound0 libgtop-2.0-11 libgtop2-common libieee1284-3 libimagequant0 libmecab2 libsane-common libsnmp-base libwebpmux3 mecab-ipadic
  mecab-ipadic-utf8 mecab-utils mobile-broadband-provider-info network-manager-gnome printer-driver-postscript-hp python3-macaroonbakery
  python3-olefile python3-pexpect python3-pil python3-protobuf python3-ptyprocess python3-renderpm python3-reportlab python3-reportlab-accel
  python3-rfc3339 python3-tz ubuntu-system-service
0 upgraded, 0 newly installed, 37 to remove and 107 not upgraded.
After this operation, 86.6 MB disk space will be freed.
Do you want to continue? [Y/n] 

Conclusion

Now, You can install the old version from Ubuntu 19.04 repository or you can install the latest version of MySQL from its official repository. You can be hardening MySQL, later you have learned how to uninstall MySQL from Ubuntu 19.04.

If you have any question related this article, write in comment box.

Thanks for Reading, Don’t forget to share.

If Appreciate My Work, You should consider: