Skip to content

Instantly share code, notes, and snippets.

@ponych
Created May 5, 2016 09:11
Show Gist options
  • Save ponych/78421695d5b20ab186674aa9acad1bbd to your computer and use it in GitHub Desktop.
Save ponych/78421695d5b20ab186674aa9acad1bbd to your computer and use it in GitHub Desktop.
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script!"
exit 1
fi
mysql_root_password=""
read -p "Enter New MySQL root password: " mysql_root_password
if [ "${mysql_root_password}" = "" ]; then
echo "Error: Password can't be NULL!!"
exit 1
fi
echo "Stoping MySQL..."
/etc/init.d/mysql stop
echo "Starting MySQL with skip grant tables"
/usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
echo "using mysql to flush privileges and reset password"
sleep 5
echo "update user set password = Password('${mysql_root_password}') where User = 'root'"
/usr/bin/mysql -u root mysql << EOF
update user set password = Password('${mysql_root_password}') where User = 'root';
EOF
reset_status=`echo $?`
if [ ${reset_status} = "0" ]; then
echo "Password reset succesfully. Now killing mysqld softly"
killall mysqld
sleep 5
echo "Restarting the actual mysql service"
/etc/init.d/mysql start
echo "Password successfully reset to '${mysql_root_password}'"
else
echo "Reset MySQL root password failed!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment