简体   繁体   English

恢复mySQL数据库的运行时过程

[英]Runtime process to restore mySQL database

Similar to this link: " How to restore a MySQL database backup using Java "类似于此链接:“ 如何使用 Java 恢复 MySQL 数据库备份

I'm trying to restore a database using mysql.exe by executing the command through runtime processes.我正在尝试通过运行时进程执行命令来使用 mysql.exe 恢复数据库。 I've tried several commands but I can't seem to make it work.我已经尝试了几个命令,但似乎无法使其工作。 They all seem to hang and return an exit value of 1.它们似乎都挂起并返回退出值 1。

These are the commands I've tried:这些是我尝试过的命令:

    String executeCmd = mySQLDir+"\\bin\\mysql -u root --password= testdb < " + backupFile;
    String[] executeCmd = new String[]{mySQLDir+"\\bin\\mysql", "-u", "root", "--password=", "testdb", " < " + backupFile};
    String[] executeCmd = new String[]{mySQLDir+"\\bin\\mysql", "--user=root", "--password=" , "testdb -e", " source "+backupFile};  
    String executeCmd = mySQLDir+"\\bin\\mysql -u root --password= testdb -e source "+ backupFile;

I've tried to output what trying to happen during the execution through input stream and this occurs prints out (There's a lot more text but the first two lines are this):我试图通过输入流输出在执行期间尝试发生的事情,这会打印出来(有更多的文本,但前两行是这样的):

C:\xampp\mysql\bin\mysql  Ver 15.1 Distrib 10.1.9-MariaDB, for Win32 (AMD64)
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Usage: C:\xampp\mysql\bin\mysql [OPTIONS] [database]

So from what I'm understanding it doesn't seem to recognize the commands through java, but doing it through command prompt seems to work.因此,据我所知,它似乎无法通过 java 识别命令,但通过命令提示符执行此操作似乎可行。 Any ideas?有任何想法吗?

Edit: A user mentioned that command might be incorrect so heres the output to the third executeCommand:编辑:一位用户提到该命令可能不正确,因此这里是第三个 executeCommand 的输出:

C:\xampp\mysql\bin\mysql
--user=root
--password=
testdb -e
source C:\Users\AA\Documents\NetBeansProjects\TestProject\backup\testDBBackup69-03-2016%19-27-45.sql

I tried playing around with spaces in second command :我尝试在第二个命令中使用空格:

String[] executeCmd = new String[]{mySQLDir+"\\bin\\mysql", " -u ", "root", " --password=", " testdb ", "< " + backupFile};

result in using system.print output:导致使用 system.print 输出:

C:\xampp\mysql\bin\mysql -u root --password= test < C:\Users\AA\Documents\NetBeansProjects\TestProject\backup\testDBBackup69-03-2016%19-27-45.sql

I know this command works for sure in cmd but still doesn't work in java我知道这个命令在 cmd 中肯定有效,但在 java 中仍然无效

I have solved the issue, answer is below.我已经解决了这个问题,答案如下。

You can't get redirection unless you start a shell.除非启动 shell,否则无法重定向。 An exe won't understand < . exe 不会理解<

Put "cmd", "/c" into the front of the String[] array."cmd", "/c"放在 String[] 数组的前面。

I've solved this issue by using Process Builder class instead.我已经通过使用 Process Builder 类解决了这个问题。

Example code:示例代码:

        String[] command = new String[]{mySQLDir+"\\bin\\mysql ", "-uroot", "--password=", "testdb"};
        ProcessBuilder processBuilder = new ProcessBuilder(command);
        processBuilder = new ProcessBuilder(Arrays.asList(command));
        processBuilder.redirectError(Redirect.INHERIT);
        processBuilder.redirectInput(Redirect.from(backupFile));
        Process process = processBuilder.start();
        process.waitFor();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM