How to Backup mysql db from Remote to local without mysql local installation?

孤街醉人 提交于 2019-12-25 04:16:35

问题


Good Day, I am trying to take mysql backup from remote server to my local machine. I am able to do so, when i have mysql client installed on local machine. But when the client doesnt have local mysql client installation, it is not working ...

I have been connecting to mysql server using the jdbc connection and my code snippet is as follows:

try{     
String cs = "jdbc:mysql://" + dbh + ":" + dbport + "/" + database + "?user=" + USER + "&password=" + PASS+"";
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = java.sql.DriverManager.getConnection(cs);
String executeCmd = "";
     if(connection!=null){
        executeCmd = "mysqldump -u "+USER+" -p"+PASS+" "+database+" -r "+path;   
        Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();
        if(processComplete == 0){
            System.out.println("Backup taken successfully");
        } else {
            System.out.println("Could not take mysql backup");
        }
    } else{
        System.out.println("connection not sucess");
    }
}catch (Exception e) {
            e.printStackTrace();
}

I think this might be some problem with mysqldump PATH, so i copied this file to client machine, and specified absolute path to mysqldump in executeCmd. This time the backupfile is created in specified path, but its empty & processComplete returns a value 2. My Questions are,

  1. Do i need any other mysql files to run the mysqldump command? If so how to do that other than copy these commands to local machine?
  2. What does waitFor() with return value 2 mean?

Thanks & Regards

iMmo


回答1:


Solved it!, I forget to specify the HostIP & port with the mysqldump command Changed my executeCmd parameter as following:

executeCmd = PathToMySqlDumpFile+"mysqldump -h "+HOSTIP+" -P "+PORT+" -u "+USER+" -p"+PASS+" "+database+" -r "+path; 

along with the mysqldump file path. We only need the mysqldump file to take the backup from remote server to local. No need for mysql client installation.



来源:https://stackoverflow.com/questions/16954543/how-to-backup-mysql-db-from-remote-to-local-without-mysql-local-installation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!