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
- Save the script to a file, e.g.,
install_mariadb.sh
. - Make the script executable:
chmod +x install_mariadb.sh
- Run the script:
sudo ./install_mariadb.sh
- Follow the on-screen prompts to select the installation method (Debian repo or official MariaDB repo).
What the Script Does
- Option 1: Installs MariaDB using Debian's default package repository.
- 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
- Save the script to a file, e.g.,
remove_mariadb.sh
. - Make the script executable:
chmod +x remove_mariadb.sh
- Run the script:
sudo ./remove_mariadb.sh
- Confirm the removal when prompted.
What the Script Does
- Stops and disables the MariaDB service.
- Purges all MariaDB-related packages (
mariadb-server
,mariadb-client
, etc.). - Deletes residual configuration files and data directories (e.g.,
/etc/mysql
,/var/lib/mysql
). - 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
Post a Comment