Batch script resets the root password for MySQL (reset password is 123456)

Source: Internet
Author: User

@echo off

Title MySQL

:: The installation path for MySQL found from the registry is written 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 second paragraph to save to variable Mysqlpath
for/f tokens^=2^ delims^=^ "%%i in (C:\mysql.txt) do set mysqlpath=%%i
Del c:\mysql.txt/f

:: In path/replace with \
Set mysqlpath=%mysqlpath:/=\%

:: Delete the last character of the path (the character is not visible, it may be a carriage return, etc.)
Set mysqlpath=%mysqlpath:~0,-1%

: Backtomain

:: Gets the last character of the path to see not equal to \
Set character=%mysqlpath:~-1,1%

:: If the last character is not equal to \, then jump to GetPath to delete the last character of Mysqlpath
If not%character% = = \ Goto GetPath

:: Enter MySQL 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
) www.jbxue.com

:: Disable MySQL service, skip permission verification, change 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

:: Read content from a file because it is interactive
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 character, jump back to main program
: GetPath
Set mysqlpath=%mysqlpath:~0,-1%
Goto Backtomain
If you are using Wamp one-click installation needs to change some scripts, mainly is the registry search path changes, return value changes, service name changed, MySQL installation path into D:\wamp\bin\mysql\mysql5.5.24\bin, less mysqld-nt this thing, There's no mysqld-nt.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 ("123456") 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

Batch script resets the root password for MySQL (reset password is 123456)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.