@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
@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
修改密碼 mysql安裝路徑下的bin目錄下執行
./mysqladmin –u root password 123
其中123是設置的秘密
登錄測試
mysql -u root –p123
出現mysql>
mysql>show databases; 查看數據庫
mysql>quit退出
1. KILL掉系統裡的MySQL(和PHP搭配之最佳組合)進程;
2. 用以下命令啟動MySQL(和PHP搭配之最佳組合)源碼天空
,以不檢查權限的方式啟動;
MySQL(和PHP搭配之最佳組合)d_safe -skip-grant-tables &
3. 然後用空密碼方式使用root用戶登錄 MySQL(和PHP搭配之最佳組合);
MySQL(和PHP搭配之最佳組合) -u root
4. 修改root用戶的密碼;
MySQL(和PHP搭配之最佳組合) update MySQL(和PHP搭配之最佳組合).user set password=PASSWORD('新密碼') where User='root';MySQL(和PHP搭配之最佳組合) flush privileges;MySQL(和PHP搭配之最佳組合) quit
5. 重新啟動MySQL(和PHP搭配之最佳組合),就可以使用新密碼登錄了。