MySQL 通過bat命令備份windows下的mysql資料庫,batmysql

來源:互聯網
上載者:User

MySQL 通過bat命令備份windows下的mysql資料庫,batmysql

前言:

朋友說在windows下面用bat命令備份失敗,他一時找不到問題所在,於是找我幫忙查看下。其實我也沒有用過bat寫指令碼,不過臨時抱佛腳,bat指令碼總不會比shell還難吧。

 

1,找回丟失的root密碼

我電腦上倒是原來安裝了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,開始調試

2.1 貼下朋友發的運行不成功的指令碼

@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

 

 

2.2 開始準備測試的庫以及資料

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');

 

 

 

2.3 我的調試過程

簡單化調試,

(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>

                  

 

 

 

3,貼下我最終修改過的代碼

@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

 

 

4,總結

bat指令碼中,對於截取日期組建檔案目錄已經檔案名稱的情況,要特別注意編碼格式,否則會形成尾部多空格的情況,比如hoomsun_credit_20150827231815 .sql,遇到這種,就會報錯的。所以大家在網上copy別人指令碼的時候,要注意這些格式問題免得出錯。

 

 

 

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

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.