C# 读取MySql binlog
我不同意这个问题得到了有效的回答 我有,我认为是相同的问题:我想从c#应用程序中读取MySql Binlog,但不知道文件的格式。如何正确解析这些文件的数据?首先,我学到了:C# 读取MySql binlog,c#,mysql,mysqlbinlog,C#,Mysql,Mysqlbinlog,我不同意这个问题得到了有效的回答 我有,我认为是相同的问题:我想从c#应用程序中读取MySql Binlog,但不知道文件的格式。如何正确解析这些文件的数据?首先,我学到了: MySql的大多数源代码文件与程序集一起安装,通常位于[basedir]\include中。例如,典型的安装会将文件放在Program files\MySql\MySql 5.6\include中 mysqlbin.cc不在该文件夹中。然而,我很容易通过谷歌快速搜索得到这个文件。可以在此处找到该文件:。它有很好的文档记录,
query show global variables like 'datadir' //returns the data directory.
query show binary logs //returns the filename of each binary log, along with its file size (helpful for reading).
- 将这些内容解析在一起将获得每个二进制日志的路径。
query show global variables like 'basedir'
然后,我用“\bin\mysqlbinlog.exe”解析结果private static string GetLogTexts(Liststring> logfilenames)
{
List<string> _logtexts = new List<string>();
string _basedir = GetBaseDir();
foreach(string logfilename in logfilenames)
{
Process proc = new Process();
proc.StartInfo.FileName = _basedir + "\\bin\\mysqlbinlog";
proc.StartInfo.Arguments = string.Format("\"{0}\"", logfile);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
_logtexts.Add(proc.StandardOutput.ReadToEnd());
}
return _logtexts;
}
private static string GetBaseDir()
{
string path = "";
using (MySqlConnection conn = new MySqlConnection(RemoteServerConnectionString))
{
conn.Open();
using (MySqlCommand cmd1 = new MySqlCommand("show global variables like 'basedir'", conn))
{
using (MySqlDataReader reader = cmd1.ExecuteReader())
{
while (reader.Read())
{
path = reader.GetString(1);
}
}
}
}
return path;
}
私有静态字符串getLogText(Liststring>LogFileName)
{
列表_logtext=新列表();
字符串_basedir=GetBaseDir();
foreach(logfilename中的字符串logfilename)
{
Process proc=新流程();
proc.StartInfo.FileName=\u basedir+“\\bin\\mysqlbinlog”;
proc.StartInfo.Arguments=string.Format(“{0}\”,日志文件);
proc.StartInfo.UseShellExecute=false;
proc.StartInfo.RedirectStandardInput=proc.StartInfo.RedirectStandardOutput=true;
proc.Start();
_添加(proc.StandardOutput.ReadToEnd());
}
返回日志文本;
}
私有静态字符串GetBaseDir()
{
字符串路径=”;
使用(MySqlConnection conn=newmysqlconnection(RemoteServerConnectionString))
{
conn.Open();
使用(MySqlCommand cmd1=new MySqlCommand(“显示全局变量,如'basedir',conn))
{
使用(MySqlDataReader=cmd1.ExecuteReader())
{
while(reader.Read())
{
path=reader.GetString(1);
}
}
}
}
返回路径;
}
我希望这对别人有帮助 问题不在于格式化知识。您可以从源代码中获得它。问题是格式可能随时改变,因为没有人承诺它会持续。我会用mysqlbinlog来解析它。谢谢你的回复。最后,我特别想知道如何自己解析binlog而不使用外部实用程序。您知道格式的确切位置吗?或者我如何找到它们?很抱歉我不知道如何访问(大概是MySql)源代码。同时,如果有办法从c#中使用mysqlbinlog实用程序,那么您的建议可能对启动我的应用程序有用。这可能吗?如果是,你知道怎么做吗?感谢您从./client/mysqlbinlog.cc开始。格式应该简单明了。我的C#知识几乎为零,我想一定有类似exec()的东西可以调用外部程序