Home Database Mysql Tutorial MySQL 备份还原数据库批处理

MySQL 备份还原数据库批处理

Jun 07, 2016 pm 06:01 PM
mysql database

在日常工作中,我们往往需要对MySQL数据库进行备份,有时可以通过拷贝整个文件,有时通过备份整个库还有的时候我们就只需要备份某个表。

1、备份数据库(单个表备份) bk_table.bat
代码如下:
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database1 table1>table1.sql
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database2 table2>table2.sql
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database2 table3>table3.sql
pause

2、还原数据库(单个表还原) restore.bat
代码如下:
mysql -h127.0.0.1 -P3306 -uroot -proot database1 mysql -h127.0.0.1 -P3306 -uroot -proot database2 mysql -h127.0.0.1 -P3306 -uroot -proot database2 pause

3、备份数据库并自动打包压缩成.rar文件(单个表备份) bk_table.bat
代码如下:
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database1 table1>table1.sql
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database2 table2>table2.sql
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database2 table3>table3.sql
winrar a Db_Bak_%date:~0,10%.rar D:\DB_Backup\*.sql
DEL D:\DB_Backup\*.sql
pause

执行后,你的备份数据脚本就会被压缩到你的电脑D:\DB_Backup 的 Db_Bak_日期.rar文件中。
如果出现“'WinRAR' 不是内部或外部命令,也不是可运行的程序或文件。” 错误,在计算机->属性->高级->环境变量->在Path项
添加你的WinRAR路径,如:C:\Program Files\WinRAR 即可。
4、备份整个库的数据
代码如下:
mysqldump -h127.0.0.1 -P3306 -uroot -proot database1 > database1.sql
Pause

说明:
1、打开记事本->把以上脚本拷贝到记事本->另存为*.bat文件即可双击打开执行。
2、需要将你的MySql安装bin目录路径添加到计算机环境变量的Path里:
计算机->属性->高级->环境变量->在Path项添加 你的Mysql安装bin目录路径 如:C:\Program Files\MySQL\MySQL Server 5.0\bin
或者将文件拷贝到bin目录下C:\Program Files\MySQL\MySQL Server 5.0\bin执行。
以上都已经过测试,O(∩_∩)O~

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

Detailed tutorial on establishing a database connection using MySQLi in PHP

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

How to create a MySQL table using PHP?

See all articles