Four small ways to change the default password for MySQL

  • 2020-11-18 06:30:39
  • OfStack

For THE windows platform, the MySQL database is installed and the license tables and accounts are generated by default. You do not need to perform the mysql_install_db script to generate the account and permissions tables as you did on the Unix platform. However, if you do not install MySQL in MSI format, you will need to manually add a new password to the root account after installation because root is not passpass-protected by default, and many non-native connections will fail if you do not re-assign the root account password.

Method 1: Use the SET PASSWORD command to update the password as follows:


c:>mysql -u root

mysql>set password for 'root'@'localhost'=password('newpasswd');

mysql>set password for 'root'@'%'=password('newpasswd'); // This optional 

With this setting, the root password is changed to newpasswd and the root user root password is set.

Method 2: Use mysqladmin


mysqladmin -u root password "newpass"

If root has already set a password, do the following


 mysqladmin -u root password oldpass "newpass"

Method 3: Edit the user table directly with UPDATE


mysql -u root

  mysql> use mysql;

  mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';

  mysql> FLUSH PRIVILEGES;

You can do this if you lose your root password


mysqld_safe --skip-grant-tables&

  mysql -u root mysql

  mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';

  mysql> FLUSH PRIVILEGES;

This is the end of the MySQL database default password change, after the default password change you can do more operations, I hope that the above content has been helpful to you.


Related articles: