From f21c7d475263f51275d770d0518b16ffd8b74327 Mon Sep 17 00:00:00 2001 From: huababa1 <2037205722@qq.com> Date: Mon, 25 Aug 2025 16:49:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91mysql?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E4=B8=8E=E7=AE=A1=E7=90=86=E5=91=98=E5=86=B2?= =?UTF-8?q?=E7=AA=81=EF=BC=8C=E5=85=B3=E9=97=AD=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications/Program.cs | 69 +++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/Applications/Program.cs b/Applications/Program.cs index a7effbd..845ce70 100644 --- a/Applications/Program.cs +++ b/Applications/Program.cs @@ -219,7 +219,6 @@ class Program break; } } - Console.WriteLine("已有 MySQL 正在运行,直接使用它1"); if (mysqlBinDir == null) { mysqlBinDir = FindMySqlBin(); if (mysqlBinDir == null) @@ -228,7 +227,6 @@ class Program } } - Console.WriteLine("已有 MySQL 正在运行,直接使用它2"); // 2. 设置临时数据目录 string tempData = @"D:\mysql_temp_data"; string tempIni = @"D:\mysql_temp.ini"; @@ -240,7 +238,6 @@ class Program { using var conn = new MySqlConnection($"server=127.0.0.1;port={port};user=root;password=;Charset=utf8mb4;"); conn.Open(); - Console.WriteLine("已有 MySQL 正在运行,直接使用它3"); } catch { @@ -249,8 +246,6 @@ class Program } else { - Console.WriteLine("已有 MySQL 正在运行,直接使用它4"); - try { if (Directory.Exists(tempData)) @@ -267,52 +262,48 @@ class Program using (StreamWriter writer = new StreamWriter(tempIni)) { - writer.WriteLine("[mysqld]"); writer.WriteLine($"datadir={tempData}"); writer.WriteLine($"port={port}"); writer.WriteLine("skip-networking=0"); writer.WriteLine("skip-grant-tables=0"); writer.WriteLine($"log-error={tempData}\\mysql.err"); - - - } } catch (Exception ex) { - Console.WriteLine("已有 MySQL 正在运行,直接使用它3" + ex); } - Console.WriteLine("已有 MySQL 正在运行,直接使用它5"); // 4. 初始化临时数据库 if (!RunProcess(Path.Combine(mysqlBinDir, "mysqld.exe"), $"--defaults-file=\"{tempIni}\" --initialize-insecure")) { return; } - Console.WriteLine("已有 MySQL 正在运行,直接使用它6"); // 5. 启动临时 MySQL(WinExe 不重定向输出) Process mysqlProcess = new Process(); mysqlProcess.StartInfo.FileName = Path.Combine(mysqlBinDir, "mysqld.exe"); mysqlProcess.StartInfo.Arguments = $"--defaults-file=\"{tempIni}\" --standalone --port={port}"; - mysqlProcess.StartInfo.UseShellExecute = true; - // mysqlProcess.StartInfo.CreateNoWindow = true; - // mysqlProcess.StartInfo.RedirectStandardOutput = true; - // mysqlProcess.StartInfo.RedirectStandardError = true; + + // 必须 false 才能重定向输出和隐藏窗口 + mysqlProcess.StartInfo.UseShellExecute = false; + mysqlProcess.StartInfo.CreateNoWindow = true; + + // 重定向输出 + mysqlProcess.StartInfo.RedirectStandardOutput = true; + mysqlProcess.StartInfo.RedirectStandardError = true; + + // 捕获输出 + mysqlProcess.OutputDataReceived += (s, e) => { if (e.Data != null) Console.WriteLine("[MySQL] " + e.Data); }; + mysqlProcess.ErrorDataReceived += (s, e) => { if (e.Data != null) Console.WriteLine("[MySQL-ERR] " + e.Data); }; mysqlProcess.Start(); + // 开始异步读取输出 mysqlProcess.BeginOutputReadLine(); mysqlProcess.BeginErrorReadLine(); - + } - Console.WriteLine("已有 MySQL 正在运行,直接使用它"); - - - - - // 6. 等待 MySQL 可连接(最长等待 30 秒) string connStr = $"server=127.0.0.1;port={port};user=root;password=;Charset=utf8mb4;"; @@ -517,6 +508,18 @@ class Program procs.Kill(); } } + + + // 关闭临时 MySQL(只关掉端口 48086) + if (proc.ProcessName.Equals("mysqld", StringComparison.OrdinalIgnoreCase)) + { + string cmdLine = GetCommandLine(proc); + if (!string.IsNullOrEmpty(cmdLine) && cmdLine.Contains("--port=48086")) + { + Console.WriteLine($"Closing temporary MySQL (PID: {proc.Id}) on port 48086"); + proc.Kill(); + } + } } catch { @@ -997,6 +1000,26 @@ class Program return null; // 没找到 } + // 获取进程命令行,需要添加 System.Management 引用 + public static string GetCommandLine(Process process) + { + try + { + using (var searcher = new System.Management.ManagementObjectSearcher( + "SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id)) + { + foreach (var obj in searcher.Get()) + { + return obj["CommandLine"]?.ToString(); + } + } + } + catch + { + return null; + } + return null; + } }