Linux下忘记mysql的root用户密码解决方法 忘记mysql密码 mysql修改密码方法
跳转到导航
跳转到搜索
原因
不小心 把自己在公司的测试机上的mysql root 密码给忘记了,网上有很多方法,但这个是我试过的 包成功哦
解决过程
注意 有时看 .mysql_history 就行了
首先 要把mysqld 进程停止 最后是用正常的停止服务方法 实在不行 kill or killall 你自己选择
接着
用--skip-grant-tables参数启动mysqld
# 其中/usr/local/mysql/bin是我的mysql安装目录
[root@localhost /]#/usr/local/mysql/bin/mysqld_safe --skip-grant-tables&
新办法 也可用于 mariadb
my.cnf
在[mysqld] 下添加
skip-grant-tables=1
#不用密码进入mysql
[root@localhost /]/usr/local/mysql/bin/mysql
#切换到mysql database good
mysql> use mysql;
#将mysql root密码该为123456#了
#mysql5.7
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
# or
update mysql.user set authentication_string=PASSWORD('123456#') where user='root';
flush privileges;
特别提醒注意的一点是,新版的mysql数据库下的user表中已经没有Password字段了
#这个是mysql5.6之前
UPDATE user SET password=password('123456#') WHERE user='root';
FLUSH PRIVILEGES;
killall mysqld
#登录正常
mysql -uroot -p
mysql5.7忘记密码
docker mysql5.6 也一样 不过改了配置后 docker restart mysqlid 就行了
vim /etc/my.cnf 或者 vi /etc/my.cnf.d/server.cnf
top your databases
systemctl stop mysqld
Modify /etc/my.cnf file add skip-grant-tables
vi /etc/my.cnf # or
vi /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
skip-grant-tables
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set authentication_string=PASSWORD("password") where User='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
Restart MySQL database
service mysqld restart 新密码
mysql -u root -p
https://stackoverflow.com/questions/43013730/how-to-change-root-password-in-mysql5-7
https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
mariadb
sudo /etc/init.d/mysql stop
Stopping mysql (via systemctl): mysql.service.
evan@myxps:~/python/jumpserver$ sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf
skip-grant-tables=1
mysql -uroot 无密码登录
use mysql;
#选择一句就行了
update user set authentication_string=password('root'),plugin='mysql_native_password' where user='root';
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
flush privileges;
vim /etc/mysql/mariadb.conf.d/50-server.cnf
将
skip-grant-tables=1 这句#注释掉
https://www.cnblogs.com/messhair/p/11782850.html