linux重启后,mysql的root用户丢失?

本用户对云服务器进行重启操作后,登录mysql发现怎么也登不上去,
解除权限登录进去查看用户root竟然不存在了。
针对这一问题进行解决:

1.创建root用户

mysql> use mysql;
mysql> create user 'root'@'localhost' identified by 'root';

由于开启skip-grant-tables权限,上述命令无法执行,可能会报以下错误

mysql> ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

输入:

mysql> flush privileges;#刷新配置

再次重新创建用户

mysql> create user 'root'@'localhost' identified by 'root';

如果再次报错,输入:

mysql> drop user 'root'@'localhost';

不报权限错误,输入:

mysql> create user 'root'@'localhost' identified by 'root';

用户创建成功后,给root增加权限

2.添加权限

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; #赋予所有库所有表操作权限
mysql> flush privileges;
mysql> exit;

给my.cnf文件开启的skip-grant-tables权限注释掉
重启mysql

3.解决远程无法登录mysql

在这里插入图片描述
修改localhost,个人使用为了方便就更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从"localhost"改成"%"

mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
mysql>FLUSH PRIVILEGES;

设置好mysql就可以远程登录了,但是这是不安全的。只需要知道ip和账户密码就可以操作你的数据库
在这里插入图片描述
黑客:以下数据库已被删除:kuangstudy, school。 我们有完整的备份。 要恢复它,您必须向我们的比特币地址bc1q2h85s00w5p0unqf3cs6vtp8pa9jd0y7zxa3cma支付0.005比特币(BTC)。 如果您需要证明,请通过以下电子邮件与我们联系。 tian23@tutanota.com 。 任何与付款无关的邮件都将被忽略!

设置指定ip登录

官方给出my.cnf的bind-address无法绑定多个ip,是不可行的,通过mysql设置无法指定多个ip访问

如何设置指定的ip才能访问

防火墙设置
下载iptables服务

yum install -y iptables-service
systemctl restart iptables
systemctl enable iptables

设置所有ip不能访问3306端口

/sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP

设置主机端口号3306只能指定ip才能访问

/sbin/iptables -A INPUT -p tcp -s ip地址 --dport 3306 -j ACCEPT

保存规则

service iptables save

重启

systemctl restart iptables

查看3306规则

echo -e "target prot opt source destination\n$(iptables -L INPUT -n | grep 3306)"
#查看`iptables`保存规则
cat /etc/sysconfig/iptables
#查看iptables运行状况
iptables -L -n -v
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值