Created
December 6, 2024 14:11
-
-
Save markjacksonfishing/1713e9d4ca5d940a82f1fa677aedfb09 to your computer and use it in GitHub Desktop.
Perc Setup
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
#!/bin/bash | |
set -e # Exit immediately if a command exits with a non-zero status | |
set -o pipefail # Fail if any piped command fails | |
# Variables (Adjust as needed) | |
DB_ROOT_PASSWORD="strong_root_password" | |
DB_NAME="my_project_db" | |
DB_USER="project_user" | |
DB_USER_PASSWORD="strong_user_password" | |
BACKUP_DIR="/var/backups/mysql" | |
PMM_SERVER_URL="https://<PMM_SERVER_URL>" | |
PMM_USER="root" | |
PMM_PASSWORD="strong_root_password" | |
# Colors for output | |
GREEN="\033[0;32m" | |
RED="\033[0;31m" | |
NC="\033[0m" # No color | |
echo -e "${GREEN}Starting Percona Database Server installation...${NC}" | |
# Step 1: Update and install prerequisites | |
echo -e "${GREEN}Updating system and installing dependencies...${NC}" | |
sudo apt update && sudo apt upgrade -y | |
sudo apt install wget gnupg -y | |
# Step 2: Add Percona repository and install server | |
echo -e "${GREEN}Adding Percona repository...${NC}" | |
wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb | |
sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb | |
sudo apt update | |
echo -e "${GREEN}Installing Percona Database Server...${NC}" | |
sudo apt install percona-server-server -y | |
# Step 3: Secure MySQL installation | |
echo -e "${GREEN}Securing MySQL installation...${NC}" | |
sudo mysql_secure_installation <<EOF | |
Y | |
$DB_ROOT_PASSWORD | |
$DB_ROOT_PASSWORD | |
Y | |
Y | |
Y | |
Y | |
EOF | |
# Step 4: Configure MySQL | |
echo -e "${GREEN}Configuring MySQL for performance...${NC}" | |
sudo tee /etc/mysql/my.cnf > /dev/null <<EOF | |
[mysqld] | |
innodb_buffer_pool_size = 1G | |
innodb_log_file_size = 256M | |
max_connections = 500 | |
query_cache_size = 0 | |
performance_schema = ON | |
slow_query_log = 1 | |
slow_query_log_file = /var/log/mysql-slow.log | |
long_query_time = 1 | |
log_bin = /var/lib/mysql/mysql-bin | |
server-id = 1 | |
EOF | |
sudo systemctl restart mysql | |
# Step 5: Create database and user | |
echo -e "${GREEN}Creating database and user...${NC}" | |
mysql -u root -p"$DB_ROOT_PASSWORD" <<EOF | |
CREATE DATABASE $DB_NAME; | |
CREATE USER '$DB_USER'@'%' IDENTIFIED BY '$DB_USER_PASSWORD'; | |
GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'%'; | |
FLUSH PRIVILEGES; | |
EOF | |
# Step 6: Install Percona Backup and PMM client | |
echo -e "${GREEN}Installing Percona Backup tool...${NC}" | |
sudo apt install percona-xtrabackup -y | |
echo -e "${GREEN}Installing PMM Client...${NC}" | |
wget https://downloads.percona.com/downloads/pmm2/2.36.0/binary/debian/pmm2-client_2.36.0-6.ubuntu20.04_amd64.deb | |
sudo dpkg -i pmm2-client_2.36.0-6.ubuntu20.04_amd64.deb | |
sudo pmm-admin config --server-insecure-tls --server-url=$PMM_SERVER_URL | |
sudo pmm-admin add mysql --user=$PMM_USER --password=$PMM_PASSWORD | |
# Step 7: Setup backups | |
echo -e "${GREEN}Setting up backup directory and cron job...${NC}" | |
sudo mkdir -p $BACKUP_DIR | |
sudo chmod 700 $BACKUP_DIR | |
(crontab -l 2>/dev/null; echo "0 2 * * * /usr/bin/xtrabackup --backup --target-dir=$BACKUP_DIR/$(date +\%F)") | crontab - | |
# Step 8: Firewall configuration | |
echo -e "${GREEN}Configuring firewall...${NC}" | |
sudo ufw allow 3306/tcp | |
sudo ufw reload | |
# Completion message | |
echo -e "${GREEN}Percona Database Server installation and configuration completed!${NC}" | |
echo -e "${GREEN}Database: $DB_NAME${NC}" | |
echo -e "${GREEN}User: $DB_USER${NC}" | |
echo -e "${GREEN}Backup directory: $BACKUP_DIR${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment