From f35408c20c99168559ef30c6cea374ef403ea771 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 18 Sep 2025 16:57:14 +0000 Subject: [PATCH] feat: Add MySQL setup scripts and documentation This commit introduces scripts for automated MySQL setup, remote execution, and batch deployment on Rocky Linux 9. It also includes a comprehensive markdown guide detailing manual and automated installation, configuration, security best practices, backup procedures, and troubleshooting. Co-authored-by: ravi --- batch-mysql-setup.sh | 40 +++ hosts.txt | 13 + mysql-backup.sh | 25 ++ mysql-rocky-linux-setup.md | 694 +++++++++++++++++++++++++++++++++++++ mysql-setup.sh | 131 +++++++ remote-mysql-setup.sh | 124 +++++++ 6 files changed, 1027 insertions(+) create mode 100755 batch-mysql-setup.sh create mode 100644 hosts.txt create mode 100755 mysql-backup.sh create mode 100644 mysql-rocky-linux-setup.md create mode 100755 mysql-setup.sh create mode 100755 remote-mysql-setup.sh diff --git a/batch-mysql-setup.sh b/batch-mysql-setup.sh new file mode 100755 index 0000000..7f1d43e --- /dev/null +++ b/batch-mysql-setup.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Batch MySQL Setup Script +# Usage: ./batch-mysql-setup.sh + +set -e + +if [ $# -lt 2 ]; then + echo "Usage: $0 " + echo "Example: $0 hosts.txt ~/.ssh/id_rsa" + exit 1 +fi + +HOSTS_FILE=$1 +SSH_KEY=$2 + +if [ ! -f "$HOSTS_FILE" ]; then + echo "Hosts file $HOSTS_FILE not found!" + exit 1 +fi + +echo "Starting batch MySQL setup..." +echo "Hosts file: $HOSTS_FILE" +echo "SSH key: $SSH_KEY" +echo "" + +# Read hosts and setup MySQL on each +while IFS= read -r host; do + if [ -n "$host" ] && [[ ! "$host" =~ ^# ]]; then + echo "Setting up MySQL on $host..." + + # Use the remote setup script + ./remote-mysql-setup.sh "$host" "$SSH_KEY" "myapp_db" "myapp_user" "SecurePassword123!" + + echo "Completed setup on $host" + echo "----------------------------------------" + fi +done < "$HOSTS_FILE" + +echo "Batch MySQL setup completed for all hosts!" \ No newline at end of file diff --git a/hosts.txt b/hosts.txt new file mode 100644 index 0000000..410aa14 --- /dev/null +++ b/hosts.txt @@ -0,0 +1,13 @@ +# List of Rocky Linux 9 hosts for MySQL setup +# Add your host IPs here, one per line +# Lines starting with # are comments and will be ignored + +192.168.1.100 +192.168.1.101 +192.168.1.102 +192.168.1.103 + +# Example hosts (uncomment and modify as needed): +# 10.0.0.10 +# 10.0.0.11 +# 10.0.0.12 \ No newline at end of file diff --git a/mysql-backup.sh b/mysql-backup.sh new file mode 100755 index 0000000..6959a16 --- /dev/null +++ b/mysql-backup.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# MySQL Backup Script +# Usage: ./mysql-backup.sh [database_name] + +DB_NAME=${1:-"myapp_db"} +BACKUP_DIR="/var/backups/mysql" +DATE=$(date +%Y%m%d_%H%M%S) +BACKUP_FILE="$BACKUP_DIR/${DB_NAME}_$DATE.sql" + +# Create backup directory +sudo mkdir -p "$BACKUP_DIR" + +# Create backup +mysqldump -u root -p"RootPassword123!" --single-transaction --routines --triggers "$DB_NAME" > "$BACKUP_FILE" + +# Compress backup +gzip "$BACKUP_FILE" + +echo "Backup created: ${BACKUP_FILE}.gz" + +# Remove backups older than 7 days +find "$BACKUP_DIR" -name "*.sql.gz" -mtime +7 -delete + +echo "Old backups cleaned up" \ No newline at end of file diff --git a/mysql-rocky-linux-setup.md b/mysql-rocky-linux-setup.md new file mode 100644 index 0000000..55fe248 --- /dev/null +++ b/mysql-rocky-linux-setup.md @@ -0,0 +1,694 @@ +# MySQL Setup on Rocky Linux 9 - Complete Guide + +This guide provides comprehensive instructions for setting up MySQL on Rocky Linux 9 machines, including both manual and automated approaches. + +## Prerequisites + +- Rocky Linux 9 machine(s) with root or sudo access +- Private key for SSH access +- Network connectivity to download packages +- Basic knowledge of Linux commands + +## Method 1: Manual Setup (Step-by-Step) + +### Step 1: Update System Packages + +```bash +# Update package cache and upgrade system +sudo dnf update -y + +# Install EPEL repository for additional packages +sudo dnf install epel-release -y +``` + +### Step 2: Install MySQL Server + +```bash +# Install MySQL 8.0 server +sudo dnf install mysql-server -y + +# Start and enable MySQL service +sudo systemctl start mysqld +sudo systemctl enable mysqld + +# Check service status +sudo systemctl status mysqld +``` + +### Step 3: Secure MySQL Installation + +```bash +# Run MySQL secure installation script +sudo mysql_secure_installation +``` + +**Interactive prompts during secure installation:** +- Set root password: `Y` (recommended) +- Remove anonymous users: `Y` +- Disallow root login remotely: `Y` (recommended for security) +- Remove test database: `Y` +- Reload privilege tables: `Y` + +### Step 4: Configure MySQL + +```bash +# Create MySQL configuration directory +sudo mkdir -p /etc/mysql/conf.d + +# Create custom configuration file +sudo tee /etc/mysql/conf.d/custom.cnf > /dev/null < myapp_db +``` + +## Method 2: Automated Setup Script + +### Create Setup Script + +Create a script file `mysql-setup.sh`: + +```bash +#!/bin/bash + +# MySQL Setup Script for Rocky Linux 9 +# Usage: ./mysql-setup.sh [database_name] [username] [password] + +set -e + +# Default values +DB_NAME=${1:-"myapp_db"} +DB_USER=${2:-"myapp_user"} +DB_PASS=${3:-"SecurePassword123!"} +MYSQL_ROOT_PASS=${4:-"RootPassword123!"} + +echo "Starting MySQL setup on Rocky Linux 9..." +echo "Database: $DB_NAME" +echo "User: $DB_USER" +echo "Password: [HIDDEN]" + +# Function to check if command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Update system +echo "Updating system packages..." +sudo dnf update -y +sudo dnf install epel-release -y + +# Install MySQL +echo "Installing MySQL server..." +sudo dnf install mysql-server -y + +# Start and enable MySQL +echo "Starting MySQL service..." +sudo systemctl start mysqld +sudo systemctl enable mysqld + +# Wait for MySQL to start +sleep 5 + +# Get temporary root password +TEMP_PASS=$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}' | tail -1) + +if [ -z "$TEMP_PASS" ]; then + echo "No temporary password found. MySQL might already be configured." + TEMP_PASS="" +fi + +# Configure MySQL +echo "Configuring MySQL..." + +# Create configuration file +sudo tee /etc/mysql/conf.d/custom.cnf > /dev/null < /tmp/mysql_setup.sql < [database_name] [username] [password] + +set -e + +if [ $# -lt 2 ]; then + echo "Usage: $0 [database_name] [username] [password]" + echo "Example: $0 192.168.1.100 ~/.ssh/id_rsa myapp_db myuser mypass" + exit 1 +fi + +HOST_IP=$1 +SSH_KEY=$2 +DB_NAME=${3:-"myapp_db"} +DB_USER=${4:-"myapp_user"} +DB_PASS=${5:-"SecurePassword123!"} + +echo "Setting up MySQL on remote host: $HOST_IP" +echo "Using SSH key: $SSH_KEY" + +# Create the setup script content +cat > /tmp/mysql-setup-remote.sh <<'EOF' +#!/bin/bash + +set -e + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +MYSQL_ROOT_PASS=$4 + +echo "Starting MySQL setup on Rocky Linux 9..." + +# Update system +sudo dnf update -y +sudo dnf install epel-release -y + +# Install MySQL +sudo dnf install mysql-server -y + +# Start and enable MySQL +sudo systemctl start mysqld +sudo systemctl enable mysqld + +# Wait for MySQL to start +sleep 5 + +# Get temporary root password +TEMP_PASS=$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}' | tail -1) + +# Create configuration file +sudo tee /etc/mysql/conf.d/custom.cnf > /dev/null <<'MYSQLCONF' +[mysqld] +bind-address = 0.0.0.0 +port = 3306 +max_connections = 200 +max_allowed_packet = 64M +innodb_buffer_pool_size = 256M +innodb_log_file_size = 64M +innodb_flush_log_at_trx_commit = 2 +log-error = /var/log/mysqld.log +slow_query_log = 1 +slow_query_log_file = /var/log/mysql-slow.log +long_query_time = 2 +character-set-server = utf8mb4 +collation-server = utf8mb4_unicode_ci + +[mysql] +default-character-set = utf8mb4 + +[client] +default-character-set = utf8mb4 +MYSQLCONF + +# Restart MySQL +sudo systemctl restart mysqld + +# Create SQL setup commands +cat > /tmp/mysql_setup.sql < + +set -e + +if [ $# -lt 2 ]; then + echo "Usage: $0 " + echo "Example: $0 hosts.txt ~/.ssh/id_rsa" + exit 1 +fi + +HOSTS_FILE=$1 +SSH_KEY=$2 + +if [ ! -f "$HOSTS_FILE" ]; then + echo "Hosts file $HOSTS_FILE not found!" + exit 1 +fi + +echo "Starting batch MySQL setup..." +echo "Hosts file: $HOSTS_FILE" +echo "SSH key: $SSH_KEY" +echo "" + +# Read hosts and setup MySQL on each +while IFS= read -r host; do + if [ -n "$host" ] && [[ ! "$host" =~ ^# ]]; then + echo "Setting up MySQL on $host..." + + # Use the remote setup script + ./remote-mysql-setup.sh "$host" "$SSH_KEY" "myapp_db" "myapp_user" "SecurePassword123!" + + echo "Completed setup on $host" + echo "----------------------------------------" + fi +done < "$HOSTS_FILE" + +echo "Batch MySQL setup completed for all hosts!" +``` + +### Execute Batch Setup + +```bash +# Make script executable +chmod +x batch-mysql-setup.sh + +# Run batch setup +./batch-mysql-setup.sh hosts.txt ~/.ssh/id_rsa +``` + +## Security Best Practices + +### 1. Firewall Configuration + +```bash +# Allow only specific IPs to access MySQL +sudo firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.0/24' port protocol='tcp' port='3306' accept" +sudo firewall-cmd --permanent --remove-port=3306/tcp +sudo firewall-cmd --reload +``` + +### 2. SSL Configuration + +```bash +# Generate SSL certificates +sudo mysql_ssl_rsa_setup --uid=mysql + +# Add SSL configuration to MySQL config +sudo tee -a /etc/mysql/conf.d/custom.cnf > /dev/null < /dev/null < "$BACKUP_FILE" + +# Compress backup +gzip "$BACKUP_FILE" + +echo "Backup created: ${BACKUP_FILE}.gz" + +# Remove backups older than 7 days +find "$BACKUP_DIR" -name "*.sql.gz" -mtime +7 -delete + +echo "Old backups cleaned up" +``` + +### 3. Performance Monitoring + +```sql +-- Check MySQL status +SHOW STATUS; + +-- Check slow queries +SHOW VARIABLES LIKE 'slow_query_log%'; + +-- Check connections +SHOW STATUS LIKE 'Connections'; +SHOW STATUS LIKE 'Max_used_connections'; + +-- Check InnoDB status +SHOW ENGINE INNODB STATUS; +``` + +## Troubleshooting + +### Common Issues and Solutions + +1. **MySQL won't start:** +```bash +# Check error logs +sudo journalctl -u mysqld +sudo tail -f /var/log/mysqld.log + +# Check configuration +sudo mysqld --help --verbose | head -20 +``` + +2. **Connection refused:** +```bash +# Check if MySQL is running +sudo systemctl status mysqld + +# Check port binding +sudo netstat -tlnp | grep 3306 + +# Check firewall +sudo firewall-cmd --list-ports +``` + +3. **Permission denied:** +```bash +# Check MySQL user permissions +mysql -u root -p -e "SELECT user, host FROM mysql.user;" + +# Reset root password +sudo systemctl stop mysqld +sudo mysqld_safe --skip-grant-tables & +mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';" +sudo systemctl restart mysqld +``` + +## Summary + +This guide provides multiple approaches for setting up MySQL on Rocky Linux 9: + +1. **Manual Setup**: Step-by-step commands for single machine setup +2. **Automated Script**: Single script for automated setup +3. **Remote Execution**: Setup from Mac to remote Rocky Linux machines +4. **Batch Setup**: Setup multiple machines simultaneously + +Choose the method that best fits your needs. The automated approaches are recommended for production environments to ensure consistency across multiple machines. + +Remember to: +- Change default passwords +- Configure firewall rules appropriately +- Enable SSL for production environments +- Set up regular backups +- Monitor MySQL performance and logs \ No newline at end of file diff --git a/mysql-setup.sh b/mysql-setup.sh new file mode 100755 index 0000000..473d9e7 --- /dev/null +++ b/mysql-setup.sh @@ -0,0 +1,131 @@ +#!/bin/bash + +# MySQL Setup Script for Rocky Linux 9 +# Usage: ./mysql-setup.sh [database_name] [username] [password] + +set -e + +# Default values +DB_NAME=${1:-"myapp_db"} +DB_USER=${2:-"myapp_user"} +DB_PASS=${3:-"SecurePassword123!"} +MYSQL_ROOT_PASS=${4:-"RootPassword123!"} + +echo "Starting MySQL setup on Rocky Linux 9..." +echo "Database: $DB_NAME" +echo "User: $DB_USER" +echo "Password: [HIDDEN]" + +# Function to check if command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Update system +echo "Updating system packages..." +sudo dnf update -y +sudo dnf install epel-release -y + +# Install MySQL +echo "Installing MySQL server..." +sudo dnf install mysql-server -y + +# Start and enable MySQL +echo "Starting MySQL service..." +sudo systemctl start mysqld +sudo systemctl enable mysqld + +# Wait for MySQL to start +sleep 5 + +# Get temporary root password +TEMP_PASS=$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}' | tail -1) + +if [ -z "$TEMP_PASS" ]; then + echo "No temporary password found. MySQL might already be configured." + TEMP_PASS="" +fi + +# Configure MySQL +echo "Configuring MySQL..." + +# Create configuration file +sudo tee /etc/mysql/conf.d/custom.cnf > /dev/null < /tmp/mysql_setup.sql < [database_name] [username] [password] + +set -e + +if [ $# -lt 2 ]; then + echo "Usage: $0 [database_name] [username] [password]" + echo "Example: $0 192.168.1.100 ~/.ssh/id_rsa myapp_db myuser mypass" + exit 1 +fi + +HOST_IP=$1 +SSH_KEY=$2 +DB_NAME=${3:-"myapp_db"} +DB_USER=${4:-"myapp_user"} +DB_PASS=${5:-"SecurePassword123!"} + +echo "Setting up MySQL on remote host: $HOST_IP" +echo "Using SSH key: $SSH_KEY" + +# Create the setup script content +cat > /tmp/mysql-setup-remote.sh <<'EOF' +#!/bin/bash + +set -e + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +MYSQL_ROOT_PASS=$4 + +echo "Starting MySQL setup on Rocky Linux 9..." + +# Update system +sudo dnf update -y +sudo dnf install epel-release -y + +# Install MySQL +sudo dnf install mysql-server -y + +# Start and enable MySQL +sudo systemctl start mysqld +sudo systemctl enable mysqld + +# Wait for MySQL to start +sleep 5 + +# Get temporary root password +TEMP_PASS=$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}' | tail -1) + +# Create configuration file +sudo tee /etc/mysql/conf.d/custom.cnf > /dev/null <<'MYSQLCONF' +[mysqld] +bind-address = 0.0.0.0 +port = 3306 +max_connections = 200 +max_allowed_packet = 64M +innodb_buffer_pool_size = 256M +innodb_log_file_size = 64M +innodb_flush_log_at_trx_commit = 2 +log-error = /var/log/mysqld.log +slow_query_log = 1 +slow_query_log_file = /var/log/mysql-slow.log +long_query_time = 2 +character-set-server = utf8mb4 +collation-server = utf8mb4_unicode_ci + +[mysql] +default-character-set = utf8mb4 + +[client] +default-character-set = utf8mb4 +MYSQLCONF + +# Restart MySQL +sudo systemctl restart mysqld + +# Create SQL setup commands +cat > /tmp/mysql_setup.sql <