Revert to working version

main
Anthony 2024-08-30 22:28:09 +08:00
parent 3bc7e9fc09
commit 062a27944b
1 changed files with 13 additions and 13 deletions

View File

@ -3,14 +3,14 @@
# Automatically generate a new secure password for the root user # Automatically generate a new secure password for the root user
new_root_password=$(openssl rand -base64 12) new_root_password=$(openssl rand -base64 12)
# Generate random database name, user, and password for the new WordPress installation # Generate random database name, user, and password for the new database
DB_NAME="wp_$(openssl rand -hex 4)" DB_NAME="db_$(openssl rand -hex 4)"
DB_USER="wp_user_$(openssl rand -hex 4)" DB_USER="user_$(openssl rand -hex 4)"
DB_PASSWORD="$(openssl rand -base64 12)" DB_PASSWORD="$(openssl rand -base64 12)"
DB_HOST="127.0.0.1" # Change if your database is hosted elsewhere DB_HOST="localhost" # Change if your database is hosted elsewhere
echo "New root password will be: $new_root_password" echo "New root password will be: $new_root_password"
echo "New WordPress database credentials:" echo "New database credentials:"
echo "Database Name: $DB_NAME" echo "Database Name: $DB_NAME"
echo "Database User: $DB_USER" echo "Database User: $DB_USER"
echo "Database Password: $DB_PASSWORD" echo "Database Password: $DB_PASSWORD"
@ -30,7 +30,7 @@ echo "Resetting the root password..."
# Reset the root password in safe mode # Reset the root password in safe mode
sudo mysql -u root <<EOF sudo mysql -u root <<EOF
FLUSH PRIVILEGES; FLUSH PRIVILEGES;
ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY '$new_root_password'; ALTER USER 'root'@'localhost' IDENTIFIED BY '$new_root_password';
FLUSH PRIVILEGES; FLUSH PRIVILEGES;
EOF EOF
@ -63,8 +63,8 @@ else
exit 1 exit 1
fi fi
# Create WordPress database and user with the new root password # Create MySQL database and user with the new root password
echo "Creating WordPress database and user with the new root password..." echo "Creating MySQL database and user with the new root password..."
mysql -u root -p"$new_root_password" <<EOF mysql -u root -p"$new_root_password" <<EOF
CREATE DATABASE ${DB_NAME}; CREATE DATABASE ${DB_NAME};
CREATE USER '${DB_USER}'@'${DB_HOST}' IDENTIFIED BY '${DB_PASSWORD}'; CREATE USER '${DB_USER}'@'${DB_HOST}' IDENTIFIED BY '${DB_PASSWORD}';
@ -74,9 +74,9 @@ EOF
# Check if the database and user creation was successful # Check if the database and user creation was successful
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "WordPress database ${DB_NAME} and user ${DB_USER} created successfully with the specified password." echo "Database ${DB_NAME} and user ${DB_USER} created successfully with the specified password."
else else
echo "Failed to create WordPress database or user. Please check the MySQL status manually." echo "Failed to create database or user. Please check the MySQL status manually."
exit 1 exit 1
fi fi
@ -90,7 +90,7 @@ escaped_db_user=$(printf '%s\n' "$DB_USER" | sed 's:[\/&]:\\&:g')
escaped_db_password=$(printf '%s\n' "$DB_PASSWORD" | sed 's:[\/&]:\\&:g') escaped_db_password=$(printf '%s\n' "$DB_PASSWORD" | sed 's:[\/&]:\\&:g')
escaped_db_host=$(printf '%s\n' "$DB_HOST" | sed 's:[\/&]:\\&:g') escaped_db_host=$(printf '%s\n' "$DB_HOST" | sed 's:[\/&]:\\&:g')
echo "Updating wp-config.php with new WordPress database credentials..." echo "Updating wp-config.php with new database credentials..."
# Update wp-config.php with new database credentials using more precise sed commands # Update wp-config.php with new database credentials using more precise sed commands
sudo sed -i.bak -e "s/define( *'DB_NAME'.*/define('DB_NAME', '${escaped_db_name}');/" \ sudo sed -i.bak -e "s/define( *'DB_NAME'.*/define('DB_NAME', '${escaped_db_name}');/" \
@ -100,8 +100,8 @@ sudo sed -i.bak -e "s/define( *'DB_NAME'.*/define('DB_NAME', '${escaped_db_name}
# Check if wp-config.php was updated successfully # Check if wp-config.php was updated successfully
if grep -q "$DB_NAME" "$WP_CONFIG" && grep -q "$DB_USER" "$WP_CONFIG" && grep -q "$DB_PASSWORD" "$WP_CONFIG"; then if grep -q "$DB_NAME" "$WP_CONFIG" && grep -q "$DB_USER" "$WP_CONFIG" && grep -q "$DB_PASSWORD" "$WP_CONFIG"; then
echo "wp-config.php updated successfully with new WordPress database credentials." echo "wp-config.php updated successfully with new database credentials."
else else
echo "Failed to update wp-config.php. Please check the file manually." echo "Failed to update wp-config.php. Please check the file manually."
exit 1 exit 1
fi fi