MySQL uses the bat command to back up mysql database in windows _ MySQL

Source: Internet
Author: User
My friend said that the backup failed with the bat command under windows. he could not find the problem at the moment, so he asked me for help. In fact, I have never used bat to write scripts, but the bat script is never more difficult than shell. 1. retrieve the lost root password. Preface:

My friend said that the backup failed with the bat command under windows. he could not find the problem at the moment, so he asked me for help. In fact, I have never used bat to write scripts, but the bat script is never more difficult than shell.

1. retrieve the lost root password

I have installed mysql on my computer, but I am used to using mysql in linux. I am not using windows for a long time, and I don't even know the root password. I have to retrieve the root password, otherwise, you have not logged on to the test.

1.1 First, check whether the mysql service is started. if it has been started, stop the service. run the following command in the Start menu:

Net stop mysql

Open the first cmd1 window, switch to the mysql bin directory, and run the following command:

Mysqld -- defaults-file = "C: \ ProgramFiles \ MySQL Server 5.1 \ my. ini" -- console -- skip-grant-tables

Note:

This command skips the permission security check and enables the mysql service. when you connect to mysql, you do not need to enter the user password. The mysql service has been enabled!

This window is retained.

1.2 open the second cmd2 window and connect to mysql:

Enter the following command:

Mysql-u root-p

Appears:

Enter password:

Press enter here without entering the password.

Then there will be information about successful logon,

Run the following command:

Show databases;

Run the following command to switch to the mysql database:

Use mysql;

Run the following command to change the root password:

UPDATE user SET Password = PASSWORD ('root') where USER = 'root ';

Refresh permission:

Flush privileges;

Then exit and log on again:

Quit

Log on again: you can close the previous cmd1 window. Then start the service with net start mysql.

Mysql-u root-p

Enter a new password to log on:

Enter password :***********

Display logon information: the logon is successful.

PS: The original blog address is http://blog.csdn.net/mchdba/article/details/48039035. thank you for your reprinting.

2. start debugging.

2.1 paste a script from a friend that fails to run.

@ 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 too many d.w.addon#.rar % D. % ADDON %. SQL 2> nul

Del/F/S/Q % D. % ADDON %. SQL 2> nul

)

)

Echo Done

: Exit

2.2 prepare the test database and data

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 My debugging process

Simple debugging,

(1) remove the for loop. because a database is successful, N databases are also successful.

(2) mysqldump error caused by invalid file name

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

The reason is that the file name of hoomsun_credit_20150827231815. SQL is generated, which is invalid, as shown below:

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

In this way, I found that the file name variable is incorrect, and it must be a format problem, so I deleted the original set ADDON = % YEAR % MONTH % DAY % HOUR % MINUTE % SECOND % code about the file name, I manually repeat it, and then run normally as follows:

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. paste the code that I finally modified

@ 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. Summary

In the bat script, pay special attention to the encoding format when the file directory generated by the truncation date has a file name. otherwise, multiple spaces at the end will be formed, such as hoomsun_credit_20150827231815. SQL. if this happens, an error is reported. Therefore, when copying other scripts on the Internet, pay attention to these format issues to avoid errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.