How to install MySQL in Ubuntu 19.10 a guide for beginners

How to install MySQL in Ubuntu 19.10 a 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, Secure MySQL, Check MySQL service, Connect and use MySQL server, Uninstall MySQL in Ubuntu and more.

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

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.

vijay@Ubuntu-19:~$sudo apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu disco InRelease                   
Get:2 http://security.ubuntu.com/ubuntu disco-security InRelease [97.5 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu disco-updates InRelease [97.5 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu disco-backports InRelease [88.8 kB]
Get:5 http://security.ubuntu.com/ubuntu disco-security/main i386 Packages [94.1 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu disco-updates/main i386 Packages [150 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu disco-updates/main amd64 Packages [171 kB]
Get:8 http://security.ubuntu.com/ubuntu disco-security/main amd64 Packages [113 kB]
Get:9 http://security.ubuntu.com/ubuntu disco-security/main amd64 DEP-11 Metadata [20.6 kB]
Get:10 http://security.ubuntu.com/ubuntu disco-security/main DEP-11 48x48 Icons [1,931 B]
Get:11 http://security.ubuntu.com/ubuntu disco-security/main DEP-11 64x64 Icons [4,931 B]
Get:12 http://security.ubuntu.com/ubuntu disco-security/universe i386 Packages [189 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu disco-updates/main amd64 DEP-11 Metadata [120 kB]
Get:14 http://in.archive.ubuntu.com/ubuntu disco-updates/main DEP-11 48x48 Icons [13.7 kB]
Get:15 http://in.archive.ubuntu.com/ubuntu disco-updates/main DEP-11 64x64 Icons [24.7 kB]
Get:16 http://security.ubuntu.com/ubuntu disco-security/universe amd64 Packages [189 kB]
Get:17 http://in.archive.ubuntu.com/ubuntu disco-updates/universe amd64 Packages [215 kB]
Get:18 http://in.archive.ubuntu.com/ubuntu disco-updates/universe i386 Packages [215 kB]
Get:19 http://security.ubuntu.com/ubuntu disco-security/universe amd64 DEP-11 Metadata [5,092 B]
Get:20 http://security.ubuntu.com/ubuntu disco-security/universe DEP-11 64x64 Icons [10.9 kB]
Get:21 http://in.archive.ubuntu.com/ubuntu disco-updates/universe amd64 DEP-11 Metadata [46.0 kB]
Get:22 http://in.archive.ubuntu.com/ubuntu disco-updates/universe DEP-11 48x48 Icons [21.7 kB]
Get:23 http://in.archive.ubuntu.com/ubuntu disco-updates/universe DEP-11 64x64 Icons [37.9 kB]
Get:24 http://in.archive.ubuntu.com/ubuntu disco-backports/universe amd64 DEP-11 Metadata [7,232 B]
Fetched 1,934 kB in 7s (278 kB/s)                                              
Reading package lists... Done
vijay@Ubuntu-19:~$

Use the following command to start installation.

vijay@Ubuntu-19:~$sudo apt-get install mysql-server
[sudo] password for vijay: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libaio1 libevent-core-2.1-6 libhtml-template-perl libmecab2 mysql-client-5.7 mysql-client-core-5.7 mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
  libaio1 libevent-core-2.1-6 libhtml-template-perl libmecab2 mysql-client-5.7 mysql-client-core-5.7 mysql-server mysql-server-5.7
  mysql-server-core-5.7
0 upgraded, 9 newly installed, 0 to remove and 109 not upgraded.
Need to get 20.9 MB of archives.
After this operation, 162 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.

vijay@Ubuntu-19:~$mysql --version
mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper
vijay@Ubuntu-19:~$

I found installed MySQL version is 5.7.26. Then I visited official website Here and I found the latest version of MySQL is 8.0.

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

vijay@Ubuntu-19:~$wget https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
--2019-06-21 08:42:59--  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]
--2019-06-21 08:43:03--  https://repo.mysql.com//mysql-apt-config_0.8.13-1_all.deb
Resolving repo.mysql.com (repo.mysql.com)... 23.202.169.138
Connecting to repo.mysql.com (repo.mysql.com)|23.202.169.138|: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  94.0KB/s    in 0.4s    

2019-06-21 08:43:06 (94.0 KB/s) - ‘mysql-apt-config_0.8.13-1_all.deb’ saved [35560/35560]
vijay@Ubuntu-19:~$

Next step to configure repository by running following command

$sudo dpkg -i mysql-apt-config*

vijay@Ubuntu-19:~$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
vijay@Ubuntu-19:~$
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
vijay@Ubuntu-19:~$mysql --version
mysql  Ver 8.0.16 for Linux on x86_64 (MySQL Community Server - GPL)
vijay@Ubuntu-19:~$

Check MySQL installation & Service

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

The following command will help:

vijay@Ubuntu-19:~$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.
vijay@Ubuntu-19:~$

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.

vijay@Ubuntu-19:~$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.

vijay@Ubuntu-19:~$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
vijay@Ubuntu-19:~$

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:

vijay@Ubuntu-19:~$sudo systemctl stop mysql.service 
[sudo] password for vijay: 
vijay@Ubuntu-19:~$
vijay@Ubuntu-19:~$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.
vijay@Ubuntu-19:~$

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:

vijay@Ubuntu-19:~$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
vijay@Ubuntu-19:~$

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

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

vijay@Ubuntu-19:~$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.

vijay@Ubuntu-19:~$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: