How To Reinstall MySQL Server v5.x On Linux

Reinstall-mysql-server
mm
Written By:- Santosh Prasad

Question: I would like to delete my old MySQL server because its database has been corrupted due to which I want to reinstall MySQL database server. How can I reinstall fresh full MySQL server again?

Answer: If your database has been corrupted and want to reinstall mysql server, you can easily do this, just follow the the below steps.

Step #1: Backup Database And Configuration Files

First of all take database backup and its related files before reinstalling MySQL server.

You will need to backup:

  • MySQL database data directory such as ” /var/lib/mysql/ ” it is default data directory path. For backup you can use mysqldump command.
  • MySQL configuration files “/etc/my.cnf, /etc/logrotate.d/mysqld and other related files.
  • MySQL log Files like “/var/log/mysqld.log“.

Backup all MySQL related files on RHEL/CentOS/Fedora based systems.

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

Backup MySQL Databases

For MySQL database backup you can use below script “database_bkp.sh“.

#!/bin/sh
# database_bkp.sh: Dump MySQL databases.
# Note: Tested only on RHEL/CentOS/Debian/Ubuntu Linux.
# Author: looklinux 
# -----------------------------------------------------
# (1) set up all the mysqldump variables
BAK=/root/mysql-bkp/
FILE=XXX_db.sql.`date +"%Y%m%d"`
DBSERVER=127.0.0.1
DATABASE=XXX
USER=XXX
PASS=XXX

# (2) in case you run this more than once a day, remove the previous version of the file
unalias rm     2> /dev/null
rm $BAK/${FILE}     2> /dev/null
rm $BAK/${FILE}.gz  2> /dev/null

# (3) do the mysql database backup (dump)

# use this command for a database server on a separate host:
#mysqldump --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} > $BAK/${FILE}

# use this command for a database server on localhost. add other options if need be.
mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > $BAK/${FILE}

# (4) gzip the mysql database dump file
gzip $BAK/$FILE

# (5) show the user the result
echo "${FILE}.gz was created:"
ls -l $BAK/${FILE}.gz

Now set the execute permission on script file.

# chmod +x database_bkp.sh

Now execute below command to take backup.

# ./database_bkp.sh

You will get some output like below.

XXX_db.sql.08-03-2017-04:00:18.gz was created:
-rw-r--r--. 1 root root  1836687 Mar  8 04:00 XXX_db.sql.08-03-2017-04:00:18.gz

Step #2 : Remove/Erase MySQL Server

After creating a database and its configuration backup, delete MySQL server using below command.

On Debian/Ubuntu systems

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

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

Now delete database and its configuration files like below.

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

On RHEL/CentOS/Fedora system

# yum remove mysql mysql-server

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!

Now delete database and its configuration files like below.

# rm -rvf /etc/my.cnf /var/lib/mysql/ /var/log/mysqld.log

Step #3: Reinstall MySQL Server

Now install MySQL fresh server using below command.

On RHEL/CentOS/Fedora Systems

# yum install mysql mysql-server

On Debian/Ubuntu Systems

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

Restart MySQL server

Restart MySQL Server if not started.

# service mysqld start

Step #4: Restore Databases And Configuration Files

Follow the below steps to restore all databases and its related files.

Restore all databases

# gunzip XXX_db.sql.08-03-2017-04:00:18.gz
# mysql -u root -p password -e 'CREATE DATABASE XXX;'
# mysql -u root -p XXX < XXX_db.sql.08-03-2017-04\:00\:18

Restore all MySQL related configuration files

# mkdir /root/backups
# tar xvf /root/mysql-bkp/mysql_all_config_bkp.dd-mm-yyyy.tar.gz -C /root/backups/
# cp /root/backups/etc/my.cnf /etc

Restart MySQL server

# service mysqld restart

I hope this article will help to reinstall MySQL server on Linux and Unix based systems. If you have any queries and problem please comment in comment section.

Thanks:)

About Author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Other Post by Santosh Prasad

Visit All Post

Related Article

You may also Like

Leave a Comment

Shares