mysql-net / MySqlConnector

MySQL Connector for .NET
https://mysqlconnector.net
MIT License
1.37k stars 330 forks source link

Question: Does MySqlConnector have ability to restore multi gig .sql file output from mysqldump? #1487

Closed AFDevMike closed 14 minutes ago

AFDevMike commented 1 week ago

Is there a direct analog to: mysql -u -p theDb < c:\GiantDbBackup.sql OR mysql source c:\GiantDbBackup.sql

Alternatively, is there an option to execute a MySqlCommand where is source is a stream?

bgrainger commented 1 week ago

No, there's nothing directly analogous.

I don't actually know how mysql source works but I assume it would have to stream the dumped .sql file to MySQL Server in a series of COM_QUERY packets. Most likely, it parses the input file into a series of SQL statements and executes each one individually on the server. (Possibly with some special handling for USE statements or anything else that needs to update client-side state rather than (or in addition to) server-side state.)

I think you would need to do something similar; i.e., in pseudocode:

var lines = File.ReadLines(inputSqlFile);
foreach (var statement in ParseLinesIntoStatements(lines))
{
    using var command = connection.CreateCommand();
    command.CommandText = statement;
    command.ExecuteNonQuery();
}

Obviously the complexity is all in ParseLinesIntoStatements which worst case is completely rewriting the SQL parsing from mysql CLI in C#.