使用Java环境从CMD行恢复MYSQL

发布于 2024-07-19 19:56:09 字数 450 浏览 6 评论 0原文

我正在使用 Java 和 Mysql 作为程序,我正在使用脚本文件来恢复数据库。

在Java下我正在执行命令:在bin下:mysql -u root -proot test< c:\test.mysql

它没有运行,而如果我在命令行下运行它,它将正确执行并恢复数据库。

有谁知道.. 为什么会发生.. 这是什么问题,为什么我在Java环境下运行它无法运行。

确切的语法: 我正在使用 Process P= runtime.getRunTime().exec(FilePath)

其中 FilePath 变量具有值: mysql -u root -proot test< c:\test.mysql

我使用的是Windiws环境。 而如果我在 CmdLine 中运行 FilePath,它将给出完美的结果。

非常感谢或帮助。

I am using Java and Mysql for a Program, I am Using a Script File in order to restore a Databsae.

Under Java I am Executing a command:under bin: mysql -u root -proot test< c:\test.mysql

It is not running while If I run it under cmd line it will execute properly and restore the database.

Is anybody there who knows.. why it happens..
Whats the problem, why its not running if i run it under Java environment.

EXACT SYNTAX:
I m Using Process P= runtime.getRunTime().exec(FilePath)

where FilePath Variable is having value: mysql -u root -proot test< c:\test.mysql

I am Using Windiws environment. while if I run the FilePath in CmdLine, it will give the perfect reesult.

Highly thankful or help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下壹個目標 2024-07-26 19:56:09

我有同样的问题!

实际上,我唯一可以做的工作(在 Windows 上,尚未测试其他平台)是使用批处理文件:

这里是代码:

public class MysqlDatabase {

private int BUFFER = 10485760;
private String host, port, user, password, db;

public MysqlDatabase(String host, String port, String user, String password, String db) {
    this.host = host;
    this.port = port;
    this.user = user;
    this.password = password;
    this.db = db;
}

public boolean restoreDatabase(String filepath) throws Exception {
    String comando = "mysql " + db + " --host=" + host + " --port=" + port
            + " --user=" + user + " --password=" + password
            + " < " + filepath;
    File f = new File("restore.bat");
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(comando.getBytes());
    fos.close();
    Process run = Runtime.getRuntime().exec("cmd /C start restore.bat ");
    return true;

}

public String getFull() throws Exception {

    Process run = Runtime.getRuntime().exec(
            "mysqldump --host=" + host + " --port=" + port
            + " --user=" + user + " --password=" + password
            + " --opt "
            + "" + db);
    InputStream in = run.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    StringBuilder temp = new StringBuilder();

    int count;
    char[] cbuf = new char[BUFFER];

    while ((count = br.read(cbuf, 0, BUFFER)) != -1) {
        temp.append(cbuf, 0, count);
    }

    br.close();
    in.close();

    return temp.toString();
}}

I had the same problem!

Actually the only thing that I could make work (on Windows, havent tested other platforms) is using batch files:

here is the code:

public class MysqlDatabase {

private int BUFFER = 10485760;
private String host, port, user, password, db;

public MysqlDatabase(String host, String port, String user, String password, String db) {
    this.host = host;
    this.port = port;
    this.user = user;
    this.password = password;
    this.db = db;
}

public boolean restoreDatabase(String filepath) throws Exception {
    String comando = "mysql " + db + " --host=" + host + " --port=" + port
            + " --user=" + user + " --password=" + password
            + " < " + filepath;
    File f = new File("restore.bat");
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(comando.getBytes());
    fos.close();
    Process run = Runtime.getRuntime().exec("cmd /C start restore.bat ");
    return true;

}

public String getFull() throws Exception {

    Process run = Runtime.getRuntime().exec(
            "mysqldump --host=" + host + " --port=" + port
            + " --user=" + user + " --password=" + password
            + " --opt "
            + "" + db);
    InputStream in = run.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    StringBuilder temp = new StringBuilder();

    int count;
    char[] cbuf = new char[BUFFER];

    while ((count = br.read(cbuf, 0, BUFFER)) != -1) {
        temp.append(cbuf, 0, count);
    }

    br.close();
    in.close();

    return temp.toString();
}}
夏尔 2024-07-26 19:56:09

我认为我们需要更多信息。 只要路径设置相同,如果从命令行运行,则应该从 Runtime.exec() 运行相同的路径。 您看到什么错误?

尝试在脚本中设置命令,以便您可以将路径和命令输出回显到文件中以便稍后查看。 在 UNIX 中,看起来像

 LOGFILE=my.log
 echo $PATH > $LOGFILE
 env >> $LOGFILE
 mysql -u root -proot test< c:\test.mysql >> $LOGFILE 2>&1

你使用的是 Windows,所以我不知道如何以这种方式设置命令文件; 重要的是确保将 mysql 命令的错误输出发送到文件。

I think we need some more information. As long as the paths are set up the same, if it will run from the command line, it should run the same from Runtime.exec(). What errors do you see?

Try setting the commend up in a script so you can echo the paths and the command output to a file to look at later. In UNIX that would look like

 LOGFILE=my.log
 echo $PATH > $LOGFILE
 env >> $LOGFILE
 mysql -u root -proot test< c:\test.mysql >> $LOGFILE 2>&1

It looks like you're using Windows, so I don't know how to set of the command file exactly this way; what's important is to make sure you're sending the error output of the mysql commend to the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文