當前位置:首頁 » 操作系統 » java備份mysql資料庫

java備份mysql資料庫

發布時間: 2022-05-01 01:42:17

java 備份mysql資料庫

備份MySQL資料庫的方法:
import java.io.File;
import java.io.IOException;

/**
* MySQL資料庫備份
*
* @author GaoHuanjie
*/
public class MySQLDatabaseBackup {

/**
* Java代碼實現MySQL資料庫導出
*
* @author GaoHuanjie
* @param hostIP MySQL資料庫所在伺服器地址IP
* @param userName 進入資料庫所需要的用戶名
* @param password 進入資料庫所需要的密碼
* @param savePath 資料庫導出文件保存路徑
* @param fileName 資料庫導出文件文件名
* @param databaseName 要導出的資料庫名
* @return 返回true表示導出成功,否則返回false。
*/
public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目錄不存在
saveFile.mkdirs();// 創建文件夾
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("mysqlmp").append(" --opt").append(" -h").append(hostIP);
stringBuilder.append(" --user=").append(userName) .append(" --password=").append(password).append(" --lock-all-tables=true");
stringBuilder.append(" --result-file=").append(savePath + fileName).append(" --default-character-set=utf8 ").append(databaseName);
try {
Process process = Runtime.getRuntime().exec(stringBuilder.toString());
if (process.waitFor() == 0) {// 0 表示線程正常終止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}

public static void main(String[] args) throws InterruptedException {
if (exportDatabaseTool("172.16.0.127", "root", "123456", "D:/backupDatabase", "2014-10-14.sql", "test")) {
System.out.println("資料庫備份成功!!!");
} else {
System.out.println("資料庫備份失敗!!!");
}
}
}

⑵ 求一個能實現mysql數據備份的程序,要Java代碼

備份資料庫:(將資料庫test備份)
mysqlmp
-u
root
-p
test>c:\test.txt
將備份數據導入到資料庫:(導回test資料庫)
mysql
-u
root
-p
test<c:\test.txt

⑶ java mysql備份資料庫數據問題

想得到所有記錄及表的sql語句?

將框裡面那句刪除了試試

插不上圖

System.out.println(result+"--");

這句刪除掉試試

⑷ 怎麼用java備份mysql資料庫

首先,設置mysql的環境變數(在path中添加%MYSQL_HOME%\bin),重啟電腦。
完整代碼:
備份:

public static void main(String[] args) {

backup();
load();
}

public static void backup() {
try {
Runtime rt = Runtime.getRuntime();

// 調用 mysql 的 cmd:
Process child = rt
.exec("mysqlmp -u root --set-charset=utf8 bjse act_obj");// 設置導出編碼為utf8。這里必須是utf8

// 把進程執行中的控制台輸出信息寫入.sql文件,即生成了備份文件。註:如果不對控制台信息進行讀出,則會導致進程堵塞無法運行
InputStream in = child.getInputStream();// 控制台的輸出信息作為輸入流

InputStreamReader xx = new InputStreamReader(in, "utf8");// 設置輸出流編碼為utf8。這里必須是utf8,否則從流中讀入的是亂碼

String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
// 組合控制台輸出信息字元串
BufferedReader br = new BufferedReader(xx);
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();

// 要用來做導入用的sql目標文件:
FileOutputStream fout = new FileOutputStream(
"e:/mysql-5.0.27-win32/bin/bjse22.sql");
OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
writer.write(outStr);
// 註:這里如果用緩沖方式寫入文件的話,會導致中文亂碼,用flush()方法則可以避免
writer.flush();

// 別忘記關閉輸入輸出流
in.close();
xx.close();
br.close();
writer.close();
fout.close();

System.out.println("");

} catch (Exception e) {
e.printStackTrace();
}

}

public static void load() {
try {
String fPath = "e:/mysql-5.0.27-win32/bin/bjse22.sql";
Runtime rt = Runtime.getRuntime();

// 調用 mysql 的 cmd:
Process child = rt.exec("mysql -u root bjse ");
OutputStream out = child.getOutputStream();//控制台的輸入信息作為輸出流
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fPath), "utf8"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();

OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
// 註:這里如果用緩沖方式寫入文件的話,會導致中文亂碼,用flush()方法則可以避免
writer.flush();
// 別忘記關閉輸入輸出流
out.close();
br.close();
writer.close();

System.out.println("");

} catch (Exception e) {
e.printStackTrace();
}

}

備份語句:
mysql> SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',
' from db_testtemp where std_state='1';
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',
' from db_testtemp ;
Query OK, 2 rows affected (0.00 sec)

只生成一個只有數據的.txt:SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',' lines terminated by '\r\n' from db_testtemp ;

只生成一個只有數據的.txt:mysqlmp -uroot -pncae2010 -w "std_state='1'" -T D:\data --no-create-info --fields-terminated-by=, exam db_testtemp

生成一個創建資料庫語句的.sql,一個只有數據的.txt:mysqlmp -uroot -pncae2010 -w "std_state='1'" -T D:\data --fields-terminated-by=, exam db_testtemp

只生成insert語句:mysqlmp -uroot -pncae2010 -w "std_state='1'" -t exam db_testtemp > D:\data\a.sql

⑸ java 備份mysql資料庫的部分數據

假設;你要備份的數據條件是時間,只備份當天的數據。備份區:資料庫。

