Home > Database > Mysql Tutorial > body text

How to change password in MySQL using cmd command

伊谢尔伦
Release: 2018-05-28 14:47:43
Original
8315 people have browsed it

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
Copy after login

Method 1: Use SET PASSWORD command

mysql -u root 
  mysql> SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘newpass’);
Copy after login

Method 2: Use mysqladmin

mysqladmin -u root password “newpass” 
  如果root已经设置过密码,采用如下方法 
  mysqladmin -u root password oldpass “newpass”
Copy after login

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;
Copy after login

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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template