mysql数据备份的方式:
- 直接拷贝物理文件
- 可视化工具中手动导出
- 使用命令行导出 mysqldump命令行使用
通过JAVA中Runtime.getRuntime().exec()的方法调用dos命令行进行导出
windwos下:
Runtime.getRuntime().exec("cmd /c"+"mysqldump -h 主机地址 -u 用户名 -p 用户密码 数据库名 表名 > 导出地址");
linux下:
Runtime.getRuntime().exec("/bin/sh -c"+"mysqldump -h 主机地址 -u 用户名 -p 用户密码 数据库名 表名 > 导出地址");
具体实现代码如下:
/**
* mysql数据库备份与恢复功能
* @date 2022/2/9 13:39
*/
public class Database {
/**
* 备份mysql数据库
* @param username 数据库账号
* @param pwd 数据库密码
* @param hostIp 主机地址
* @param savePath 路径
* @param dbName 数据库名
* @param tableName 表名
* @throws Exception
*/
public static void dbBackUpMysql(String username,String pwd,String hostIp,String dbName,String tableName,String savePath) throws Exception {
//mysqldump -h 127.0.0.1 -u root -proot mysql user >D:/test/test.sql
dbName += " "+tableName;
String pathSql = savePath+tableName+backTime+".sql";
File fileSql = new File(pathSql);
File filePath = new File(savePath);
//创建备份sql文件
if (!filePath.exists()){
filePath.mkdirs();
}
if (!fileSql.exists()){
fileSql.createNewFile();
}
StringBuffer sb = new StringBuffer();
sb.append("mysqldump");
sb.append(" -h"+hostIp);
sb.append(" -u"+username);
sb.append(" -p"+pwd);
sb.append(" "+dbName+" >");
sb.append(pathSql);
Process process = null;
//判断操作系统windwos与linux使用不同进入dos命令
if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
process = Runtime.getRuntime().exec("cmd /c"+sb.toString());
}else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
process = Runtime.getRuntime().exec("/bin/sh -c"+sb.toString());
}else{
throw new Exception("暂不支持该操作系统,进行数据库备份或还原!");
}
//设置超时一分钟
process.waitFor(60000, TimeUnit.MILLISECONDS);
//输出返回的错误信息
StringBuffer mes = new StringBuffer();
String tmp = "";
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((tmp = error.readLine()) != null){
mes.append(tmp + "\n");
}
if(mes != null || !"".equals(mes) ){
System.out.println("备份成功!==>"+mes.toString());
}
error.close();
}
/**
* 数据库还原
* @param username 数据库账号
* @param pwd 数据库密码
* @param hostIp 主机地址
* @param savePath 路径
* @param dbName 数据库名
* @param tableName 表名
* @throws Exception
*/
public static void dbRestoreMysql(String username,String pwd,String hostIp,String dbName,String tableName,String savePath) throws Exception{
//mysql -h localhost -u root -p root db < /test/test.sql
StringBuilder sb = new StringBuilder();
sb.append("mysql");
sb.append(" -h"+hostIp);
sb.append(" -u"+username);
sb.append(" -p"+pwd);
sb.append(" "+dbName+" <");
sb.append(savePath+tableName+".sql");
System.out.println("cmd命令为:"+sb.toString());
Process process = null;
//判断操作系统 windwos与linux使用不同进入dos命令
if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
process = Runtime.getRuntime().exec("cmd /c"+sb.toString());
}else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
process = Runtime.getRuntime().exec("/bin/sh -c"+sb.toString());
}else{
throw new Exception("暂不支持该操作系统,进行数据库备份或还原!");
}
System.out.println("开始还原数据");
InputStream is = process.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(is,"utf8"));
String line = null;
while ((line=bf.readLine())!=null){
System.out.println(line);
}
is.close();
bf.close();
System.out.println("还原成功!");
}
public static void main(String[] args) throws Exception {
//调用备份
try {
dbBackUpMysql("root","root", "127.0.0.1","test","t_alarm","D:\\test\\");
} catch (InterruptedException e) {
e.printStackTrace();
}
//调用还原
dbRestoreMysql("root","root", "127.0.0.1","test","t_alarm","D:\\test\\");
}
}
注意:mysql数据库一点要设置密码(正常来说都是设置的),我当时图方便没设置密码,导致备份不成功