# mysql-reset-root-password.sh # Simple script to reset the mysql root password # # Created by Paul Maunders on 27-12-2008. # Copyright 2008 Fubra Limited. All rights reserved. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # You may obtain a copy of the License at: # http://www.gnu.org/licenses/gpl-3.0.txt # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You must run this script as the root user if [ `id -u` != 0 ]; then echo "You must run this script as root, and do not use sudo as it doesn't seem to work!. Aborted."; exit fi # MYSQL_ROOT_PASSWORD can be passed as the first parameter to the script, if not passed, ask for it. if [ -n "$1" ] then MYSQL_ROOT_PASSWORD=$1 fi while [ ! -n "$MYSQL_ROOT_PASSWORD" ] do echo "Please define a MySQL root password, or you can pass this to the script as the first parameter." echo -n "Password:" read MYSQL_ROOT_PASSWORD done echo "New root password: $MYSQL_ROOT_PASSWORD" verify=0 while [[ $verify != Y && $verify != n ]] do echo -n "Do you wish to update the root password to the one shown above? [Y/n]" read verify done if [ $verify != Y ] then echo "Aborted." exit fi # Terminate any mysqld processes (including mysqld_safe) killall -15 mysqld # Wait a while to give mysqld_safe a chance to exit sleep 1 # Run mysql in bootstrap mode, and pipe in the update password command echo "UPDATE mysql.user SET Password=PASSWORD('$MYSQL_ROOT_PASSWORD') WHERE User='root';" | mysqld --bootstrap echo "The root password has been reset. Start MySQL in the normal way and test it."