Last active
December 3, 2019 00:32
-
-
Save miamibc/cdf8e260a3e7276d4cd33842fe83a3e3 to your computer and use it in GitHub Desktop.
ubuntu 16 mysql 5.7 change root password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# this sctipt will change mysql root password in 3 seconds | |
# run it with one parameter - desired password | |
if [ -z "$1" ] | |
then | |
echo "Usage: $0 <new password>" | |
exit 1 | |
fi | |
# root is required for service restarts | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root" | |
exit 1 | |
fi | |
# stop mysql process | |
echo "Stopping mysql process" | |
service mysql stop | |
sleep 1 | |
# if not exists, create necessary directory for pid file | |
if [ ! -d "/var/run/mysqld" ]; then | |
mkdir -p /var/run/mysqld | |
chown mysql:mysql /var/run/mysqld | |
fi | |
# run mysqld_safe in background | |
echo "Running mysqld_safe in background" | |
mysqld_safe --skip-grant-tables & | |
# remember pid | |
pid=$! | |
# short pause to complete mysql initialization | |
sleep 5 | |
# print users info before/after and change password for user root | |
mysql << END | |
SELECT "Before:" AS ''; | |
SELECT User, authentication_string FROM mysql.user WHERE User='root'; | |
UPDATE mysql.user SET authentication_string=PASSWORD('$1') WHERE User='root'; | |
FLUSH PRIVILEGES; | |
SELECT "After:" AS ''; | |
SELECT User, authentication_string FROM mysql.user WHERE User='root'; | |
SHUTDOWN; | |
END | |
# wait for process to stop | |
echo "" | |
echo "Waiting for process to stop..." | |
wait $pid | |
# start normally | |
echo "Starting mysql-server normally" | |
service mysql start | |
service mysql status | |
echo "" | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment