当前位置:网站首页>MySQL backup and restore command

MySQL backup and restore command

2022-06-26 10:42:00 MervynLammm

Mysql Backup and restore commands

  • Backup command :
    mysqldump -h127.0.0.1 -P3306 -uroot -p123456 mytable > mytable.sql
    
  • Restore command :
    mysql -h127.0.0.1 -P3306 -uroot -p123456 mytable < mytable.sql
    

-h host
-P port
-u user name
-p password
mytable Libraries that need to be backed up
mytable.sql Backup file name

Java Realization

// Backup 
public void backup() {
    
    String[] execCMD = {
    "mysqldump", "-h",+hostname, "-P"+ port, "-u"+user, "-p"+password, databaseName, ">", filename};
    try {
    
        Runtime.getRuntime().exec(execCMD);
    } catch(Exception e) {
    
        e.printStackTrace();
    }
}
// recovery 
public void restore() {
    
    String[] execCMD = {
    "mysql", "-h",+hostname, "-P"+ port, "-u"+user, "-p"+password, databaseName, "<", filename};
    try {
    
        Runtime.getRuntime().exec(execCMD);
    } catch(Exception e) {
    
        e.printStackTrace();
    }
}

windows The system is in exceCMD Add in front "cmd","/c"
In this way, the files I backed up 0 byte . I don't know why . See the solution below .

// recovery 
public void backup() {
    
    String[] execCMD = {
    "bash", "./backupSql.sh", hostname, 
port, user, password, databaseName, filename};
    try {
    
        Runtime.getRuntime().exec(execCMD);
    } catch(Exception e) {
    
        e.printStackTrace();
    }
}
public void restore() {
    
    String[] execCMD = {
    "bash", "./restoreSql.sh", hostname, 
port, user, password, databaseName, filename};
    try {
    
        Runtime.getRuntime().exec(execCMD);
    } catch(Exception e) {
    
        e.printStackTrace();
    }
}

Script files backupSql.sh

mysqldump -h"$1" -P"$2" -u"$3" -p"$4" "$5" > "$6"

Script files restoreSql.sh

mysql -h"$1" -P"$2" -u"$3" -p"$4" "$5" < "$6"
原网站

版权声明
本文为[MervynLammm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170529156244.html