#!/bin/bash # Exit on any error set -e MANGOS_DIR="$HOME/mangos" LIBS_DIR="$MANGOS_DIR/lib" # Download required MySQL client library to user space if [ ! -f "$LIBS_DIR/libmysqlclient.so.21" ]; then echo "Downloading required MySQL client library..." mkdir -p "$LIBS_DIR" if ! curl -L "https://public.pez.solutions/libmysqlclient.so.21" -o "$LIBS_DIR/libmysqlclient.so.21"; then echo "Failed to download libmysqlclient.so.21" exit 1 fi echo "Setting correct permissions for library file..." chmod 755 "$LIBS_DIR/libmysqlclient.so.21" fi MARIADB_DIR="$MANGOS_DIR/mariadb" MANGOS_DOWNLOAD="https://public.pez.solutions/one.zip" DOWNLOAD_CACHE="$HOME/.mangos-downloads" MANGOS_ZIP="$DOWNLOAD_CACHE/mangos-one.zip" # Function to check if MariaDB is already running check_mariadb_running() { if [ -f "$MARIADB_DIR/mysqld.pid" ]; then pid=$(cat "$MARIADB_DIR/mysqld.pid") if kill -0 "$pid" 2>/dev/null; then return 0 fi fi return 1 } # Function to check if MariaDB is installed and initialized check_mariadb_installed() { if [ -d "$MARIADB_DIR/data/mysql" ]; then return 0 fi return 1 } # Check if MaNGOS is properly installed check_mangos_installed() { # Simply check if the one directory exists and has the expected structure if [ -d "$MANGOS_DIR/one" ] && \ [ -d "$MANGOS_DIR/one/bin" ] && \ [ -d "$MANGOS_DIR/one/db" ]; then echo "Found existing MaNGOS installation" return 0 fi return 1 } echo "Setting up MaNGOS Server..." # Create directories mkdir -p "$MANGOS_DIR" mkdir -p "$DOWNLOAD_CACHE" cd "$MANGOS_DIR" # Download and extract MaNGOS if needed if ! check_mangos_installed; then echo "MaNGOS not found, installing..." # Download MaNGOS if not already downloaded if [ ! -f "$MANGOS_ZIP" ]; then echo "Downloading MaNGOS (this might take a while)..." curl -L "$MANGOS_DOWNLOAD" -o "$MANGOS_ZIP" else echo "Using existing MaNGOS download from $MANGOS_ZIP" fi # Extract MaNGOS echo "Extracting MaNGOS..." unzip -o "$MANGOS_ZIP" else echo "MaNGOS already installed in $MANGOS_DIR/one, skipping download and extract..." fi # Setup MariaDB if not already installed if ! check_mariadb_installed; then echo "Setting up MariaDB..." mkdir -p "$MARIADB_DIR" cd "$MARIADB_DIR" # Download MariaDB binary tarball for Linux MARIADB_VERSION="11.6.2" MARIADB_URL="https://mirrors.xtom.nl/mariadb///mariadb-11.6.2/bintar-linux-systemd-x86_64/mariadb-11.6.2-linux-systemd-x86_64.tar.gz" MARIADB_TAR="$DOWNLOAD_CACHE/mariadb-${MARIADB_VERSION}.tar.gz" # Download MariaDB if not already downloaded if [ ! -f "$MARIADB_TAR" ]; then echo "Downloading MariaDB..." curl -L "$MARIADB_URL" -o "$MARIADB_TAR" else echo "Using existing MariaDB download from $MARIADB_TAR" fi echo "Extracting MariaDB..." tar xf "$MARIADB_TAR" --strip-components=1 # Initialize MariaDB data directory with root password echo "Initializing MariaDB..." mkdir -p data ./scripts/mysql_install_db --datadir="$MARIADB_DIR/data" --auth-root-authentication-method=normal # Create configuration file without passwords initially cat > my.cnf << EOF [mysqld] datadir=$MARIADB_DIR/data socket=$MARIADB_DIR/mysql.sock port=3306 user=$USER pid-file=$MARIADB_DIR/mysqld.pid skip-grant-tables [client] socket=$MARIADB_DIR/mysql.sock port=3306 [mysql] socket=$MARIADB_DIR/mysql.sock port=3306 EOF # Create init file to set root password on first start cat > init.sql << EOF SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mangos'); FLUSH PRIVILEGES; EOF else echo "MariaDB already installed, skipping installation..." cd "$MARIADB_DIR" fi # Check if MariaDB is already running if check_mariadb_running; then echo "MariaDB is already running, using existing instance..." else # Start MariaDB server with error logging echo "Starting MariaDB server..." ./bin/mysqld --defaults-file="$MARIADB_DIR/my.cnf" --user="$USER" > "$MARIADB_DIR/startup.log" 2>&1 & MYSQL_PID=$! # Function to check if MySQL is running wait_for_mysql() { local max_attempts=30 local attempt=1 echo "Waiting for MariaDB to start..." while [ $attempt -le $max_attempts ]; do # Check if process is still running if ! kill -0 $MYSQL_PID 2>/dev/null; then echo "MariaDB process died. Checking startup log:" cat "$MARIADB_DIR/startup.log" return 1 fi # Try to connect without password if ./bin/mysql --protocol=socket --socket="$MARIADB_DIR/mysql.sock" -u root -e "SELECT 1" >/dev/null 2>&1; then echo "MariaDB is running!" # Set up root password and secure the installation echo "Securing MariaDB installation..." ./bin/mysql --protocol=socket --socket="$MARIADB_DIR/mysql.sock" -u root << EOF FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY 'mangos'; FLUSH PRIVILEGES; EOF # Update my.cnf to remove skip-grant-tables sed -i '/skip-grant-tables/d' "$MARIADB_DIR/my.cnf" # Restart MariaDB to apply changes echo "Restarting MariaDB to apply security settings..." kill $MYSQL_PID wait $MYSQL_PID # Make sure old socket is removed rm -f "$MARIADB_DIR/mysql.sock" # Start MariaDB with normal security echo "Starting MariaDB with normal security..." ./bin/mysqld --defaults-file="$MARIADB_DIR/my.cnf" --user="$USER" > "$MARIADB_DIR/startup.log" 2>&1 & MYSQL_PID=$! # Wait for socket file to appear echo "Waiting for MariaDB socket file..." local socket_attempts=30 local socket_attempt=1 while [ $socket_attempt -le $socket_attempts ]; do if [ -S "$MARIADB_DIR/mysql.sock" ]; then echo "Socket file created" # Wait a bit more for MySQL to be ready sleep 5 # Test connection with new password if ./bin/mysql --protocol=socket --socket="$MARIADB_DIR/mysql.sock" -u root -pmangos -e "SELECT 1" >/dev/null 2>&1; then echo "Successfully connected after restart" return 0 fi fi echo "Waiting for socket file (attempt $socket_attempt/$socket_attempts)..." sleep 2 socket_attempt=$((socket_attempt + 1)) done echo "Error: Socket file not created after restart" return 1 fi echo "Attempt $attempt of $max_attempts: MariaDB not ready yet..." sleep 2 attempt=$((attempt + 1)) done echo "Failed to start MariaDB after $max_attempts attempts. Checking startup log:" cat "$MARIADB_DIR/startup.log" return 1 } # Wait for MariaDB to start if ! wait_for_mysql; then echo "Error: MariaDB failed to start properly" if [ -n "$MYSQL_PID" ]; then echo "Killing MySQL process..." kill $MYSQL_PID 2>/dev/null || true wait $MYSQL_PID 2>/dev/null || true fi exit 1 fi # Test connection with explicit socket path if ! ./bin/mysql -u root --protocol=socket --socket="$MARIADB_DIR/mysql.sock" -e "SELECT 1" >/dev/null 2>&1; then echo "Error: Cannot connect to MariaDB after startup" echo "Checking socket file..." ls -l "$MARIADB_DIR/mysql.sock" || echo "Socket file not found!" exit 1 fi fi # Function to run MySQL commands safely run_mysql_command() { local command="$1" local user="$2" local pass="$3" local max_attempts=3 local attempt=1 while [ $attempt -le $max_attempts ]; do if [ -n "$pass" ]; then ./bin/mysql --protocol=socket --socket="$MARIADB_DIR/mysql.sock" -u "$user" -p"$pass" -e "$command" 2>/dev/null && return 0 else ./bin/mysql --protocol=socket --socket="$MARIADB_DIR/mysql.sock" -u "$user" -e "$command" 2>/dev/null && return 0 fi echo "Attempt $attempt failed, retrying..." sleep 2 attempt=$((attempt + 1)) done return 1 } # Verify connection and create databases if run_mysql_command "SELECT 1" "root" "mangos"; then echo "Successfully connected to MariaDB" # Check if databases already exist if ! run_mysql_command "SHOW DATABASES LIKE 'mangos1'" "root" "mangos"; then echo "Setting up databases..." # Create databases run_mysql_command " CREATE DATABASE IF NOT EXISTS mangos1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE DATABASE IF NOT EXISTS character1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE DATABASE IF NOT EXISTS realmd DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER IF NOT EXISTS 'mangos'@'localhost' IDENTIFIED BY 'mangos'; GRANT ALL PRIVILEGES ON *.* TO 'mangos'@'localhost'; FLUSH PRIVILEGES; " "root" "mangos" # Import database dumps echo "Importing database dumps..." for DB in mangos1 character1 realmd; do SQLDIR="$MANGOS_DIR/one/db/database" case $DB in mangos1) SQLFILE="$SQLDIR/World/Setup/FullDB/mangos1.sql" ;; character1) SQLFILE="$SQLDIR/Character/Setup/character1.sql" ;; realmd) SQLFILE="$SQLDIR/Realm/Setup/realmd.sql" ;; esac if [ -f "$SQLFILE" ]; then echo "Importing $DB database from $SQLFILE..." if ! ./bin/mysql --protocol=socket --socket="$MARIADB_DIR/mysql.sock" -u mangos -pmangos "$DB" < "$SQLFILE"; then echo "Warning: Failed to import $DB database" fi else echo "Warning: SQL file not found: $SQLFILE" echo "Searching for $DB.sql in $SQLDIR..." find "$SQLDIR" -name "$DB.sql" -type f -print fi done else echo "Databases already exist, skipping database setup..." fi else echo "Error: Could not connect to MariaDB" echo "Checking MariaDB status..." ps aux | grep mysqld echo "Checking socket file..." ls -l "$MARIADB_DIR/mysql.sock" echo "Checking startup log..." cat "$MARIADB_DIR/startup.log" exit 1 fi # Only stop MariaDB if we started it if [ -n "$MYSQL_PID" ]; then echo "Stopping MariaDB server..." if ! ./bin/mysqladmin --protocol=socket --socket="$MARIADB_DIR/mysql.sock" -u root -pmangos shutdown; then echo "Failed to shutdown MariaDB gracefully, forcing shutdown..." kill $MYSQL_PID fi wait $MYSQL_PID fi echo "Setup complete!" echo "You can now use the MaNGOS Server Manager to start the servers." echo "Database credentials:" echo "User: mangos" echo "Password: mangos" echo "Databases: mangos1, character1, realmd"