用Java代码备份和还原MySQL数据库详解编程语言
2023-06-13 09:20:28 时间
public static void backup(String path) throws IOException{
Runtime runtime = Runtime.getRuntime();
//-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字
Process process = runtime.exec("mysqldump -u root -p123456 family");
InputStream inputStream = process.getInputStream();//得到输入流,写成.sql文件
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(reader);
String s = null;
StringBuffer sb = new StringBuffer();
while((s = br.readLine()) != null){
sb.append(s+"/r/n");
s = sb.toString();
System.out.println(s);
File file = new File(path);
file.getParentFile().mkdirs();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
br.close();
reader.close();
inputStream.close();
public static void recover(String path) throws IOException{
Runtime runtime = Runtime.getRuntime();
//-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字,--default-character-set=utf8,这句话一定的加
//我就是因为这句话没加导致程序运行成功,但是数据库里面的内容还是以前的内容,最好写上完成的sql放到cmd中一运行才知道报错了
//错误信息:
//mysql: Character set utf-8 is not a compiled character set and is not specified in the
//C:/Program Files/MySQL/MySQL Server 5.5/share/charsets/Index.xml file ERROR 2019 (HY000): Cant
// initialize character set utf-8 (path: C:/Program Files/MySQL/MySQL Server 5.5/share/charsets/),
//又是讨人厌的编码问题,在恢复的时候设置一下默认的编码就可以了。
Process process = runtime.exec("mysql -u root -p123456 --default-character-set=utf8 family");
OutputStream outputStream = process.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String str = null;
StringBuffer sb = new StringBuffer();
while((str = br.readLine()) != null){
sb.append(str+"/r/n");
str = sb.toString();
System.out.println(str);
OutputStreamWriter writer = new OutputStreamWriter(outputStream,"utf-8");
writer.write(str);
writer.flush();
outputStream.close();
br.close();
writer.close();
10145.html
cjavamysqlxml相关文章
- MySQL实现高效增删改查(mysql实现增删改查)
- 实现MySQL远程数据库的备份与恢复(mysql备份远程数据库)
- 重置MySQL密码重置:轻松完成充值(mysql充值密码)
- Mysql数据库的事务:实现回滚的绝招(mysql数据库事务回滚)
- 分析MySQL数据库日志分析实践(mysql数据库日志)
- MySQL数据库登录:安全的密码认证(mysql密码登陆)
- MySQL存储过程返回表:实现高效数据处理(mysql存储过程返回表)
- VB连接远程MySQL数据库实现数据异地备份(vb远程mysql)
- MySQL中如何查询表格(mysql中查询表)
- 如何在MySQL中使用CHAR字段来实现自增功能(mysql中char自增)
- 进使用C语言改进MySQL数据库性能(cmysql 改)
- 使用cmd命令调入MySQL数据库(cmd调入mysql)
- ADO MySQL连接技巧开放端口让数据源安全读取(ado mysql 端口)
- 优化MySQL性能优化处理3亿条数据的利器(3亿数据mysql性能)
- 使用CMD命令关闭Mysql数据库(cmd关闭mysql代码)
- 从sql中刷新MySQL数据库(.sql刷mysql)
- MySQL数据库操作如何快速查询前3行数据(mysql中前3行)
- MySQL多实例安装及配置指南(mysql_multi)
- MySQL 数据库查询多种查询方式解析(mysql不同的查询方式)
- Mysql游标优化查询和提高效率的好帮手(mysql上面的游标)
- 解锁MySQL功夫周一零点MySQL技巧攻略(mysql 上周一 0点)