Created
October 27, 2024 07:12
-
-
Save cuongitl/a53744c69e311d6db2ab1c59e617c11c to your computer and use it in GitHub Desktop.
Shell Script to install MariaDB on Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # exit on error | |
| set -o errexit | |
| params_file='params.ini' | |
| #Including .ini file | |
| . $params_file | |
| if [ ! -f $params_file ]; then | |
| echo "$params_file does not exist. Exit setup." | |
| exit | |
| fi | |
| root_password=${DB_ROOT_PASSWORD} | |
| db_port=${DB_PORT} | |
| echo "root: $root_password, port: $db_port" | |
| cd /tmp | |
| # Check Mariad is installed or not? | |
| type mariadb >/dev/null 2>&1 && echo "MySQL already installed." && exit || echo "MySQL not present. Let's install..." | |
| dnf group install "Development Tools" -y | |
| yum install wget vim -y | |
| # Add mariadb_repo to OS (if not) | |
| if [ ! -f "/etc/yum.repos.d/mariadb.repo" ]; then | |
| echo "/etc/yum.repos.d/mariadb.repo does not exist. Let's create it." | |
| wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | |
| chmod +x mariadb_repo_setup | |
| ./mariadb_repo_setup | |
| fi | |
| dnf install -y curl gcc openssl-devel bzip2-devel libffi-devel zlib-devel wget make | |
| yum install MariaDB-shared MariaDB-devel -y | |
| yum -y install MariaDB-server MariaDB-client | |
| sudo systemctl enable mariadb | |
| sudo systemctl start mariadb | |
| # Make sure that NOBODY can access the server without a password | |
| #sudo mariadb -e "UPDATE mysql.user SET Password = PASSWORD('$root_password') WHERE User = 'root'" | |
| #==> error:ERROR 1356 (HY000) at line 1: View 'mysql.user' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them | |
| #sudo mariadb -e "SET PASSWORD FOR 'root'@'%' = PASSWORD('$root_password')" # error: ERROR 1133 (28000) at line 1: Can't find any matching row in the user table | |
| #sudo mariadb -e "UPDATE user SET Password=PASSWORD('$root_password') WHERE User='root';FLUSH PRIVILEGES;" # No database selected | |
| #sudo mariadb -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$root_password'" # Can't find any matching row in the user table | |
| sudo mariadb -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$root_password';FLUSH PRIVILEGES;" | |
| # Kill off the demo database | |
| sudo mariadb -e "DROP DATABASE IF EXISTS test" | |
| # Kill the anonymous users | |
| sudo mariadb -e "DROP USER IF EXISTS ''@'localhost'" | |
| # Because our hostname varies we'll use some Bash magic here. | |
| #sudo mariadb -e "DROP USER IF EXISTS ''@'$(hostname)'" | |
| # CHANGE PORT - IF NEED | |
| # Check if the script is run as root | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "Please run this script as root or with sudo." | |
| exit 1 | |
| fi | |
| # Define the new MariaDB port | |
| new_port=$db_port # Change this to your desired port number | |
| # Check if the port is in use using netstat | |
| if netstat -tuln | grep -q ":$new_port "; then | |
| echo "Port $port_to_check is in use." | |
| # exit | |
| else | |
| echo "Port $port_to_check is not in use." | |
| echo "Let's change db-port to: $db_port" | |
| # Backup the MariaDB configuration file | |
| cp /etc/my.cnf /etc/my.cnf.bak | |
| # Check if the port setting already exists in the configuration file | |
| if grep -q "^port" /etc/my.cnf; then | |
| # If the port setting exists, update it | |
| sed -i "s/^port = .*/port = $new_port/" /etc/my.cnf | |
| else | |
| # If the port setting does not exist, add it | |
| echo -e "\n[mysqld]\nport = $new_port" >> /etc/my.cnf | |
| fi | |
| # Restart MariaDB | |
| systemctl restart mariadb | |
| fi | |
| # Update firewall rules if firewalld is installed | |
| if command -v firewall-cmd &> /dev/null; then | |
| firewall-cmd --zone=public --add-port=$new_port/tcp --permanent | |
| firewall-cmd --reload | |
| fi | |
| echo "MariaDB port is $new_port. Don't forget to update application configurations." | |
| # Open port | |
| #echo "Open port: $db_port" | |
| #firewall-cmd --permanent --zone=public --add-port=$db_port/tcp | |
| #firewall-cmd --reload | |
| echo "Complete install MariaDB-server." | |
| exit | |
| # Source: https://infra.lecuong.info/2024/09/shell-script-to-install-mariadb-on-linux.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment