首先登录MySQL。
格式:
mysql> set password for 用户名@'localhost' = password('新密码');
例子:
mysql> set password for root@'localhost' = password('123');
mysqladmin
格式:
mysqladmin -u用户名 -p旧密码 password 新密码
例子:
mysqladmin -uroot -p123456 password 123
首先登录MySQL。
mysql> use mysql;
mysql> update user set authentication_string=password('123') where user='root' and host='localhost';
mysql> flush privileges;
mysqld --skip-grant-tables
回车。--skip-grant-tables
的意思是启动MySQL服务的时候跳过权限表认证。mysql\bin
目录。mysql
回车,如果成功,将出现MySQL提示符>
。use mysql;
。mysql>update user set authentication_string=password("123") where user="root";
mysql>flush privileges;
root
和刚才设置的新密码123
登录。sudo service mysql stop
cd /etc/mysql/mysql.conf.d
sudo nano mysqld.cnf
在[mysqld]
下面添加 skip-grant-tables
(可以试试这个命令:mysqld --skip-grant-tables
)
sudo service mysql start
mysql -u root -p
执行以下命令
mysql>update mysql.user set authentication_string=password('newpassword') where user='root';
mysql>update mysql.user set plugin="mysql_native_password";
mysql>flush privileges;
mysql>quit;
skip-grant-tables
sudo service mysql restart
/etc/my.cnf
,最下面添加skip-grant-tables=1
mysql
服务systemctl restart mysqld
mysql -u root -p
执行以下命令
mysql>update mysql.user set authentication_string=password('newpassword') where user='root';
mysql>update mysql.user set plugin="mysql_native_password";
mysql>flush privileges;
mysql>quit;
skip-grant-tables=1
systemctl restart mysqld
解决办法
1、 修改用户密码
mysql> alter user 'root'@'localhost' identified by 'youpassword';
或者
mysql> set password=password("youpassword");
2、刷新权限
mysql> flush privileges;
此故障一般见于非mysql终端下直接用-e
执行命令的时候。解决办法:在命令后加上--connect-expired-password
即可:
mysql -u root -pyoupassword -e "show databases;" --connect-expired-password
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:MySQL—修改root密码方法总结(包括已知密码和忘记密码的情况)