HowTo: Reinstall MySQL v5.x On Linux

by on December 9, 2013 · 2 comments· LAST UPDATED December 9, 2013

in , ,

My 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?

Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
RequirementsNone
Estimated completion time20m
You can easily reinstall mysql server on Linux or Unix-like operating system. The steps are as follows:

  1. Backup database and all config files
  2. Erase/uninstall existing mysql server/client.
  3. Delete all files data directory.
  4. Delete all mysql config files.
  5. Completely reinstall mysql server.
  6. 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

  1. MySQL database data directory (e.g. /var/lib/mysql/.
  2. MySQL databases using mysqldump command.
  3. Mysql config files /etc/my.cnf, /etc/logrotate.d/mysqld, and other files.
  4. 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 mysql-backup.sh:

#!/bin/sh
# mysql-backup.sh: Dump MySQL databases.
# Note: Test only on RHEL/CentOS/Debian/Ubuntu Linux.
# Author: nixCraft <www.cyberciti.biz> Under GPL v2.0+
# -----------------------------------------------------
NOW=$(date +"%d-%m-%Y")
BAK="/root/mysql-files/$NOW"
 
##################
## SET ME FIRST ##
##################
MUSER="root"
MPASS="YOUR-ROOT-PASSWORD-HERE"
MHOST="localhost"
 
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
 
if [ ! -d $BAK ]; then
  mkdir -p $BAK
else
 :
fi
 
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
echo -n "Dumping...${THISDB}..."
for db in $DBS
do
 echo -n "$db "
 FILE=$BAK/mysql-$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
echo -n  "...Done @ ${BAK} directory."
echo ""

Run it as follows
$ ./mysql-backup.sh
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
  libplrpc-perl
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*
  mysql-server-5.5*
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
======================================================================================================
Removing:
 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
 
Removed:
  mysql.x86_64 0:5.1.71-1.el6                    mysql-server.x86_64 0:5.1.71-1.el6
 
Complete!
 

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 2 comments… read them below or add one }

1 bob December 10, 2013 at 8:26 am

Thank you mate, just a simple thank you!

Reply

2 n.arshad August 25, 2014 at 4:37 am

nice explanation!!!!!!!!!!! thank u very much……..

Reply

Leave a Comment

Tagged as: , ,

Previous Faq:

Next Faq: