MySQL is a popular open-source relational database management system. However, sometimes you may need to remove MySQL from your Linux system due to various reasons such as upgrading to a newer version, replacing it with another database system, or simply uninstalling it. In this article, we’ll show you how to remove MySQL completely from your Linux system in a safe and effective manner.

Advertisement

Before we begin, it is recommended to back up your data and configuration files before uninstalling MySQL. You can use the mysqldump utility to back up your data or copy the entire MySQL data directory to a safe location.

Here’s the step-by-step guide to removing MySQL from your Linux system:

Step 1: Stop MySQL Service

First of all, you need to stop MySQL service on your system.

sudo systemctl stop mysql 

This stops the MySQL service, allowing you to safely remove the database.

Step 2: 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.

  • On Debain-based systems
    sudo apt remove mysql-server mysql-client 
    sudo apt autoremove 
    sudo apt autoclean 
    
  • On RHEL-based systems
    sudo dnf remove mysql-server mysql-client 
    

This removes the MySQL packages and their dependencies from your system. The autoremove and autoclean commands remove unnecessary packages and clean up the package cache.

Step 3: Remove MySQL configuration and data files

Now we need to remove the MySQL data directory from the 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 rm -rf /etc/mysql 
sudo rm -rf /var/lib/mysql 

This removes the MySQL configuration files and data directory. Be careful while removing these files, as they contain important data. Make sure to back up your data before removing these files.

Step 4: Verify the Removal

This following command lists all the packages installed on your system, and grep for the word ‘mysql’. If there are no MySQL packages installed, you have successfully removed MySQL from your system.

dpkg -l | grep -i mysql 

That’s it! You have now successfully removed MySQL from your Linux system. You can now install a different database system or simply enjoy a cleaner system without MySQL.

Conclusion

In conclusion, removing MySQL from your Linux system is a straightforward process, but it’s important to back up your data and follow the steps carefully to avoid any data loss. I hope this ultimate guide has been helpful in removing MySQL from your Linux system.

Share.

12 Comments

  1. Hi Omkar I followed the same step but now I am getting below error can you please suggest ?

    [root@devdesk]# 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

  2. 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

  3. 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.

  4. 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.

Exit mobile version