简体   繁体   English

Java中的MySQL转储不起作用

[英]mysql dump in java not working

i am trying this command in java 我正在java中尝试此命令

executeCmd = DBConfig.dbLocation + "\\mysqldump -u " + mysqlUser +
    " -p"+DBConfig.dbPass + " " + DBConfig.dbName + 
    " -r -opt>supervisorDbBkup.sql";

Its giving me code 1 instead of 0 它给我代码1而不是0

            String executeCmd = "";
    executeCmd = DBConfig.dbLocation+"\\mysqldump -u"+mysqlUser+
            " -p"+DBConfig.dbPass+" "+DBConfig.dbName+" -r -opt>supervisorDbBkup.sql";

    Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
    int processComplete = runtimeProcess.waitFor();
    if(processComplete == 0){
        return "Backup taken successfully";
    } else {
        return "Could not take mysql backup";
    }

this is the command which i am running : 这是我正在运行的命令:

                E:\program file\MySQL\MySQL Server 5.5\bin\mysqldump -uroot -p0502 dashboardsupervisor -r -opt>supervisorDbBkup.sql

any idea 任何想法

What you need to do is to print out the contents of executeCmd after you've built it, to see what it contains, with something like: 您需要做的是,在构建完后,打印出executeCmd的内容,以查看其中包含的内容,例如:

Console.Out.WriteLine (executeCmd);

If that command doesn't work at the command line exactly as you've constructed it, it's unlikely to work within your program. 如果该命令在您构建的命令行中无法完全正常运行,则它不太可能在您的程序中运行。 Copy and paste the command exactly as it's output and attempt to execute it from a cmd shell. 复制和粘贴命令,使其与输出完全相同 ,并尝试从cmd shell执行该cmd

I suspect your problem lies with the path to the executable or one of the options. 我怀疑您的问题出在可执行文件或选项之一的路径上。

If that's not the case, you need to watch out for redirection. 如果不是这种情况,则需要注意重定向。 Redirection is a shell feature which may not be available to the Runtime.exec() call. 重定向是一种外壳功能,它可能不适用于Runtime.exec()调用。 What you will find is that the redirection bit is being passed as a parameter to the mysqldump execuatble which won't understand it at all. 您会发现,重定向位作为参数传递给了mysqldump execuatble,而后者根本无法理解。

The usual way to do this is to call the shell itself with arguments telling it how to run the command and redirect properly, something like: 这样做的通常方法是使用参数告诉外壳本身,该参数告诉外壳程序如何运行命令并正确重定向,例如:

String[] cmd = {"/bin/sh", "-c", "/path/to/mysqldump blah blah >/tmp/tempout"};
Process p = Runtime.getRuntime().exec(cmd);

(or equivalent cmd /c for Windows). (或Windows的等效cmd /c )。

That ensures that redirection is handled correctly (by the shell itself) and the mysqldump executable only gets the arguments it understands. 这样可以确保正确地处理了重定向(由外壳本身),并且mysqldump可执行文件仅获取其理解的参数。

Try the following 尝试以下

executeCmd = "<Absolute Path to MYSQL>\bin" + "\\mysqldump -u " + mysqlUser +
    " -p"+DBConfig.dbPass + " " + DBConfig.dbName + 
    " -r --opt > supervisorDbBkup.sql";

UPDATE: 更新:

-opt is not an option for mysqldump following is -opt不是mysqldump的选项,以下是

--opt

try with this command "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump -u username -pPASS_WORD database_name -r backup.sql" 尝试使用此命令“ C:\\ Program Files \\ MySQL \\ MySQL Server 5.1 \\ bin \\ mysqldump -u用户名-pPASS_WORD database_name -r backup.sql”

Mind the space between -u and username and -p and password(there should be no space between -p and password). 注意-u和用户名之间以及-p和密码之间的空间(-p和密码之间不应有空格)。 It worked for me. 它为我工作。

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

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