When you first create MySQL, you generally need to change the password; moreover, I have encountered operating on several servers and forgotten the root password of one of them (remember the user password, root has not been used for a long time), so Find the following method online and record it.
1. Initialize password setting
/etc/init.d/mysql stop cd /usr/local/mysql mysqld_safe –user=mysql –skip-grant-tables –skip-networking & mysql -u root mysql mysql > UPPATE user SET password=PASSWORD(‘newpassword’) where USER=’root’; mysql > FLUSH PRIVILEGES; mysql > quit ; /etc/init.d/mysql restart mysql -u root -p
Method 1: Use SET PASSWORD command
mysql -u root mysql> SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘newpass’);
Method 2: Use mysqladmin
mysqladmin -u root password “newpass” 如果root已经设置过密码,采用如下方法 mysqladmin -u root password oldpass “newpass”
Method 3: Use UPDATE directly Edit the user table
mysql -u root mysql> use mysql; mysql> UPDATE user SET Password = PASSWORD(‘newpass’) WHERE user = ‘root’; mysql> FLUSH PRIVILEGES;
2. Set the root password when you lose the root password
Method 1:
mysqld_safe –skip-grant-tables& mysql -u root mysql mysql> UPDATE user SET password=PASSWORD(“new password”) WHERE user=’root’; mysql> FLUSH PRIVILEGES;
Method 2:
1. Modify mysql Configuration file (default is /etc/my.cnf), add a line skip-grant-tables under [mysqld]
2. After saving the configuration file, restart the mysql service service mysqld restart
3. Mysql -u root -p log in to mysql, then press Enter without entering the password, and then change the password according to the above process
4. After the password is changed, delete the configuration file according to process 1. That line, then restart the mysql service
The above is the detailed content of How to change password in MySQL using cmd command. For more information, please follow other related articles on the PHP Chinese website!