【修改】mysql进程与管理员冲突,关闭进程
This commit is contained in:
@@ -219,7 +219,6 @@ class Program
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它1");
|
|
||||||
if (mysqlBinDir == null) {
|
if (mysqlBinDir == null) {
|
||||||
mysqlBinDir = FindMySqlBin();
|
mysqlBinDir = FindMySqlBin();
|
||||||
if (mysqlBinDir == null)
|
if (mysqlBinDir == null)
|
||||||
@@ -228,7 +227,6 @@ class Program
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它2");
|
|
||||||
// 2. 设置临时数据目录
|
// 2. 设置临时数据目录
|
||||||
string tempData = @"D:\mysql_temp_data";
|
string tempData = @"D:\mysql_temp_data";
|
||||||
string tempIni = @"D:\mysql_temp.ini";
|
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;");
|
using var conn = new MySqlConnection($"server=127.0.0.1;port={port};user=root;password=;Charset=utf8mb4;");
|
||||||
conn.Open();
|
conn.Open();
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它3");
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@@ -249,8 +246,6 @@ class Program
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它4");
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (Directory.Exists(tempData))
|
if (Directory.Exists(tempData))
|
||||||
@@ -267,52 +262,48 @@ class Program
|
|||||||
using (StreamWriter writer = new StreamWriter(tempIni))
|
using (StreamWriter writer = new StreamWriter(tempIni))
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
writer.WriteLine("[mysqld]");
|
writer.WriteLine("[mysqld]");
|
||||||
writer.WriteLine($"datadir={tempData}");
|
writer.WriteLine($"datadir={tempData}");
|
||||||
writer.WriteLine($"port={port}");
|
writer.WriteLine($"port={port}");
|
||||||
writer.WriteLine("skip-networking=0");
|
writer.WriteLine("skip-networking=0");
|
||||||
writer.WriteLine("skip-grant-tables=0");
|
writer.WriteLine("skip-grant-tables=0");
|
||||||
writer.WriteLine($"log-error={tempData}\\mysql.err");
|
writer.WriteLine($"log-error={tempData}\\mysql.err");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它3" + ex);
|
|
||||||
}
|
}
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它5");
|
|
||||||
// 4. 初始化临时数据库
|
// 4. 初始化临时数据库
|
||||||
if (!RunProcess(Path.Combine(mysqlBinDir, "mysqld.exe"), $"--defaults-file=\"{tempIni}\" --initialize-insecure"))
|
if (!RunProcess(Path.Combine(mysqlBinDir, "mysqld.exe"), $"--defaults-file=\"{tempIni}\" --initialize-insecure"))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它6");
|
|
||||||
// 5. 启动临时 MySQL(WinExe 不重定向输出)
|
// 5. 启动临时 MySQL(WinExe 不重定向输出)
|
||||||
Process mysqlProcess = new Process();
|
Process mysqlProcess = new Process();
|
||||||
mysqlProcess.StartInfo.FileName = Path.Combine(mysqlBinDir, "mysqld.exe");
|
mysqlProcess.StartInfo.FileName = Path.Combine(mysqlBinDir, "mysqld.exe");
|
||||||
mysqlProcess.StartInfo.Arguments = $"--defaults-file=\"{tempIni}\" --standalone --port={port}";
|
mysqlProcess.StartInfo.Arguments = $"--defaults-file=\"{tempIni}\" --standalone --port={port}";
|
||||||
mysqlProcess.StartInfo.UseShellExecute = true;
|
|
||||||
// mysqlProcess.StartInfo.CreateNoWindow = true;
|
// 必须 false 才能重定向输出和隐藏窗口
|
||||||
// mysqlProcess.StartInfo.RedirectStandardOutput = true;
|
mysqlProcess.StartInfo.UseShellExecute = false;
|
||||||
// mysqlProcess.StartInfo.RedirectStandardError = true;
|
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.Start();
|
||||||
|
|
||||||
|
// 开始异步读取输出
|
||||||
mysqlProcess.BeginOutputReadLine();
|
mysqlProcess.BeginOutputReadLine();
|
||||||
mysqlProcess.BeginErrorReadLine();
|
mysqlProcess.BeginErrorReadLine();
|
||||||
|
|
||||||
}
|
}
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 6. 等待 MySQL 可连接(最长等待 30 秒)
|
// 6. 等待 MySQL 可连接(最长等待 30 秒)
|
||||||
string connStr = $"server=127.0.0.1;port={port};user=root;password=;Charset=utf8mb4;";
|
string connStr = $"server=127.0.0.1;port={port};user=root;password=;Charset=utf8mb4;";
|
||||||
@@ -517,6 +508,18 @@ class Program
|
|||||||
procs.Kill();
|
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
|
catch
|
||||||
{
|
{
|
||||||
@@ -997,6 +1000,26 @@ class Program
|
|||||||
|
|
||||||
return null; // 没找到
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user