MySQL修改密码或开启远程连接

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

image-20220606231002422

(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;

image-20220606232706339

可以看到root用户只允许本地连接

4、修改root用户权限
UPDATE user SET Host='%' where user='root' AND Host='localhost' LIMIT 1;
5、再次查看
select user,host from user;

image-20220606233356252

6、刷新
flush privileges;
7、结束了,赶紧去试试吧!

elect user,host from user;


[外链图片转存中...(img-TUbTcEWJ-1654670030541)]

#### 6、刷新

flush privileges;


#### 7、结束了,赶紧去试试吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值