HowTo: Reinstall MySQL v5.x On Linux


y MySQL database installation has been corrupted. I would like to delete old mysql server. How do I reinstall MySQL database server version 5.x on Linux or Unix-like oses? A software upgrade broke my system. How can I uninstall and re-install mysql again to get fresh full mysql again?


You can easily reinstall mysql server on Linux or Unix-like operating system. The steps are as follows:

Backup database and all config files

Erase/uninstall existing mysql server/client.

Delete all files data directory.

Delete all mysql config files.

Completely reinstall mysql server.

Restore config files and database.

Step #1: Backup backup backup

Make a backup – it cannot be stressed enough how important it is to make a backup of your system before you do this. You need to backup

MySQL database data directory (e.g. /var/lib/mysql/.

MySQL databases using mysqldump command.

Mysql config files /etc/my.cnf, /etc/logrotate.d/mysqld, and other files.

Mysql log files (e.g. /var/log/mysqld.log.

In this example, I am backing up all important files on CentOS/RHEL 6.x based server:

# mkdir /root/mysql-files/

# tar zcvf /root/mysql-files/mysql.config-files.dd-mm-yyyy.tar.gz /etc/logrotate.d/mysqld /var/log/mysqld.log /etc/my.cnf /root/my.cnf /var/lib/mysql/


In this example, I am backing up all databases using a shell script called


# Dump MySQL databases.

# Note: Test only on RHEL/CentOS/Debian/Ubuntu Linux.

# Author: nixCraft <> Under GPL v2.0+

# —————————————————–

NOW=$(date + %d-%m-%Y )

BAK= /root/mysql-files/$NOW





MUSER= root


MHOST= localhost


MYSQL= $(which mysql)

MYSQLDUMP= $(which mysqldump)

GZIP= $(which gzip)


if [ ! -d $BAK ]; then

mkdir -p $BAK





DBS= $($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse  show databases )

echo -n  Dumping…${THISDB}…

for db in $DBS


echo -n  $db

FILE=$BAK/mysql-$db.$NOW-$(date + %T ).gz



echo -n   …Done @ ${BAK} directory.


Run it as follows

$ ./


Sample outputs:

Dumping……blog cyberciti mysql…Done @ /root/mysql-files/08-12-2013 directory.

Verify backups:

# ls -l /root/mysql-files/08-12-2013


Sample outputs:

-rw-r–r–. 1 root root  1836687 Dec  8 04:00 mysql-blog.08-12-2013-04:00:18.gz

-rw-r–r–. 1 root root  7152648 Dec  8 04:00 mysql-cyberciti.08-12-2013-04:00:25.gz

-rw-r–r–. 1 root root   135530 Dec  8 04:00 mysql-mysql.08-12-2013-04:00:41.gz

I suggest that you read our previous guide “HowTo: Migrate / Move MySQL Database And Users To New Server” for more information.

Step #2: Erase/Delete MySQL server

If you are using Debian/Ubuntu Linux type the following apt-get command:

$ sudo apt-get purge mysql-server mysql-common mysql-client


Sample outputs:

Reading package lists… Done

Building dependency tree

Reading state information… Done

Package mysql-client is not installed, so not removed

The following packages were automatically installed and are no longer required:

libnet-daemon-perl libdbi-perl libterm-readkey-perl mysql-server-core-5.5 mysql-client-core-5.5


Use  apt-get autoremove  to remove them.

The following packages will be REMOVED:

libdbd-mysql-perl* libmysqlclient18* mysql-client-5.5* mysql-common* mysql-server*


0 upgraded, 0 newly installed, 6 to remove and 2 not upgraded.

After this operation, 67.3 MB disk space will be freed.

Do you want to continue [Y/n]? y

(Reading database … 82128 files and directories currently installed.)

Removing mysql-server …

Removing mysql-server-5.5 …

mysql stop/waiting

Purging configuration files for mysql-server-5.5 …

Removing mysql-client-5.5 …

Removing libdbd-mysql-perl …

Removing libmysqlclient18 …

Purging configuration files for libmysqlclient18 …

Removing mysql-common …

Purging configuration files for mysql-common …

dpkg: warning: while removing mysql-common, directory  /etc/mysql  not empty so not removed.

Processing triggers for man-db …

Processing triggers for ureadahead …

Processing triggers for libc-bin …

ldconfig deferred processing now taking place

Delete config/database/log files using rm command:

$ sudo rm -rvfi /var/lib/mysql /etc/mysql/ /var/log/mysql*

If you are using CentOS/RHEL/Fedora/Red Hat/Scientific Linux type the following yum command to uninstall mysql:

# yum remove mysql mysql-server


Sample outputs:

Loaded plugins: product-id, rhnplugin, security, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

This system is receiving updates from RHN Classic or RHN Satellite.

Setting up Remove Process

Resolving Dependencies

–> Running transaction check

—> Package mysql.x86_64 0:5.1.71-1.el6 will be erased

—> Package mysql-server.x86_64 0:5.1.71-1.el6 will be erased

–> Finished Dependency Resolution


Dependencies Resolved



Package                Arch             Version                Repository                       Size



mysql                  x86_64           5.1.71-1.el6           @rhel-x86_64-server-6           2.4 M

mysql-server           x86_64           5.1.71-1.el6           @rhel-x86_64-server-6            25 M


Transaction Summary


Remove        2 Package(s)


Installed size: 27 M

Is this ok [y/N]: y

Downloading Packages:

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Erasing    : mysql-server-5.1.71-1.el6.x86_64                                                   1/2

Erasing    : mysql-5.1.71-1.el6.x86_64                                                          2/2

Verifying  : mysql-server-5.1.71-1.el6.x86_64                                                   1/2

Verifying  : mysql-5.1.71-1.el6.x86_64                                                          2/2



mysql.x86_64 0:5.1.71-1.el6                    mysql-server.x86_64 0:5.1.71-1.el6



Delete config/database/log files using rm command:

[code]# rm -rfvi /etc/my.cnf /var/lib/mysql/ /var/log/mysqld.log[/code]

Step #3: Reinstall mysql database server

If you are using CentOS/RHEL/Fedora/Red Hat/Scientific Linux type the following yum command to install mysql:

# yum install mysql mysql-server

If you are using Debian/Ubuntu Linux type the following apt-get command:

$ sudo apt-get install mysql-client mysql-server mysql-common

Step #4: Restore config files and databases

Restore all config files and databases as demonstrated in step #1. See how to restore a backup of a mysql database:

$ gunzip mysql-blog.08-12-2013-04:00:18.gz

$ mysql -u root -p mysql -e  CREATE DATABASE blog;

$ mysql -u root -p blog < mysql-blog.08-12-2013-04\:00\:18


Restore mysql config file as follows:

# tar xvf /root/mysql-files/mysql.config-files.dd-mm-yyyy.tar.gz -C /root/backups/

# cp /root/backups/etc/my.cnf /etc

# service mysqld restart


This entry is 5 of 5 in the CentOS Linux MySQL Server Tutorial series. Keep reading the rest of the series:

CentOS install Mysql database server

CentOS install MySQL client only

MySQL Create a user accounts

MySQL Create a database & tables and data

Reinstall MySQL On Linux



Leave a Reply

Your email address will not be published. Required fields are marked *