前言:
朋友說在windows下面用bat命令備份失敗,他一時找不到問題所在,於是找我幫忙查看下。其實我也沒有用過bat寫腳本,不過臨時抱佛腳,bat腳本總不會比shell還難吧。
我電腦上倒是原來安裝了mysql,但是由於習慣用linux下的mysql,windows的這個長久不使用,連root密碼都不知道了,還得需要找回root密碼,不然沒有登錄做測試
1.1、 首先檢查mysql服務是否啟動,若已啟動則先將其停止服務,可在開始菜單的運行,使用命令:
net stop mysql
打開第一個cmd1窗口,切換到mysql的bin目錄,運行命令:
mysqld --defaults-file="C:\ProgramFiles\MySQL\MySQL Server 5.1\my.ini" --console --skip-grant-tables
注釋:
該命令通過跳過權限安全檢查,開啟mysql服務,這樣連接mysql時,可以不用輸入用戶密碼。 此時已經開啟了mysql服務了!
這個窗口保留不關閉。
1.2、打開第二個cmd2窗口,連接mysql:
輸入命令:
mysql -u root -p
出現:
Enter password:
在這裡直接回車,不用輸入密碼。
然後就就會出現登錄成功的信息,
使用命令:
show databases;
使用命令切換到mysql數據庫:
use mysql;
使用命令更改root密碼:
UPDATE user SET Password=PASSWORD('root')where USER='root';
刷新權限:
FLUSH PRIVILEGES;
然後退出,重新登錄:
quit
重新登錄:可以關掉之前的cmd1 窗口了。然後用net start mysql 啟動服務
mysql -u root -p
出現輸入密碼提示,輸入新的密碼即可登錄:
Enter password: ***********
顯示登錄信息:成功 就可以了。
PS:原blog地址為:http://blog.csdn.net/mchdba/article/details/48039035,謝絕轉載
2,開始調試
@echo off & setlocal ENABLEEXTENSIONS
set BACKUP_PATH=D:\Backup\
set DATABASES=hoomsun_credit
set USERNAME=root
set PASSWORD=root
set MYSQL=D:\mysql-5.6.21-winx64\bin
set WINRAR=F:\winrar\Rar.exe
set YEAR=%date:~0,4%
set MONTH=%date:~5,2%
set DAY=%date:~8,2%
set HOUR=%time:~0,2%
set MINUTE=%time:~3,2%
set SECOND=%time:~6,2%
set DIR=%BACKUP_PATH%%YEAR%\%MONTH%\%DAY%\
setADDON=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND%
:: create dir
if not exist %DIR% (
mkdir %DIR% 2>nul
)
if not exist %DIR% (
echo Backup path: %DIR% not exists, createdir failed.
goto exit
)
cd /d %DIR%
:: backup
echo Start dump databases...
for %%D in (%DATABASES%) do (
echo Dumping database %%D ...
%MYSQL%mysqldump -u%USERNAME% -p%PASSWORD%%%D > %%D.%ADDON%.sql 2>nul
:: winrar
if exist %WINRAR% (
%WINRAR% a -k -r -s -m1 -ep1 %%D.%ADDON%.rar%%D.%ADDON%.sql 2>nul
del /F /S /Q %%D.%ADDON%.sql 2>nul
)
)
echo Done
:exit
create database hoomsun_credit;
CREATE TABLE t (
idint(1) NOT NULL DEFAULT '0',
name varchar(1) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO t VALUES (1,'a');
簡單化調試,
(1)去掉了for循環,因為一個庫成功了,那麼N個庫也會相應成功了。
(2)文件名失效導致mysqldump報錯
D:\mysql-5.6.21-winx64\bin>D:\vm\backup.bat
20150827230401
Startdump databases...
Dumpingdatabase hoomsun_credit ...
Warning:Using a password on the command line interface can be insecure.
mysqldump:Couldn't find table: "04"
Dumpingdatabase manonggu ...
查到原因是生成了hoomsun_credit_20150827231815.sql的文件名,這樣是無效的,如下顯示
D:\mysql-5.6.21-winx64\bin>D:\vm\backup.bat
20150827231815.sql
hoomsun_credit_20150827231815.sql
Startdump databases...
Dumpingdatabase %D ...
Warning:Using a password on the command line interface can be insecure.
mysqldump:Couldn't find table: ".sql"
Done
D:\mysql-5.6.21-winx64\bin>D:\vm\backup.bat
這樣我就分析出了是由於文件名變量出錯,肯定是格式問題,於是我刪除了原來的關於文件名的set ADDON=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND% 這一行代碼,我自己手動再敲一遍,然後運行正常如下:
D:\mysql-5.6.21-winx64\bin>D:\vm\backup.bat
20150827231922.sql
hoomsun_credit_20150827231922.sql
Startdump databases...
Dumpingdatabase %D ...
Warning:Using a password on the command line interface can be insecure.
Done
D:\mysql-5.6.21-winx64\bin>
@echo off & setlocal ENABLEEXTENSIONS
set BACKUP_PATH=D:\Backup\
set DATABASES=hoomsun_credit
set USERNAME=root
set PASSWORD=root
set MYSQL=D:\mysql-5.6.21-winx64\bin
set h=%time:~0,2%
set h=%h: =0%
setbak_filename=%date:~0,4%%date:~5,2%%date:~8,2%%h%%time:~3,2%%time:~6,2%.sql
echo %bak_filename%
set YEAR=%date:~0,4%
set MONTH=%date:~5,2%
set DAY=%date:~8,2%
set HOUR=%time:~0,2%
set MINUTE=%time:~3,2%
set SECOND=%time:~6,2%
set DIR=%BACKUP_PATH%%YEAR%\%MONTH%\%DAY%\
setADDON=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND%
set BACKUP_FILE=%DATABASES%_%ADDON%.sql
echo %BACKUP_FILE%
:: create dir
if not exist %DIR% (
echo %DIR%
mkdir %DIR% 2>nul
)
if not exist %DIR% (
echo Backup path: %DIR% not exists, createdir failed.
goto exit
)
cd /d %DIR%
:: backup
echo Start dump databases...
::for %%D in (%DATABASES%) do (
echo Dumping database %%D ...
::%MYSQL%\mysqldump -u%USERNAME%-p%PASSWORD% >%BACKUP_FILE%
%MYSQL%\mysqldump.exe -u%USERNAME%-p%PASSWORD% %DATABASES% > %BACKUP_FILE%
::)
echo Done
:exit
bat腳本中,對於截取日期生成文件目錄已經文件名的情況,要特別注意編碼格式,否則會形成尾部多空格的情況,比如hoomsun_credit_20150827231815 .sql,遇到這種,就會報錯的。所以大家在網上copy別人腳本的時候,要注意這些格式問題免得出錯。