一键重置mysql的root密码脚本

时间:2021-08-22 10:07:59
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@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