中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

通過(guò)java代碼備份恢復(fù)mysql

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Test {
    public static void main(String[] args) throws IOException{
        backup("d:\\\\d.sql");http://www.huiyi8.com/jiaoben/
        recover("d:\\\\d.sql");
    }
    public static void backup(String path) throws IOException{
        Runtime runtime = Runtime.getRuntime();
        //-u后面是用戶名,-p是密碼-p后面最好不要有空格,-family是數(shù)據(jù)庫(kù)的名字
        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是數(shù)據(jù)庫(kù)的名字,--default-character-set=utf8,這句話一定的加
        //我就是因?yàn)檫@句話沒(méi)加導(dǎo)致程序運(yùn)行成功,但是數(shù)據(jù)庫(kù)里面的內(nèi)容還是以前的內(nèi)容,最好寫上完成的sql放到cmd中一運(yùn)行才知道報(bào)錯(cuò)了
        //錯(cuò)誤信息:
        //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): Can't
        // initialize character set utf-8 (path: C:\\Program Files\\MySQL\\MySQL Server 5.5\\share\\charsets\\),
        //又是討人厭的編碼問(wèn)題,在恢復(fù)的時(shí)候設(shè)置一下默認(rèn)的編碼就可以了。
        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();
    }
}

標(biāo)簽: Mysql 數(shù)據(jù)庫(kù)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:采用json-lib進(jìn)行Map與Json轉(zhuǎn)換

下一篇:Java借助Zxing生成二維碼