【修改】mysql端口号,临时文件存放地址

This commit is contained in:
huababa1
2025-08-21 16:17:42 +08:00
parent 19ecdde64d
commit f5007c49fe

View File

@@ -198,7 +198,7 @@ class Program
{
if (path == "navicat")
{
int port = 6033;
int port = 48086;
// 1. 自动搜索 MySQL bin 目录
string[] possiblePaths = new string[]
@@ -219,7 +219,7 @@ class Program
break;
}
}
Console.WriteLine("已有 MySQL 正在运行直接使用它1");
if (mysqlBinDir == null) {
mysqlBinDir = FindMySqlBin();
if (mysqlBinDir == null)
@@ -227,11 +227,11 @@ class Program
return;
}
}
Console.WriteLine("已有 MySQL 正在运行直接使用它2");
// 2. 设置临时数据目录
string tempData = @"C:\mysql_temp_data";
string tempIni = @"C:\mysql_temp.ini";
string tempData = @"D:\mysql_temp_data";
string tempIni = @"D:\mysql_temp.ini";
int existingPid = GetProcessIdByPort(port);
if (existingPid > 0)
@@ -240,7 +240,7 @@ class Program
{
using var conn = new MySqlConnection($"server=127.0.0.1;port={port};user=root;password=;Charset=utf8mb4;");
conn.Open();
Console.WriteLine("已有 MySQL 正在运行,直接使用它");
Console.WriteLine("已有 MySQL 正在运行,直接使用它3");
}
catch
{
@@ -249,34 +249,57 @@ class Program
}
else
{
if (Directory.Exists(tempData))
Console.WriteLine("已有 MySQL 正在运行直接使用它4");
try
{
if (Directory.Exists(tempData))
Directory.Delete(tempData, true);
Directory.CreateDirectory(tempData);
// 3. 生成临时 my.ini
using (StreamWriter writer = new StreamWriter(tempIni))
// 如果已有 ini 文件,先删掉
if (File.Exists(tempIni))
{
File.Delete(tempIni);
}
// 3. 生成临时 my.ini
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");
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 正在运行,直接使用它");
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 = false;
mysqlProcess.StartInfo.CreateNoWindow = true;
mysqlProcess.StartInfo.RedirectStandardOutput = true;
mysqlProcess.StartInfo.RedirectStandardError = true;
mysqlProcess.StartInfo.UseShellExecute = true;
// mysqlProcess.StartInfo.CreateNoWindow = true;
// mysqlProcess.StartInfo.RedirectStandardOutput = true;
// mysqlProcess.StartInfo.RedirectStandardError = true;
mysqlProcess.Start();
@@ -284,12 +307,12 @@ class Program
mysqlProcess.BeginErrorReadLine();
}
Console.WriteLine("已有 MySQL 正在运行,直接使用它");
// 6. 等待 MySQL 可连接(最长等待 30 秒)
string connStr = $"server=127.0.0.1;port={port};user=root;password=;Charset=utf8mb4;";