How Install MariaDB Instead of MySQL?
DB_ROOT_PASSWORD='your-password'
DB_PORT='3307'
Thật ra không cần dùng file 'param.ini' cũng được, nhưng tôi có thói quen đọc/sửa mọi thứ từ file cấu hình cho tiện, không muốn sau này phải sửa trực tiếp từ script.
Tiếp theo là script cài đặt các gói cần thiết và Mariadb-Server
Dán đoạn code sau vào:
Phân quyền chạy cho script:
Tiến hành chạy script để cài đặt Mariadb-Server tự động:
Hết!
vim mariadb_install.sh
Dán đoạn code sau vào:
#!/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
chmod a+x mariadb_install.sh
sh mariadb_install.sh
gist: mariadb_install.sh
Hết!