Skip to content

Instantly share code, notes, and snippets.

@syneart
Last active May 29, 2019 11:53
Show Gist options
  • Save syneart/d615ba2a87e2f8b24a2a3aeb53f73d67 to your computer and use it in GitHub Desktop.
Save syneart/d615ba2a87e2f8b24a2a3aeb53f73d67 to your computer and use it in GitHub Desktop.
Set default root password for MySQL 5.7 with Ubuntu when MySQL doesn't ask for root password at installing
#!/bin/sh
# | THIS SCRIPT IS TESTED CORRECTLY ON |
# |--------------------------------------------------------|
# | OS | MySQL | Test | Last test |
# |--------|--------------------------|------|-------------|
# | Ubuntu | Ver 14.14 Distrib 5.7.26 | OK | 16 May 2019 |
MYSQL_ROOT_PASSWORD=`head /dev/urandom | tr -dc A-Za-z0-9 | head -c10`
sudo mysql -uroot<<EOF
use mysql;
update user set authentication_string=password('$MYSQL_ROOT_PASSWORD') where user='root';
update user set plugin="mysql_native_password" where User='root';
flush privileges;
EOF
(mysql -uroot -p$MYSQL_ROOT_PASSWORD -e ";") >/dev/null 2>&1 && {
echo -e "\033[33m[INFO] Temporary password for your mysql's user root: $MYSQL_ROOT_PASSWORD\033[0m"
echo "[INFO] You can excute mysql_secure_installation to change ..."
}
@syneart
Copy link
Author

syneart commented May 16, 2019

Use below command to set default root password for MySQL 5.7 with Ubuntu on your own operating system.
wget -O - https://gist.githubusercontent.com/syneart/d615ba2a87e2f8b24a2a3aeb53f73d67/raw/mysql_native_password.sh | sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment