以下寫的是Windows環境下,不過Linux環境下區別也不大,路徑改下就好
import java.io.*; import java.net.URL; public class DatabaseTool { public static void main(String[] args) { try { if (exportDatabase("127.0.0.1","3306","root","root", "C:\\reyo\\backupDatabase","test-20190516.sql","test") ){ System.out.println("數據庫成功備份"); }else { System.out.println("數據庫備份失敗"); } } catch (InterruptedException e) { e.printStackTrace(); } if (importDatabase("127.0.0.1", "3306", "root", "root", "C:\\reyo\\backupDatabase", "test-20190516.sql", "test")) { System.out.println("數據庫導入成功"); } else { System.out.println("數據庫導入失敗"); } } /** * Mysql數據庫導出 * @param hostIP 數據庫地址 * @param hostPort 端口 * @param userName 用戶名 * @param password 密碼 * @param savePath 導出路徑 * @param fileName 導出文件名 * @param databaseName 要導出數據庫名 * @return * @throws InterruptedException */ public static boolean exportDatabase(String hostIP, String hostPort, String userName, String password, String savePath, String fileName, String databaseName) throws InterruptedException { //目錄不存在則新建 File saveFile = new File(savePath); if (!saveFile.exists()) { saveFile.mkdirs(); } //在地址后補充系統默認分隔符 if (!savePath.endsWith(File.separator)) { savePath = savePath + File.separator; } PrintWriter printWriter = null; BufferedReader bufferedReader = null; try { Runtime runtime = Runtime.getRuntime(); //因為我的地址有空格,為解決找不到路徑所以用了這個方式 URL url = new URL("file:C:\\reyo\\develop software\\mysql-5.6.41-winx64\\bin"); String path = url.getPath(); //"mysqldump -h127.0.0.1 -uroot -P3306 -proot test" String cmd = "\\mysqldump -h" + hostIP + " -u" + userName + " -P" + hostPort + " -p" + password + " " + databaseName; cmd = path + cmd; Process process = runtime.exec(cmd); InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8"); bufferedReader = new BufferedReader(inputStreamReader); printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8")); String line; while ((line = bufferedReader.readLine()) != null) { printWriter.println(line); } printWriter.flush(); if (process.waitFor() == 0) { return true; } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } if (printWriter != null) { printWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } return false; } /** * 導入Mysql數據庫 * @param hostIP 數據庫地址 * @param hostPort 端口 * @param userName 用戶名 * @param password 密碼 * @param importFilePath 數據庫文件路徑 * @param sqlFileName 要導入的文件名 * @param databaseName 要導入的數據庫名 * @return * @throws InterruptedException */ public static boolean importDatabase(String hostIP, String hostPort, String userName, String password, String importFilePath, String sqlFileName, String databaseName) { File imporFile = new File(importFilePath); if (!imporFile.exists()) { imporFile.mkdirs(); } if (!importFilePath.endsWith(File.separator)) { importFilePath = importFilePath + File.separator; } //mysql -h127.0.0.1 -uroot -P3306 -p test<C:\reyo\backupDatabase\ try { Process process = Runtime.getRuntime().exec("cmd /C" + "mysql -h"+hostIP+" -P"+hostPort+" -u"+userName+" -p"+password+" "+databaseName+"<"+importFilePath+sqlFileName); if (process.waitFor()==0){ return true; } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e){ e.printStackTrace(); } return false; } }
不過這種方法有個缺陷就是部署程序的機器要安裝MySQL,如我本地C:\\reyo\\develop software\\mysql-5.6.41-winx64\\bin目錄就是MySQL路徑。
手動備份恢復數據庫也寫一下
備份數據庫時在mysql安裝目錄的bin目錄下執行以下命令即可
mysqldump -h127.0.0.1 -P3306 -uroot -proot test>C:\reyo\backupDatabase\test-20190516.sql
這句的含義是備份地址127.0.0.1端口3306用戶root密碼root數據庫test的數據到C:\reyo\backupDatabase目錄下重命名為test-20190516.sql
恢復數據庫時不需要規定目錄,執行以下命令即可
mysql -h127.0.0.1 -P3306 -uroot -proot test<C:\reyo\backupDatabase\test-20190516.sql
這句的含義是把C:/reyo/backupDatabase\test-20190516.sql文件內容恢復到地址127.0.0.1端口3306用戶root密碼root的數據庫test中
需要注意-proot可能報警告,只寫-p就可以正常運行。
此外修改my.cnf(或my.ini)文件配置
[mysqldump]password=root
[mysql]password=root
也可以去除警告