Complete Guide to Remove MySQL from Ubuntu 20.04

remove mysql do ubuntu

Script for complete removal:

  • Complete automated script to remove MySQL
  • Additional verification and cleaning

Important aspects covered:

  • Safety notices about data backup
  • Commands to stop services and remove packages
  • Cleaning files, directories, and settings
  • Removing users and groups from the system
  • Complete removal verification
  • Solving common problems

Additional resources:

  • Ready-to-use script
  • Verification commands
  • Tips for clean reinstallation
  • Troubleshooting for problematic cases

Click here to make a complete backup of all MySQL databases.

Edit the new file to remove mysql from the Linux server.

nano remove_mysql.sh

copy and paste the content below:

#!/bin/bash
# Script: remove_mysql.sh

echo "=== Removendo MySQL do Ubuntu 20.04 ==="

# Parar serviços
echo "Parando serviços MySQL..."
sudo systemctl stop mysql 2>/dev/null
sudo systemctl disable mysql 2>/dev/null

# Matar processos MySQL restantes
echo "Finalizando processos MySQL..."
sudo pkill -f mysql

# Remover pacotes
echo "Removendo pacotes MySQL..."
sudo apt remove --purge -y mysql-server mysql-client mysql-common
sudo apt remove --purge -y mysql-server-core-* mysql-client-core-*
sudo apt remove --purge -y mysql-community-server mysql-community-client
sudo apt autoremove --purge -y

# Remover arquivos e diretórios
echo "Removendo arquivos e diretórios..."
sudo rm -rf /etc/mysql/
sudo rm -rf /var/lib/mysql/
sudo rm -rf /var/log/mysql/
sudo rm -rf /usr/lib/mysql/
sudo rm -rf /etc/apparmor.d/usr.sbin.mysqld
sudo rm -rf /etc/logrotate.d/mysql-server

# Remover usuário e grupo
echo "Removendo usuário e grupo mysql..."
sudo deluser mysql 2>/dev/null
sudo delgroup mysql 2>/dev/null

# Atualizar cache de pacotes
sudo apt update

echo "=== Remoção do MySQL concluída! ==="
```

Change the file permission and then run the following to remove mysql:

chmod +x remove_mysql.sh
./remove_mysql.sh

Verify that mysql has been completely removed:
Run the commands below individually, line by line:

# Verificar se o serviço ainda existe
systemctl status mysql

# Verificar pacotes instalados
dpkg -l | grep -i mysql

# Verificar diretórios
ls -la /etc/mysql/
ls -la /var/lib/mysql/

# Verificar processos
ps aux | grep mysql

# Verificar portas em uso
sudo netstat -tlnp | grep :3306
```

Troubleshooting common problems:

See the error and solution below.

### Erro: "Package not found"
# Atualizar cache de pacotes
sudo apt update

# Forçar remoção de pacotes problemáticos
sudo dpkg --remove --force-remove-reinstreq mysql-server
sudo dpkg --remove --force-remove-reinstreq mysql-common
```
### Processos que Não Finalizam
```bash
# Forçar finalização de processos MySQL
sudo killall -9 mysql mysqld mysqld_safe

# Verificar se há arquivos de lock
sudo rm -f /var/lib/mysql/*.pid
sudo rm -f /var/run/mysqld/*
```
### Arquivos Protegidos
```bash
# Remover proteção de arquivos (se necessário)
sudo chattr -i /etc/mysql/my.cnf 2>/dev/null
sudo chattr -i /var/lib/mysql/* 2>/dev/null

# Então remover normalmente
sudo rm -rf /etc/mysql/
sudo rm -rf /var/lib/mysql/

Clean Reinstallation (Optional):

# Atualizar sistema
sudo apt update && sudo apt upgrade

# Instalar MySQL
sudo apt install mysql-server

# Configurar MySQL
sudo mysql_secure_installation

Important Tips:

  1. Always back up important data before removal.
  2. Check dependencies that may be affected by removing MySQL
  3. Restart the system after removal to ensure all changes take effect.
  4. Document your current configuration before removing in case you need to reinstall.

Commands to check if mysql has been removed:

Create a file for verification:

nano verifica_final.sh

Paste the contents into the file:

echo "=== Verificação Final ==="
echo "Serviços MySQL:"
systemctl list-units --type=service | grep mysql

echo "Pacotes MySQL:"
dpkg -l | grep mysql

echo "Processos MySQL:"
ps aux | grep mysql

echo "Diretórios MySQL:"
find / -name "*mysql*" -type d 2>/dev/null

echo "Arquivos de configuração:"
find /etc -name "*mysql*" 2>/dev/null

echo "Portas em uso (3306):"
sudo ss -tlnp | grep :3306

Change file permission and run:

chmod +x verifica_final.sh
./verifica_final.sh

Este guia garante uma remoção completa e segura do MySQL de seu sistema Ubuntu 20.04.