mb-admin/scripts/dbreset.sh

68 lines
2.0 KiB
Bash
Raw Normal View History

2025-08-26 16:26:33 +00:00
#!/bin/bash
# Generate a secure password
new_password=$(openssl rand -base64 12)
echo "🔐 New MariaDB root password will be: $new_password"
# Stop MariaDB
echo "🛑 Stopping MariaDB service..."
sudo systemctl stop mariadb
sleep 3
# Start MariaDB in safe mode
echo "🔧 Starting MariaDB in safe mode (skip-grant-tables)..."
sudo mysqld_safe --skip-grant-tables --skip-networking --skip-name-resolve &
sleep 5
# Check if mysqld is running
if ! pgrep mysqld > /dev/null; then
echo "❌ Failed to start mysqld_safe. Exiting."
exit 1
fi
# Reset root password
echo "🔄 Resetting root password..."
mysql -u root << EOF
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY '$new_password';
ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY '$new_password';
FLUSH PRIVILEGES;
EOF
# Check if reset succeeded
if [ $? -ne 0 ]; then
echo "❌ Failed to reset password. Cleaning up..."
sudo pkill -f mysqld
exit 1
fi
# Stop safe mode
echo "🛑 Stopping safe mode..."
sudo pkill -f mysqld_safe
sudo pkill -f mysqld
sleep 3
# Start MariaDB normally
echo "🟢 Starting MariaDB normally..."
sudo systemctl start mariadb
if sudo systemctl is-active --quiet mariadb; then
echo "✅ MariaDB is running."
echo "🔐 Root password has been reset to: $new_password"
echo ""
echo "📌 IMPORTANT:"
echo " 1. Update /etc/phpMyAdmin/config.inc.php:"
echo " \$cfg['Servers'][\$i]['user'] = 'root';"
echo " \$cfg['Servers'][\$i]['password'] = '$new_password';"
echo " \$cfg['Servers'][\$i]['auth_type'] = 'config';"
echo ""
echo " 2. Restart the database node in the Virtuozzo control panel!"
echo " This ensures Apache/phpMyAdmin can reconnect."
else
echo "❌ Failed to start MariaDB. Run: sudo systemctl status mariadb"
exit 1
fi
sudo sed -i "s/\(\['password'\]\s*=\s*'\)[^']*'/\1$new_password'/" /etc/phpMyAdmin/config.inc.php
sudo sed -i "s/\(\['auth_type'\]\s*=\s*'\)[^']*'/\1config'/" /etc/phpMyAdmin/config.inc.php
sudo sed -i "s/\(\['user'\]\s*=\s*'\)[^']*'/\1root'/" /etc/phpMyAdmin/config.inc.php