MySQL修改密码或开启远程连接
一、修改密码
1、没有忘记密码
(1)执行mysql -u root -p ,回车后输入密码登录MySQL
mysql -u root -p
(2)切换到mysql库
use mysql
(3)修改密码
mysql5.7版本
update mysql.user set authentication_string = password('123456') where user='root';
mysql8.0版本(mysql 5.7.9 之后取消了password 函数,authentication_string=password(“123456”) 会报错)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
(4)刷新
flush privileges;
退出后重新登录即可
2、忘记密码
(1)修改mysql配置文件
自行找到my.cnf文件(默认在 /etc/my.cnf),在[mysqld]下加上skip-grant-tables
skip-grant-tables
(2)重启mysql
systemctl restart mysql
(3)执行mysql -u root -p ,直接回车登录MySQL
mysql -u root -p
(2)切换到mysql库
use mysql
(3)修改密码
mysql5.7版本
update mysql.user set authentication_string = password('123456') where user='root';
mysql8.0版本(mysql 5.7.9 之后取消了password 函数,authentication_string=password(“123456”) 会报错)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
(4)刷新
flush privileges;
(5)删除skip-grant-tables
(6)重启mysql
systemctl restart mysql
二、开启远程连接
1、执行mysql -u root -p ,回车后输入密码登录MySQL
mysql -u root -p
2、切换到mysql库
use mysql
3、查看现有的用户及允许连接的主机
select user,host from user;
可以看到root用户只允许本地连接
4、修改root用户权限
UPDATE user SET Host='%' where user='root' AND Host='localhost' LIMIT 1;
5、再次查看
select user,host from user;
6、刷新
flush privileges;
7、结束了,赶紧去试试吧!
elect user,host from user;
[外链图片转存中...(img-TUbTcEWJ-1654670030541)]
#### 6、刷新
flush privileges;
#### 7、结束了,赶紧去试试吧!