Reset mysql's root password script with one key

  • 2020-06-23 02:07:37
  • OfStack


 @echo off 

title mysql 

:: From the registry Mysql Write the installation path to the file 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 
) 

:: Take "as delimiter, intercept the first 2 Section contents are saved to variables mysqlPath 
FOR /F tokens^=2^ delims^=^" %%i in (C:\mysql.txt) do set mysqlPath=%%i 
del C:\mysql.txt /f 

:: In the path / Replace with \ 
set mysqlPath=%mysqlPath:/=\% 

:: Delete path last 1 Six characters (the character is not visible, it may be carriage return line feed, etc.)  
set mysqlPath=%mysqlPath:~0,-1% 

:BACKTOMAIN 

:: Get to the end of the path 1 Three characters does not equal \ 
set character=%mysqlPath:~-1,1% 

:: If the last 1 Three characters does not equal \ , then jump to GETPATH delete mysqlPath At the end of the 1 A character  
if not %character% == \ goto GETPATH 

:: Enter the mysql The installation path C:\Program Files\MySQL\MySQL Server 5.0\bin 
cd /d "%mysqlPath%" 

::echo %mysqlPath% 

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

:: disable mysql Service to skip permission authentication and change the password  
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 

:: Because it is interactive, the content is read from the file  
mysql <C:\config.tmp 
taskkill /F /IM mysqld-nt.exe 
net stop mysql >nul 
net start mysql 
del C:\config.tmp /F 
pause 
exit 

:: Delete path last 1 Six characters, jump back to the main program  
:GETPATH 
set mysqlPath=%mysqlPath:~0,-1% 
goto BACKTOMAIN

If installing with the wamp1 key, you need to change 1 of the scripts, mainly registry search path changed, return value changed, service name changed, mysql installation path changed to D:\wamp\ mysql\ mysql5.5.24 \bin, no mysqld-ES11en, no mysqld-ES13en.exe in the process


@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

Related articles: