Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MySQL to realize scheduled backup in Windows environment

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to use MySQL to achieve regular backup in the Windows environment, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

First, write backup scripts

Rem auther:www.yumi-info.comrem date:20171222rem * MySQL backup start*@echo offforfiles / p "E:\ mysql\ MySQL BackUp" / m backup_*.sql-d-30 / c "cmd / c del / f @ path" set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%"E:\ mysql\ MySQL Server 5.6\ bin\ mysqldump" -- opt-- single-transaction=TRUE-- user=root-- password=123456-- host=127.0.0.1-- protocol=tcp-- port=3306-- default-character-set=utf8-- single-transaction=TRUE-- routines-- events "yumi_website" > "E:\ mysql\ MySQL BackUp\ backup_%Ymd%.sql" @ echo onrem * MySQL backup end*

This is a common windows batch script file, and let me briefly explain the key parts of it:

Forfiles / p "E:\ mysql\ MySQL BackUp" / m backup_*.sql-d-30 / c "cmd / c del / f @ path"

Forfiles is used to delete expired backups in the backup directory. "E:\ mysql\ MySQL BackUp" is the path where the backup files are located and can be modified by yourself. "backup_*.sql" refers to all database backup files under this path that begin with "backup_" and are suffixed with ".sql". The following number "30" indicates that it expires in 30 days.

Set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%"

Use the set command to define a variable called "Ymd" whose value is the following long list of rules, simply the current date and time

"E:\ mysql\ MySQL Server 5.6\ bin\ mysqldump"

This command calls the backup tool that comes with MySQL. Note that this path must be written as your own "mysqldump.exe" path, usually in the / bin directory of the MySQL installation path. This line of command is followed by a long list of parameters, and we choose a few important ones to explain:

-- user=root

An account that connects to the MySQL database service, which usually must have permission to back up the database. We use root for simplicity, but it is not recommended to use a root account in the actual production environment to avoid account and password disclosure, thus causing unnecessary trouble.

-- password=123456

This is the password to connect to the MySQL database service

-- host=127.0.0.1

This is the ip address of the server where the database service is located

-- port=3306

This is the port number of the server where the database service is located.

Events "yumi_website" > "E:\ mysql\ MySQL BackUp\ backup_%Ymd%.sql"

The events parameter implements the operation of backing up the database to a specified file. "yumi_website" is the database that needs to be backed up, and the one to the right of the greater than sign ">" is the server directory and file name where our backup file is saved.

Second, set Windows tasks

After completing the above steps, we need to add the Windows scheduled task.

In Windows Server2012, we go to the server administration panel, click "tools" in the upper right menu bar, and select "Task Scheduler":

After opening the task scheduler, we click "create basic Task" on the right:

Then we need to fill in the name of the task, as well as the description:

After clicking the next step, we need to set the frequency of the task. I chose "every day":

Click "next" again to set the time for the execution of the task. I chose 1 point in the dead of night:

In "next", we select "start the program":

In the following dialog box, we need to select the batch file we just wrote:

After completing these steps, windows will show us an overview of the entire task:

Once you are sure, click "finish". At this point, we will see that a new task has been added to the Windows task list:

The above is how to use MySQL to achieve regular backup in the Windows environment. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report