Jordi Kroon
Jordi Kroon

Reputation: 2597

mysql dump - Unknown database 'mysqldump'

I am trying to export a database from c# using mysqldump.

When I run it i get this message: Unknown database 'mysqldump' when selecting the database. I can't find the solution.

public static void mysqlBackup()
{
    try
    {
        //string time = DateTime.Now.ToString("dd-MM-yyyy");

        Log.Info("Starting MySQL dump");


        Process MySqlDump = new Process();
        MySqlDump.StartInfo.FileName = @"mysqldump.exe";
        MySqlDump.StartInfo.UseShellExecute = false;
        MySqlDump.StartInfo.Arguments = 
           "mysqldump -uroot -p******** b3 >"+ 
           " C:/Users/Administrator/Documents/temp/backups/backup.sql";
        MySqlDump.StartInfo.RedirectStandardInput = false; 
        MySqlDump.StartInfo.RedirectStandardOutput = false;

        MySqlDump.Start();

        MySqlDump.WaitForExit();
        MySqlDump.Close();

        Log.Info("Successfull created");
    }

    catch (IOException ex)
    {
        Log.Error("Unable to write the database file" + ex.ToString());
    }
}

I tried to remove the mysqldump from the arguments kinda the same problem.

Upvotes: 0

Views: 4438

Answers (4)

Robert H
Robert H

Reputation: 11730

The Mysql documentation states there are 3 ways to use the mysqldump command:

shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases

Ensure the command works fine via your command line. If it does that execute that command directly within your code. If that works then start extracting your arguments and replacing them with your own parameters within code.

Basically you want to get as basic as possible and work back up from there.

If the file works on the command line, try this:

using (Process p = new Process())
{        
        p.StartInfo.FileName = @"mysqldump.exe -u root -p *** --database b3 -r test.sql"; <~~~ note the change here
        p.Start();
        p.WaitForExit();
}

The file will be dumped to your project folders bin/debug or bin/release folder unless you change that code.

Here is your edited method:

public static void mysqlBackup() { try {

        //string time = DateTime.Now.ToString("dd-MM-yyyy");

        Log.Info("Starting MySQL dump");


    using(Process MySqlDump = new Process()
    {
        MySqlDump.StartInfo.FileName = @"mysqldump.exe";
        MySqlDump.StartInfo.UseShellExecute = false;
        MySqlDump.StartInfo.Arguments = "-uroot -p******** b3 --result-file=C:/Users/Administrator/Documents/temp/backups/backup.sql";
        MySqlDump.StartInfo.RedirectStandardInput = false; 
        MySqlDump.StartInfo.RedirectStandardOutput = false; //You can redirect this as mention in other answers 

        MySqlDump.Start();

        MySqlDump.WaitForExit();
        MySqlDump.Close();
    }
   Log.Info("Successfully created");
    }

    catch (IOException ex)
    {
        Log.Error("Unable to write the database file" + ex.ToString());
    }
}

Upvotes: 0

Levi Botelho
Levi Botelho

Reputation: 25204

MySqlDump.StartInfo.Arguments = "-u root -p *** database_name --result-file [path]\backup.sql";

You don't need to specify mysqldump again in the command either (not that it should make much difference).

Upvotes: 1

Richard
Richard

Reputation: 30618

The redirection operator > is not an argument to mysqldump. When you execute it on the command line, it's being interpreted by the command line itself, not by mysqldump. You have two choices here:

  1. Use the --result-file option as others have mentioned
  2. Capture the stdout of the process and do what you like with the output by setting the RedirectStandardOutput property of StartInfo to be true. After this, you can read from the StandardOutput stream of the process.

Upvotes: 4

Daniel A. White
Daniel A. White

Reputation: 190907

I think you need to specify the name of the database you want to dump as the first argument. Thanks to nathan it goes after --databases at the end.

Upvotes: 1

Related Questions