  1. 匹配當天的數據讀出來。

  2. 在將數據添加到備份區的資料庫。

備份區表的創建問題:1.你可以事先手動建好。

2.也可以通過程序自動建表有兩步;

a).判斷當前備份數據,在備份區是否有對應的表。(有直接添加)

b).如果沒有,拷貝當前備份數據的表結構,在備份區生成。

⑹ java備份mysql數據問題, 無法找到庫,錯誤信息如下

樓主可以直接使用mysql的導出導入命令來進行資料庫的備份和還原


將MySQL添加到環境變數中,然後在cmd命令行中執行[將C:ProgramFilesMySQLMySQL Server 5.5in加到系統變數的path中或到C:ProgramFilesMySQLMySQL Server 5.5in文件夾中去執行命令]。


導出數據:

mysqlmp-h127.0.0.1-uroot-p123456--add-drop-table-Rald>e:	estdb.sql

127.0.0.1是ip地址,root是用戶,123456是密碼,ald是資料庫名稱,e: estdb.sql導出文件路徑


導入數據:

mysql-h127.0.0.1-uroot-p123456--default-character-set=utf8ald<e:	estdb.sql

⑺ javaweb如何備份資料庫

類JavaMysql備份還原資料庫

importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Properties;

publicclassJavaMysql{
/*
*備份資料庫1、讀取配置文件2、啟動智能查詢Mysql安裝目錄3、備份資料庫為sql文件
*/
publicstaticvoidbackup(Stringsql){

Propertiespros=getPprVue("prop.properties");
Stringusername=pros.getProperty("username");
Stringpassword=pros.getProperty("password");

CheckSoftwarec=null;
try{
System.out.println("MySQL服務安裝地址:"+c.check().toString());
}catch(Exceptione2){
e2.printStackTrace();
}
Stringmysqlpaths;
try{
mysqlpaths=c.check().toString()+"bin"+"\";

StringdatabaseName=pros.getProperty("databaseName");
Stringaddress=pros.getProperty("address");
Stringsqlpath=pros.getProperty("sql");
Filebackupath=newFile(sqlpath);
if(!backupath.exists()){
backupath.mkdir();
}

StringBuffersb=newStringBuffer();

sb.append(mysqlpaths);
sb.append("mysqlmp");
sb.append("--opt");
sb.append("-h");
sb.append(address);
sb.append("");
sb.append("--user=");
sb.append(username);
sb.append("");
sb.append("--password=");
sb.append(password);
sb.append("");
sb.append("--lock-all-tables=true");
sb.append("--result-file=");
sb.append(sqlpath);
sb.append(sql);
sb.append("");
sb.append("--default-character-set=utf8");
sb.append(databaseName);
System.out.println("cmd指令:"+sb.toString());
Runtimecmd=Runtime.getRuntime();
try{
Processp=cmd.exec(sb.toString());
}catch(IOExceptione){
e.printStackTrace();
}
}catch(Exceptione1){
e1.printStackTrace();
}
}

/*
*讀取屬性文件
*/
(StringproperName){

InputStreaminputStream=JavaMysql.class.getClassLoader()
.getResourceAsStream(properName);
Propertiesp=newProperties();

try{
p.load(inputStream);
inputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}

returnp;

}

/*
*根據備份文件恢復資料庫
*/
publicstaticvoidload(Stringfilename){
Propertiespros=getPprVue("prop.properties");
Stringroot=pros.getProperty("jdbc.username");
Stringpass=pros.getProperty("jdbc.password");
Stringmysqlpaths=c.check().toString()+"bin"+"\";
Stringsqlpath=pros.getProperty("sql");
Stringfilepath=mysqlpaths+sqlpath+filename;//備份的路徑地址

Stringstmt1=mysqlpaths+"mysqladmin-u"+root+"-p"+pass
+"createfinacing";//-p後面加的是你的密碼
Stringstmt2=mysqlpaths+"mysql-u"+root+"-p"+pass
+"finacing<"+filepath;
String[]cmd={"cmd","/c",stmt2};
try{
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("數據已從"+filepath+"導入到資料庫中");
}catch(IOExceptione){
e.printStackTrace();
}

}

/*
*Test測試
*/
publicstaticvoidmain(String[]args)throwsIOException{
backup("2221.sql");
}
}

