批處理指令碼一鍵重設mysql的root密碼

來源:互聯網
上載者:User

標籤:style   http   檔案   art   for   re   

一鍵重設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
) www.jbxue.com

::禁用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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.