MySQL远程登录的设置

MySQL远程登录的设置

云平台上安装了MySQL docker环境后,需要进行远程登录的设置。MySQL镜像为8.0(8.0以下的设置有一些区别),数据库用户为root。
mysql -u root -p (输入初始密码登录123456)

#create user root@'%' identified by 'cloudSpring091845@#';
update user set host = '%' where user = 'root';
#grant all privileges on to 'root'@'%' ;
flush privileges; #刷新权限
alter user 'root'@'%' identified with mysql_native_password by 'password';

 

这个时候在客户端填充对应的host公网IP地址、root和password(cloudSpring091845@#)就可以进行访问了。
现在存在一个问题,就是你在云主机的shell上,会出现:
mysql -u root -p 是旧密码(123456)
mysql -u localhost - u root -p 是旧密码(123456)
mysql -u 127.0.01 -u root -p是新密码(cloudSpring091845@#)
mysql -u 内网IP地址 -u root -p是新密码(cloudSpring091845@#)
mysql -u 外网IP地址 -u root -p是新密码(cloudSpring091845@#)

select host, user, authentication_string from user;
会发现root对应两个用户%和localhost,且authentication_string不一样。

这个时候可以通过sql修改localhost/root对应的authentication_string,修改后
使用mysql -u root -p就可以使用新密码登录了,与其他的保持一致。

但是存在一个问题(原因不明),select host, user, authentication_string from user;
看到的密码仍然不一样,虽然localhost能用相同的新密码登录,重启主机和服务也不行。

解决方式如下:
//root用户(数据库用户)对应的密码置为空
update user set authentication_string=‘’ where user=‘root’;
//localhost(访问数据库的主机)
alter user root@‘localhost’ identified with mysql_native_password by ‘cloudSpring091845@#’;
//% (访问数据库的任意主机,不包括localhost)
alter user root@‘%’ identified with mysql_native_password by ‘cloudSpring091845@#’;
flush privileges;
做完后,得到结果:
能远程登录
本地shell的localhost能用新密码登录
在数据库中查看,localhost与%的authentication_string相同。 

 

posted @   CodeWhisperer001  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示