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 realize MySQL Command Line backup and recovery by C #

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article is about how C # implements MySQL command line backup and recovery. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

There are many tools available for the backup of MySQL database. In the past two days, we have written a gadget to complete the backup and recovery of MySQL database by calling the mysqldump command of MYSQL with C #.

First, let's talk about how to use the mysqldump command to back up the MySQL database.

Mysqldump-hhostname-uusername-ppassword databasename > backupfile.sql

Compress backup of MySQL database directly

Mysqldump-hhostname-uusername-ppassword databasename | gzip > backupfile.sql.gz

Back up some table (s) in the MySQL database

Mysqldump-hhostname-uusername-ppassword databasename specific_table1 specific_table2 > backupfile.sql

Backup multiple MySQL databases at the same time

Mysqldump-hhostname-uusername-ppassword-databases databasename1 databasename2 databasename3 > multibackupfile.sql

Just back up the database structure

Mysqldump-no-data-databases databasename1 databasename2 databasename3 > structurebackupfile.sql

Back up all databases on the server

Mysqldump-all-databases > allbackupfile.sql

Commands for restoring MySQL databases

Mysql-hhostname-uusername-ppassword databasename < backupfile.sql

Restore a compressed MySQL database

Gunzip < backupfile.sql.gz | mysql-uusername-ppassword databasename

Transfer the database to a new server

Mysqldump-uusername-ppassword databasename | mysql-host=*.*-C databasename

Using C # to operate MYSQL backup and recovery, mainly using C # to execute external programs to achieve

The following is part of the C # source code

/ / back up the database to a specific directory / Bin folder directory to get the mysqldump.exe file / / server / / user name / / password / / the name of the database to be backed up / public static bool BackupDB (string binfolderpath, string server, string character, string user, string pass, string db String backupfile) {string command = string.Format ("mysqldump.exe-- quick-- host=\" {0}\ "- default-character-set=\" {1}\ "--lock-tables-- verbose-- force-- port=3306-- user=\" {2}\ "- password=\" {3}\ "- r\" {5}\ ", server, character.Trim (). ToLower (), user, pass, db, backupfile) StartCmd (binfolderpath + @ "\", command); if (File.Exists (backupfile)) {return true;} else {return false } / restore the specified database to the specified file / Bin folder directory to get the mysqldump.exe file / / server / / user name / / password / / database name to be backed up / SQL file / public static bool RestoreDB to be restored (string binfolderpath, string character, string server, string user, string pass, string db) String restorefile) {string command = string.Format ("mysql.exe-host=\" {0}\ "- default-character-set=\" {1}\ "- port=3306-- user=\" {2}\ "- password=\" {3}\ "\" {4}\ "

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