Skip to main content

How to Install MariaDB / MySQL on Debian 12 - with bash script for apt repo

 Here's a bash script to automate the installation of MariaDB Server on Debian. The script handles both installation from the default Debian repository and from the official MariaDB repository if a newer version is required.

Install MariaDB on Linux

#!/bin/bash

# Bash script to install MariaDB Server on Debian

# Function to install MariaDB from the default Debian repository
install_from_debian_repo() {
    echo "Updating package list..."
    sudo apt update -y

    echo "Installing MariaDB Server and Client from Debian repository..."
    sudo apt install -y mariadb-server mariadb-client

    echo "Starting and enabling MariaDB service..."
    sudo systemctl start mariadb
    sudo systemctl enable mariadb

    echo "Securing MariaDB installation..."
    sudo mysql_secure_installation

    echo "MariaDB installed from Debian repository successfully!"
}

# Function to install MariaDB from the official MariaDB repository
install_from_mariadb_repo() {
    echo "Downloading MariaDB repository setup script..."
    wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup -O mariadb_repo_setup

    echo "Running repository setup script..."
    sudo bash mariadb_repo_setup

    echo "Updating package list..."
    sudo apt update -y

    echo "Installing MariaDB Server and Client from MariaDB repository..."
    sudo apt install -y mariadb-server mariadb-client

    echo "Starting and enabling MariaDB service..."
    sudo systemctl start mariadb
    sudo systemctl enable mariadb

    echo "Securing MariaDB installation..."
    sudo mysql_secure_installation

    echo "MariaDB installed from official MariaDB repository successfully!"
}

# Main script
echo "Select installation method:"
echo "1) Install MariaDB from Debian repository (default)"
echo "2) Install the latest MariaDB from the official MariaDB repository"
read -p "Enter your choice [1-2]: " choice

case $choice in
    1)
        install_from_debian_repo
        ;;
    2)
        install_from_mariadb_repo
        ;;
    *)
        echo "Invalid choice. Exiting."
        exit 1
        ;;
esac

How to Use the Script

  1. Save the script to a file, e.g., install_mariadb.sh.
  2. Make the script executable: chmod +x install_mariadb.sh
  3. Run the script: sudo ./install_mariadb.sh
  4. Follow the on-screen prompts to select the installation method (Debian repo or official MariaDB repo).

What the Script Does

  1. Option 1: Installs MariaDB using Debian's default package repository.
  2. Option 2: Sets up the MariaDB repository to install the latest version.

This script automates the installation process and ensures the MariaDB service is properly configured.

Remove MariaDB from Linux

Here’s a bash script to completely remove MariaDB from a Debian system, including configuration files, databases, and dependencies:

#!/bin/bash

# Bash script to completely remove MariaDB from Debian

# Function to stop MariaDB service
stop_mariadb() {
    echo "Stopping MariaDB service..."
    sudo systemctl stop mariadb
    sudo systemctl disable mariadb
    echo "MariaDB service stopped and disabled."
}

# Function to remove MariaDB packages
remove_mariadb_packages() {
    echo "Removing MariaDB packages..."
    sudo apt purge -y mariadb-server mariadb-client mariadb-common mariadb-backup mariadb-plugin-* libmariadb3 libmariadb-dev
    echo "MariaDB packages removed."
}

# Function to clean up residual files
cleanup_files() {
    echo "Removing residual MariaDB files and directories..."
    sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql
    echo "Residual MariaDB files removed."
}

# Function to remove unused dependencies
cleanup_dependencies() {
    echo "Removing unused dependencies..."
    sudo apt autoremove -y
    sudo apt autoclean
    echo "Unused dependencies cleaned up."
}

# Main script execution
echo "This script will completely remove MariaDB from your system."
read -p "Are you sure you want to proceed? (yes/no): " confirmation

if [[ "$confirmation" == "yes" ]]; then
    stop_mariadb
    remove_mariadb_packages
    cleanup_files
    cleanup_dependencies
    echo "MariaDB has been completely removed from your system."
else
    echo "Operation cancelled. MariaDB has not been removed."
fi

How to Use the Script

  1. Save the script to a file, e.g., remove_mariadb.sh.
  2. Make the script executable: chmod +x remove_mariadb.sh
  3. Run the script: sudo ./remove_mariadb.sh
  4. Confirm the removal when prompted.

What the Script Does

  1. Stops and disables the MariaDB service.
  2. Purges all MariaDB-related packages (mariadb-server, mariadb-client, etc.).
  3. Deletes residual configuration files and data directories (e.g., /etc/mysql, /var/lib/mysql).
  4. Cleans up unused dependencies and cached files to free up disk space.

⚠️ Warning: This script will delete all MariaDB-related files, including databases. Make sure to back up your data if needed before proceeding.




Comments

Popular posts from this blog

How to Enable PHP OPcode OPcache - Drupal 8

How to Enable fix PHP OPcode, OPcache caching in Drupal 8

How to Install MySQL Server on Ubuntu Debian

Run this bash script with sudo on your debian, ubuntu, linux mint and other debian based distros. install_mysql.sh #!/bin/bash # Exit immediately if a command exits with a non-zero status set -e # Define MySQL APT Config package URL and file MYSQL_APT_CONFIG_URL="https://dev.mysql.com/get/mysql-apt-config_0.8.33-1_all.deb" MYSQL_APT_CONFIG_DEB="mysql-apt-config_0.8.33-1_all.deb" # Update the package index echo "Updating package index..." sudo apt update -y # Download MySQL APT Config package if [[ ! -f "$MYSQL_APT_CONFIG_DEB" ]]; then echo "Downloading MySQL APT Config package..." wget "$MYSQL_APT_CONFIG_URL" -O "$MYSQL_APT_CONFIG_DEB" fi # Install the MySQL APT Config package echo "Installing MySQL APT Config package..." sudo dpkg -i "$MYSQL_APT_CONFIG_DEB" # Fix missing GPG keys (if any) echo "Updating package index again and fixing missing keys..." sudo...