mysql忘记root密码与root帐号被删除处理方法

windows服务器如果使用我司管理助手,重置方法请参考:http://faq.myhostadmin.net/faq/listagent.asp?unid=876

linux服务器如果使用WDCP系统,http://faq.myhostadmin.net/faq/listagent.asp?unid=722使用脚本进行重置。


说明:请根据自己主机系统及问题选择对应步骤。

1、查找修改配置文件

修改my.ini(windows系统)或my.cnf(linux系统)文件,

  windows系统:my.ini在mysql的安装目录下,如果是我司默认环境,路径是:d:\SOFT_PHP_PACKAGE\mysql\my.ini 

用记事本打开编辑在 [mysqld] 节下加入下面一行

  [mysqld]

  skip-grant-tables

  linux系统:ssh登陆服务器后执行mysql --help |grep my.cnf 回车

  [root@west9853 etc]# mysql --help |grep my.cnf

                      order of preference, my.cnf, $MYSQL_TCP_PORT,

  /etc/my.cnf /etc/mysql/my.cnf /www/wdlinux/etc/my.cnf ~/.my.cnf

  可查找到my.cnf位置,然后执行vi /www/wdlinux/etc/my.cnf 回车,按 进入编辑状态

  在 [mysqld] 下加入下面一行

  [mysqld]

  skip-grant-tables

  按esc,再按: 输入wq 回车

2.重启 MySQL 服务

windows系统在运行里面输入services.msc,打开服务从中找到mysql,然后右键选择重启。

linux系统执行service mysqld restart

3.重置密码

windows系统:打开 开始-运行,输入cmd回车,在弹出的黑色窗口中输入:

 mysql     回车

 use mysql;回车

linux系统:输入:

 mysql     回车

 use mysql;回车

(1)如果是误删了root用户,则使用下面的语句创建root用户:

insert into user set Host='localhost',User='root',Password=Password('111111'),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',Event_priv='Y',Trigger_priv='Y';

    (注意:复制语句后,把回车换行符去掉)

(2)如果是忘记了root用户的密码,使用下面的语句修改root密码输入(注意版本):

mysql5.1、mysql5.6版本

update user set Password=Password('111111') where User='root';    //按回车,其中111111换成您需要设定新的密码

flush privileges;                //刷新权限

                                             

mysql5.7版本

update mysql.user set authentication_string = password('testpassword') where user='root';  //testpassword替换为需要设定的新密码

flush privileges;                   //刷新权限


4.还原mysql配置

windows系统:

找到my.ini或my.cnf文件,将[mysqld] 节下面的skip-grant-tables 去掉,或者前面加上#字符保存以后重启mysql服务。

linux系统:

vi /www/wdlinux/etc/my.cnf,按上下键移动找到skip-grant-tables 连按两次d即可删除,然后输入:wq保存退出,输入service mysqld restart重启


日期:2015-01-05

打印 】