一键重置mysql的root密码脚本
@echooff titlemysql ::从注册表找到Mysql的安装路径写入文件mysql.txt regqueryHKLM\SYSTEM\ControlSet001\Services\MySQL|find/I"ImagePath">C:\mysql.txt if%errorlevel%neq0( echoMySQLnotfound pause exit ) ::以”为分隔符,截取第二段内容保存到变量mysqlPath FOR/Ftokens^=2^delims^=^"%%iin(C:\mysql.txt)dosetmysqlPath=%%i delC:\mysql.txt/f ::路径中/替换为\ setmysqlPath=%mysqlPath:/=\% ::删除路径最后一个字符(该字符不可见,可能是回车换行之类的) setmysqlPath=%mysqlPath:~0,-1% :BACKTOMAIN ::取得路径最后一个字符看等不等于\ setcharacter=%mysqlPath:~-1,1% ::如果最后一个字符不等于\,那么跳转到GETPATH删除mysqlPath的最后一个字符 ifnot%character%==\gotoGETPATH ::进入mysql安装路径C:\ProgramFiles\MySQL\MySQLServer5.0\bin cd/d"%mysqlPath%" ::echo%mysqlPath% if%errorlevel%neq0( echoMySQLnotfound pause exit ) ::禁用mysql服务,跳过权限验证修改密码 taskkill/F/IMmysqld-nt.exe netstopmysql>nul start/bmysqld-nt--skip-grant-tables ping-n2127.0.0.1>nul echousemysql>c:\config.tmp echoupdateusersetpassword=password("")whereuser="root";>>C:\config.tmp echoflushprivileges;>>C:\config.tmp echoexit>>C:\config.tmp ::因为是交互式,所以从文件读取内容 mysql<C:\config.tmp taskkill/F/IMmysqld-nt.exe netstopmysql>nul netstartmysql delC:\config.tmp/F pause exit ::删除路径最后一个字符,跳回主程序 :GETPATH setmysqlPath=%mysqlPath:~0,-1% gotoBACKTOMAIN
如果是用wamp一键安装需要改一些脚本,主要是注册表搜索路径改变,返回值改变,服务名改变了,mysql安装路径变成D:\wamp\bin\mysql\mysql5.5.24\bin,少了mysqld-nt这个东西,进程里面也没有mysqld-nt.exe
@echooff titlemysql regqueryHKLM\SYSTEM\ControlSet001\Services\wampmysqld|find/I"ImagePath">C:\mysql.txt if%errorlevel%neq0( echoMySQLnotfound pause exit ) FOR/F"tokens=3delims="%%iin(C:\mysql.txt)dosetmysqlPath=%%i delC:\mysql.txt/f setmysqlPath=%mysqlPath:/=\% setmysqlPath=%mysqlPath:~0,-1% :BACKTOMAIN setcharacter=%mysqlPath:~-1,1% ifnot%character%==\gotoGETPATH cd/d"%mysqlPath%" ::echo%mysqlPath% if%errorlevel%neq0( echoMySQLnotfound pause exit ) taskkill/F/IMmysqld.exe netstopwampmysqld>nul start/bmysqld-nt--skip-grant-tables ping-n2127.0.0.1>nul echousemysql>c:\config.tmp echoupdateusersetpassword=password("")whereuser="root";>>C:\config.tmp echoflushprivileges;>>C:\config.tmp echoexit>>C:\config.tmp mysql<C:\config.tmp taskkill/F/IMmysqld.exe netstopwampmysqld>nul netstartwampmysqld delC:\config.tmp/F pause exit :GETPATH setmysqlPath=%mysqlPath:~0,-1% gotoBACKTOMAIN