completely erase and reinstall mysql script.

 Warning: this will delete ALL your databases.

After running this script it will let you automatically run mysql without a password (as root). Note this  is a security risk so do not run this on a production server.

#!/bin/bash

set -e

# Function to remove MySQL and clean up
remove_mysql() {
    echo "Removing MySQL server and packages..."
    sudo apt-get remove -y mysql-server mysql-client mysql-common

    echo "Removing MySQL directories..."
    sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql /var/run/mysqld

    # Clean up any residual packages and update package info
    sudo apt-get autoremove -y
    sudo apt-get clean
}

# Function to ensure MySQL configuration file exists
ensure_mysql_config() {
    if [ ! -f /etc/mysql/mysql.cnf ]; then
        echo "Creating MySQL configuration file..."
        echo "[client]" | sudo tee /etc/mysql/mysql.cnf
        echo "user=root" | sudo tee -a /etc/mysql/mysql.cnf
        echo "password=abcd1234*" | sudo tee -a /etc/mysql/mysql.cnf
        echo "MySQL configuration file created at /etc/mysql/mysql.cnf."
    fi
}

# Function to create .my.cnf files in user directories
create_user_mysql_config() {
    echo "Configuring MySQL client for all users..."
    for home in /home/*; do
        if [ -d "$home" ]; then
            user=$(basename "$home")
            echo "[client]" > "$home/.my.cnf"
            echo "user=root" >> "$home/.my.cnf"
            echo "password=abcd1234*" >> "$home/.my.cnf"
            chown "$user:$user" "$home/.my.cnf"
            chmod 600 "$home/.my.cnf"
            su -c "mysql_config_editor set --host=localhost --user=root --password" -s /bin/bash "$user"
        fi
    done
}

is_running() {
echo "Checking MySQL service status..."
    if ! sudo systemctl is-active --quiet mysql; then
        sudo systemctl stop mysql
    fi
}

# Function to reinstall and configure MySQL
reinstall_mysql() {
    echo "Reinstalling MySQL server and client..."
    sudo apt-get update
    sudo apt-get install -y mysql-server mysql-client mysql-common

    # Ensure necessary MySQL configuration directories and files
    echo "Ensuring MySQL configuration files and directories..."
    sudo mkdir -p /etc/mysql /var/lib/mysql /var/log/mysql /var/run/mysqld
    sudo chown -R mysql:mysql /var/lib/mysql /var/run/mysqld
    sudo chmod 755 /var/run/mysqld

    echo "Ensure MySQL configuration file"
    ensure_mysql_config

    echo "Fixing broken packages and reconfiguring..."
    sudo dpkg --configure -a
    sudo apt-get install -f
}

# Function to reset MySQL root password
reset_mysql_password() {
    echo "Resetting MySQL root password..."

    # Ensure the MySQL directory for socket exists
    sudo mkdir -p /var/run/mysqld
    sudo chown mysql:mysql /var/run/mysqld
    sudo chmod 755 /var/run/mysqld

    # Start MySQL with --skip-grant-tables in the background
    sudo echo "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'abcd1234*'; FLUSH PRIVILEGES;" > /var/lib/mysql/reset.txt

sudo chown mysql:mysql /var/run/mysqld
sudo chmod 777 /var/run/mysqld # you can make this 755 some other time
sudo mysqld --init-file=/var/lib/mysql/reset.txt  --user=mysql

    # Start MySQL normally
    sudo systemctl restart mysql
}

# Main script logic
echo "Do you want to remove MySQL packages and directories? (y/n): "
read answer

if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
    remove_mysql
fi

is_running
create_user_mysql_config
ensure_mysql_config
reinstall_mysql
reset_mysql_password

echo "MySQL has been reinstalled (if needed), root password has been reset, and client configuration is updated for all users."

Comments

Popular posts from this blog

what to do if you crashed your database OR it says there are no tables (but there are!)

Backup a database