Ubuntu 18.04 How to Install, Reinstall and Uninstall MySQL

In this post you can find information about Ubuntu 18.04 and MySQL how to install, reinstall and uninstall it completely. Below you can find all topics:

This article will install the default package for the Ubuntu which is still MySQL 5.7. If you want to install MySQL 8 (or any other version) than you can follow this instructions:

How to install MySQL 8 on Ubuntu 16.04

Install MySQL default package

Update your package version in order to install latest possible version. Then install MySQL server by installing the default package. Optionally you can install additional libraries for MySQL like workbench. The last command is needed if you want to configure your server security. This script will delete test data, password levels will be set up and root account password will be changed:

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

sudo apt-get install mysql-client
sudo apt-get install mysql-workbench

sudo mysql_secure_installation 

Testing MySQL

After the successful installation MySQL should start automatically. You can test it by:

systemctl status mysql.service 

You'll see output similar to the following:

Output:

mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
  Active: active (running) since Wed 2016-11-23 21:21:25 UTC; 30min ago
Main PID: 3754 (mysqld)

If you are able to see it then this means MySQL is up and running.

Connect to MySQL

Connection to MySQL with user root and password with(you will be prompted for the password) : mysql -u [username]-p

mysql -u root -p

After successful connection you can start running queries.

Install mysql 8 Ubuntu 18.04

You can install the latest version (currently 8) by manually adding MySQL's repository first.

  • First you need to update your system:
sudo apt-get update
sudo apt-get upgrade
  • Install MySQL 8 by adding APT Repository
  • Download MySQL APT Repository from: MySQL APT Repository (Click Download on the bottom right, then copy the link on the next page ( from No thanks, just start my download)). Sample code:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.9-1_all.deb
  • Install the APT by dpkg

At this step you need to select what to be installed and press OK.

sudo dpkg -i mysql-apt-config_0.8.9-1_all.deb
  • Install the MySQL Server 8
sudo apt-get update
sudo apt-get install mysql-server
  • During installation you will be asked for root password. Select a good one and continue installation

reinstall MySQL server

Full version

Reinstalling is a process which consists of two steps: removing the old MySQL installation and then installing a new one.
First to remove or uninstall MySQL Client Core 5.7 you need to run:

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get remove --purge mysql-client-core-5.7
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /var/lib/mysql

Now install or reinstall MySQL client and server(or the one that you need):

sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install mysql-workbench
sudo mysql_secure_installation 

Short version:

First remove MySQL:

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean

Then reinstall:

sudo apt-get update
sudo apt-get install mysql-server mysql-client mysql-common
sudo mysql_secure_installation 

Uninstall MySQL server

Completely uninstall MySQL from Ubuntu 18.04(tested also on Ubuntu 16 and Linux Mint):

sudo apt-get remove --purge mysql*
sudo apt-get autoremove
sudo apt-get autoclean

Completely uninstall MySQL Client Core 5.7:

sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.7 mysql-client-core-5.7
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /var/lib/mysql
sudo rm -rf /etc/mysql

Install MySQL Workbench

In order to install MySQL Workbench the best is first to update the system by:

sudo apt-get update
sudo apt-get upgrade

Then you need to add the repository:

sudo dpkg -i mysql-apt-config_0.3.1-1ubuntu14.04_all.deb
sudo apt-get install mysql-workbench-community
sudo rpm -Uvh mysql-community-release-el7-5.noarch.rpm

And finally install it with

wget http://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-workbench-community-6.0.9-1ubu1310-amd64.deb
sudo dpkg -i mysql-workbench-community-6.0.9-1ubu1310-amd64.deb

MySQL services

After Ubuntu 15.04

systemctl start mysql # Use it to start a MySQL. Does not persist after reboot
systemctl stop mysql # Use it to stop a MySQL. Does not persist after reboot
systemctl restart mysql # Use it to restart a MySQL
systemctl reload mysql # If the MySQL supports it, it will reload the config files related to it without interrupting any process that is using the MySQL.
systemctl status mysql # Shows the status of a MySQL. Tells whether a MySQL is currently running.
systemctl enable mysql # Turns the MySQL on, on the next reboot or on the next start event. It persists after reboot.
systemctl disable mysql # Turns the MySQL off on the next reboot or on the next stop event. It persists after reboot.
systemctl show mysql # Show all the information about the MySQL.

Before Ubuntu 15.04

sudo start mysql #START 
sudo stop mysql  #STOP
sudo restart mysql #RESTART

Duplicate Entire MySQL Database

Two times will be prompted for password.

mysqldump -u admin -p originaldb | mysql -u backup -pPassword duplicateddb; 
mysqldump -u root -p mydb | mysql -u root -p mydbCopy;

Related Article