⑻ java用MySql 單擊備份按鈕實現備份該怎麼寫啊!

首先設置mysql環境變數

package com.vote.utils;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BackupMysql {

public static void main(String[] args) {
//new Backup().start();
//new Recovery().start();
}

static class Backup extends Thread {
public void run() {
String user = "root"; // Database Account
String password = "root"; // Login Password
String database = "資料庫"; // Need to back up the Database name
String filepath = "d:\\資料庫bak\\" + getFileName(database,".bak"); // Address of the backup path
File savefile = new File("d:\\資料庫bak\\", "aa.txt");
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
String command = "mysqlmp " + database + " -h localhost " + " -u " + user + " -p" + password + " --default-character-set=utf8 --result-file=" + filepath;
try {
Runtime.getRuntime().exec(command);
System.out.println("Successful data backup of " + database);
}catch(IOException e) {
e.printStackTrace();
}
}
private String getFileName(String soure,String suffix) {
String fileName = null;
DateFormat df = new SimpleDateFormat(".yyyy.MM.dd.hh.mm.ss.");
String dfs = df.format(new Date());
String rans = ((int)(Math.random() * 100) + 1) + "";
int rlen = rans.length();
if(rlen == 1){
rans = "0" + rans;
}
fileName = soure + dfs + Integer.toHexString(Integer.parseInt(rans)) + suffix;
return fileName;
}
}

static class Recovery extends Thread {
public void run() {
String user = "root"; // Database Account
String password = "root"; // Login Password
String database = "資料庫"; // Need to back up the Database name
String filepath = "d:\\資料庫bak\\資料庫.2011.10.27.12.56.04.50.bak"; // Address of the recovery path
String command = " -h localhost " + " -u " + user + " -p" + password;
String cmd[] = {"cmd","/c","mysql " + command + " " + database + " < " + filepath};
try {
Runtime.getRuntime().exec("mysqladmin " + command + " create " + database);
Runtime.getRuntime().exec(cmd);
}catch(IOException e) {
e.printStackTrace();
}
}
}

}

⑼ 如何使用java程序備份和恢復MySql資料庫

java用開源的ssh jar包連接到b伺服器執行備份/恢復命令,同樣通過命令也可以獲取到備份的文件信息,恢復資料庫也是一樣的,通過命令把文件傳輸到b伺服器,通過命令進行還原

⑽ 如何用Java實現MySQL資料庫的備份和恢復

註:要將mysql的bin目錄加入到環境變數Path中
將MySql中的資料庫導出到文件中 備份
import java.io.*;
import java.lang.*;
public class BeiFen {
public static void main(String[] args) {
// 資料庫導出
String user = "root"; // 資料庫帳號
String password = "root"; // 登陸密碼
String database = "test"; // 需要備份的資料庫名
String filepath = "e:\\test.sql"; // 備份的路徑地址
String stmt1 = "mysqlmp " + database + " -u " + user + " -p"
+ password + " --result-file=" + filepath;
/*
* String mysql="mysqlmp test -u root -proot
* --result-file=d:\\test.sql";
*/
try {
Runtime.getRuntime().exec(stmt1);
System.out.println("數據已導出到文件" + filepath + "中");
}
catch (IOException e) {
e.printStackTrace();
}
}
}

將數據從磁碟上的文本文件還原到MySql中的資料庫
import java.io.*;
import java.lang.*;

/*
* 還原MySql資料庫
* */
public class Recover {

public static void main(String[] args) {

String filepath = "d:\\test.sql"; // 備份的路徑地址
//新建資料庫test
String stmt1 = "mysqladmin -u root -proot create test";
String stmt2 = "mysql -u root -proot test < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };

try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("數據已從 " + filepath + " 導入到資料庫中");
} catch (IOException e) {
e.printStackTrace();
}
}
}

熱點內容
C事件編程 發布:2024-10-05 15:15:43 瀏覽:638
一台伺服器出現兩IP 發布:2024-10-05 15:10:05 瀏覽:924
md5加密演算法c 發布:2024-10-05 15:05:40 瀏覽:760
如何重設控制器密碼 發布:2024-10-05 14:19:13 瀏覽:439
安卓如何遠程簽到 發布:2024-10-05 14:11:11 瀏覽:301
阿里雲伺服器控制面板 發布:2024-10-05 13:57:48 瀏覽:819
涉法涉訴信訪問題意見 發布:2024-10-05 13:56:23 瀏覽:895
華為路由器配置導出的方法有哪些 發布:2024-10-05 13:55:36 瀏覽:163
我的世界好玩伺服器拍視頻 發布:2024-10-05 13:23:19 瀏覽:555
穿越火線掛機腳本 發布:2024-10-05 13:05:44 瀏覽:37