【修改】mysql进程与管理员冲突,关闭进程

This commit is contained in:
huababa1
2025-08-25 16:49:35 +08:00
parent 3e1020babe
commit f21c7d4752

View File

@@ -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. 启动临时 MySQLWinExe 不重定向输出)
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;
}
}