首页 > 编程 > BAT > 正文

一键重置mysql的root密码的实现脚本

2020-07-26 20:13:54
字体:
来源:转载
供稿:网友

重置mysql的root密码方法有很多,本文介绍的这个是使用批处理来完成的

@echo offtitle mysql::从注册表找到Mysql的安装路径写入文件mysql.txtreg query HKLM/SYSTEM/ControlSet001/Services/MySQL | find /I "ImagePath">C:/mysql.txtif %errorlevel% neq 0 (echo MySQL not foundpauseexit)::以”为分隔符,截取第二段内容保存到变量mysqlPathFOR /F tokens^=2^ delims^=^" %%i in (C:/mysql.txt) do set mysqlPath=%%idel 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/bincd /d "%mysqlPath%"::echo %mysqlPath%if %errorlevel% neq 0 (echo MySQL not foundpauseexit) www.VeVB.COm::禁用mysql服务,跳过权限验证修改密码taskkill /F /IM mysqld-nt.exenet stop mysql >nulstart /b mysqld-nt --skip-grant-tablesping -n 2 127.0.0.1 >nulecho use mysql >c:/config.tmpecho update user set password=password("") where user="root";>>C:/config.tmpecho flush privileges; >>C:/config.tmpecho exit >>C:/config.tmp::因为是交互式,所以从文件读取内容mysql <C:/config.tmptaskkill /F /IM mysqld-nt.exenet stop mysql >nulnet start mysqldel C:/config.tmp /Fpauseexit::删除路径最后一个字符,跳回主程序:GETPATHset mysqlPath=%mysqlPath:~0,-1%goto BACKTOMAIN如果是用wamp一键安装需要改一些脚本,主要是注册表搜索路径改变,返回值改变,服务名改变了,mysql安装路径变成D:/wamp/bin/mysql/mysql5.5.24/bin,少了mysqld-nt这个东西,进程里面也没有mysqld-nt.exe@echo offtitle mysqlreg query HKLM/SYSTEM/ControlSet001/Services/wampmysqld | find /I "ImagePath">C:/mysql.txtif %errorlevel% neq 0 (echo MySQL not foundpauseexit)FOR /F "tokens=3 delims= " %%i in (C:/mysql.txt) do set mysqlPath=%%idel C:/mysql.txt /fset mysqlPath=%mysqlPath:/=/%set mysqlPath=%mysqlPath:~0,-1%:BACKTOMAINset character=%mysqlPath:~-1,1%if not %character% == / goto GETPATHcd /d "%mysqlPath%"::echo %mysqlPath%if %errorlevel% neq 0 (echo MySQL not foundpauseexit)taskkill /F /IM mysqld.exenet stop wampmysqld >nulstart /b mysqld -nt --skip-grant-tablesping -n 2 127.0.0.1 >nulecho use mysql >c:/config.tmpecho update user set password=password("") where user="root";>>C:/config.tmpecho flush privileges; >>C:/config.tmpecho exit >>C:/config.tmpmysql <C:/config.tmptaskkill /F /IM mysqld.exenet stop wampmysqld >nulnet start wampmysqlddel C:/config.tmp /Fpauseexit:GETPATHset mysqlPath=%mysqlPath:~0,-1%goto BACKTOMAIN
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表