mysql数据库root用户无法登录/root用户被删解决方法

整体描述

之前在阿里云数据库上把远程访问数据库打开了,打开远程数据库访问的方法在此文章里: 数据库mysql无法远程访问,出现Host is not allowed to connect to this MySQL server解决办法。但是,打开之后有诈,数据库应该是被暴力破解了。下面记录一下解决方法。友情提示:远程访问数据库还是尽量不要开吧,要开也限制一下IP。

解决方法

1. 关闭数据库

登录阿里云服务器,关闭数据库服务,具体操作:

service mysqld stop

停止数据库

2. 去掉登录数据库认证

进入etc目录,在文件最后加上skip_grant_tables,文件如下:

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
validate_password = off
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

# 在此处添加
[mysqld]
skip_grant_tables

3. 进入数据库

此时,直接输入mysql,就可以进入数据库了。

mysql

4. 数据库操作

use mysql;

进入mysql数据库,查看当前存在的用户。

select user from user;

如果root用户还在,就直接修改root用户的密码。

UPDATE user SET password = password('newpassword') WHERE user = 'root';

如果root用户不在了,创建root用户并设置密码:
注:我是直接把之前的root删了重新创建的root用户,都被破解了谁知道他给我改没改root用户的配置。

insert into user set user='root',ssl_cipher='',x509_issuer='',x509_subject='';

update user set Host='localhost',select_priv='y', insert_priv='y',update_priv='y', Alter_priv='y',delete_priv='y',create_priv='y',drop_priv='y',reload_priv='y',shutdown_priv='y',Process_priv='y',file_priv='y',grant_priv='y',References_priv='y',index_priv='y',create_user_priv='y',show_db_priv='y',super_priv='y',create_tmp_table_priv='y',Lock_tables_priv='y',execute_priv='y',repl_slave_priv='y',repl_client_priv='y',create_view_priv='y',show_view_priv='y',create_routine_priv='y',alter_routine_priv='y',create_user_priv='y' where user='root';

flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

修改成功。退出数据库,将第二步里修改的文件进行恢复然后重启mysql
先停止数据库,再启动:

service mysqld stop
service mysqld start

再次登录,成功。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