Sometimes we face issues with MySQL installation on a Linux system. If we simply remove MySQL packages and re-install doesn’t fix the issue, in that case, old settings may still exist on the server which again affects the new install. In that case, first, uninstall MySQL completely from the system and erase all settings of the old install. To do the same follow the below settings.
Note: Please do not use the below steps if MySQL has any running databases.
Step 1 – Uninstall MySQL Packages
First, uninstall all the MySQL packages installed on your server. Use one of the following commands as per your Linux distribution. The package names may vary based on the Operating system and installation types.
sudo yum remove mysql mysql-server #CentOS and RedHat systems sudo apt remove mysql mysql-server #Ubuntu and Debian systems sudo dnf remove mysql mysql-server #Fedora 22+ systems
Step 2 – Romove MySQL Directory
Now we need to remove MySQL data directory from system which by default exists at /var/lib/mysql. If you didn’t find this, It may be changed to some other place, which you can find in my.cnf file with variable datadir. Delete the /var/lib/mysql directory from the system but we prefer to rename it to keep a backup of existing files.
sudo mv /var/lib/mysql /var/lib/mysql_old_backup sudo mv /etc/mysql /etc/mysql_old_backup
Step 3 – Reinstall MySQL (If Required)
After removing MySQL completely, install it again using package manager, It will recreate all the required directories on your system. Below are the commands to install MySQL from default package repositories.
sudo yum install mysql-server #CentOS and RedHat systems sudo apt install mysql-server #Ubuntu and Debian systems sudo dnf install mysql-server #Fedora 22+ systems
After completing the above steps, now you have a fresh MySQL install on your system with new settings.
12 Comments
Hi Omkar I followed the same step but now I am getting below error can you please suggest ?
[[email protected]]# yum install mysql mysql-server
Loaded plugins: downloadonly, product-id, refresh-packagekit, subscription-manager
Updating Red Hat repositories.
http://repo.mysql.com/yum/mysql-connectors-community/el/6/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 22 – “The requested URL returned error: 403”
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: mysql-connectors-community. Please verify its path and try again
There is NOT a simple 1 line command to force yum to overwrite or remove a complete install package set like mariadb. it’s an ugly process so far to get an install of a different version of mysql. It has multiple entries in the local yum repo to erase.
run
yum list all > yum_list_all.txt
search that for both maria and mysql.
remove ALL those entries found from the yum repo .. e.g.
yum erase mariadb-libs.x86_64
also find all references on disk to folders with maria or mysql
rm -fr
now, in theory, you have a clean starting point to begin adding mysql packages to yum specifically that you will download and install
I am looking for this, thanks for sharing with us.
When i give this command : yum remove mysql mysql-server
it shows : You need to be root to perform this command.
I am not able to connect with root
Log in as root then, for this type the command below:
su –
(Provide your password when prompted for)
In case you do not have any root credentials then try using sudo command. i.e
sudo yum remove mysql mysql-server
If this does not help then you really need to contact your system administrator.
Perfect! Exactly what I needed! But it is only for Fedora/Redhat.
nice share, and What you want to know how to resolve the issue??
i have a problem with the command, can u give any solution?
I have followed the above steps and the system throws the below error.
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
Any one can help me?
Although I hope you have fixed your issue already I thought I might post my answer regardless since I had the same issue after completing all the above steps.
Simply run “service mysqld start” before connecting to your database and the issue should be fixed.
Exactly what I needed! Thanks!
Extremely simple explanation that lead to successful uninstallation.
Thank you so much Rahul.