一键重置mysql的root密码脚本

 @echo off 

title MysqL 

::从注册表找到MysqL安装路径写入文件MysqL.txt 
reg query HKLM\SYstem\ControlSet001\Services\MysqL | find /I "ImagePath">C:\MysqL.txt 
if %errorlevel% neq 0 ( 
echo MysqL not found 
pause 
exit 
) 

::以”为分隔符,截取第二段内容保存到变量MysqLPath 
FOR /F tokens^=2^ delims^=^" %%i in (C:\MysqL.txt) do set MysqLPath=%%i 
del C:\MysqL.txt /f 

::路径中/替换为\ 
set MysqLPath=%MysqLPath:/=\% 

::删除路径最后一个字符(该字符不可见,可能是回车换行之类的) 
set MysqLPath=%MysqLPath:~0,-1% 

:BACKTOMAIN 

::取得路径最后一个字符看等不等于\ 
set character=%MysqLPath:~-1,1% 

::如果最后一个字符不等于\,那么跳转到GETPATH删除MysqLPath的最后一个字符 
if not %character% == \ goto GETPATH 

::进入MysqL安装路径C:\Program Files\MysqL\MysqL Server 5.0\bin 
cd /d "%MysqLPath%" 

::echo %MysqLPath% 

if %errorlevel% neq 0 ( 
echo MysqL not found 
pause 
exit 
) 

::禁用MysqL服务,跳过权限验证修改密码 
taskkill /F /IM MysqLd-nt.exe 
net stop MysqL >nul 
start /b MysqLd-nt --skip-grant-tables 
ping -n 2 127.0.0.1 >nul 
echo use MysqL >c:\config.tmp 
echo update user set password=password("") where user="root";>>C:\config.tmp 
echo flush privileges; >>C:\config.tmp 
echo exit >>C:\config.tmp 

::因为是交互式,所以从文件读取内容 
MysqL <C:\config.tmp 
taskkill /F /IM MysqLd-nt.exe 
net stop MysqL >nul 
net start MysqL 
del C:\config.tmp /F 
pause 
exit 

::删除路径最后一个字符,跳回主程序 
:GETPATH 
set MysqLPath=%MysqLPath:~0,-1% 
goto BACKTOMAIN

如果是用wamp一键安装需要改一些脚本,主要是注册搜索路径改变,返回值改变,服务名改变了,MysqL安装路径变成D:\wamp\bin\MysqL\MysqL5.5.24\bin,少了MysqLd-nt这个东西,进程里面也没有MysqLd-nt.exe

@echo off 

title MysqL 

reg query HKLM\SYstem\ControlSet001\Services\wampMysqLd | find /I "ImagePath">C:\MysqL.txt 

if %errorlevel% neq 0 ( 
echo MysqL not found 
pause 
exit 
) 

FOR /F "tokens=3 delims= " %%i in (C:\MysqL.txt) do set MysqLPath=%%i 
del C:\MysqL.txt /f 
set MysqLPath=%MysqLPath:/=\% 
set MysqLPath=%MysqLPath:~0,-1% 

:BACKTOMAIN 
set character=%MysqLPath:~-1,1% 
if not %character% == \ goto GETPATH 
cd /d "%MysqLPath%" 

::echo %MysqLPath% 

if %errorlevel% neq 0 ( 
echo MysqL not found 
pause 
exit 
) 

taskkill /F /IM MysqLd.exe 
net stop wampMysqLd >nul 
start /b MysqLd -nt --skip-grant-tables 
ping -n 2 127.0.0.1 >nul 
echo use MysqL >c:\config.tmp 
echo update user set password=password("") where user="root";>>C:\config.tmp 
echo flush privileges; >>C:\config.tmp 
echo exit >>C:\config.tmp 

MysqL <C:\config.tmp 

taskkill /F /IM MysqLd.exe 
net stop wampMysqLd >nul 
net start wampMysqLd 
del C:\config.tmp /F 

pause 
exit 

:GETPATH 
set MysqLPath=%MysqLPath:~0,-1% 
goto BACKTOMAIN

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...