Fix DB Account Issue

main
Anthony 2024-09-09 23:05:01 +08:00
parent 99c42497ca
commit 3c70bfc4a3
1 changed files with 3 additions and 3 deletions

View File

@ -7,7 +7,7 @@ new_root_password=$(openssl rand -base64 12)
DB_NAME="db_$(openssl rand -hex 4)" DB_NAME="db_$(openssl rand -hex 4)"
DB_USER="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="localhost" # Change if your database is hosted elsewhere DB_HOST="127.0.0.1" # 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 database credentials:" echo "New database credentials:"
@ -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'@'localhost' IDENTIFIED BY '$new_root_password'; ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY '$new_root_password';
FLUSH PRIVILEGES; FLUSH PRIVILEGES;
EOF EOF
@ -68,7 +68,7 @@ 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}';
GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'${DB_HOST}'; GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'${DB_HOST}' IDENTIFIED BY '${DB_PASSWORD}';
FLUSH PRIVILEGES; FLUSH PRIVILEGES;
EOF EOF