【修改】mysql端口号,临时文件存放地址
This commit is contained in:
@@ -198,7 +198,7 @@ class Program
|
|||||||
{
|
{
|
||||||
if (path == "navicat")
|
if (path == "navicat")
|
||||||
{
|
{
|
||||||
int port = 6033;
|
int port = 48086;
|
||||||
|
|
||||||
// 1. 自动搜索 MySQL bin 目录
|
// 1. 自动搜索 MySQL bin 目录
|
||||||
string[] possiblePaths = new string[]
|
string[] possiblePaths = new string[]
|
||||||
@@ -219,7 +219,7 @@ class Program
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Console.WriteLine("已有 MySQL 正在运行,直接使用它1");
|
||||||
if (mysqlBinDir == null) {
|
if (mysqlBinDir == null) {
|
||||||
mysqlBinDir = FindMySqlBin();
|
mysqlBinDir = FindMySqlBin();
|
||||||
if (mysqlBinDir == null)
|
if (mysqlBinDir == null)
|
||||||
@@ -227,11 +227,11 @@ class Program
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine("已有 MySQL 正在运行,直接使用它2");
|
||||||
// 2. 设置临时数据目录
|
// 2. 设置临时数据目录
|
||||||
string tempData = @"C:\mysql_temp_data";
|
string tempData = @"D:\mysql_temp_data";
|
||||||
string tempIni = @"C:\mysql_temp.ini";
|
string tempIni = @"D:\mysql_temp.ini";
|
||||||
|
|
||||||
int existingPid = GetProcessIdByPort(port);
|
int existingPid = GetProcessIdByPort(port);
|
||||||
if (existingPid > 0)
|
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;");
|
using var conn = new MySqlConnection($"server=127.0.0.1;port={port};user=root;password=;Charset=utf8mb4;");
|
||||||
conn.Open();
|
conn.Open();
|
||||||
Console.WriteLine("已有 MySQL 正在运行,直接使用它");
|
Console.WriteLine("已有 MySQL 正在运行,直接使用它3");
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@@ -249,34 +249,57 @@ class Program
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (Directory.Exists(tempData))
|
Console.WriteLine("已有 MySQL 正在运行,直接使用它4");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (Directory.Exists(tempData))
|
||||||
Directory.Delete(tempData, true);
|
Directory.Delete(tempData, true);
|
||||||
Directory.CreateDirectory(tempData);
|
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("[mysqld]");
|
||||||
writer.WriteLine("skip-networking=0");
|
writer.WriteLine($"datadir={tempData}");
|
||||||
writer.WriteLine("skip-grant-tables=0");
|
writer.WriteLine($"port={port}");
|
||||||
writer.WriteLine($"log-error={tempData}\\mysql.err");
|
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. 初始化临时数据库
|
// 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 正在运行,直接使用它");
|
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 = false;
|
mysqlProcess.StartInfo.UseShellExecute = true;
|
||||||
mysqlProcess.StartInfo.CreateNoWindow = true;
|
// mysqlProcess.StartInfo.CreateNoWindow = true;
|
||||||
mysqlProcess.StartInfo.RedirectStandardOutput = true;
|
// mysqlProcess.StartInfo.RedirectStandardOutput = true;
|
||||||
mysqlProcess.StartInfo.RedirectStandardError = true;
|
// mysqlProcess.StartInfo.RedirectStandardError = true;
|
||||||
|
|
||||||
mysqlProcess.Start();
|
mysqlProcess.Start();
|
||||||
|
|
||||||
@@ -284,12 +307,12 @@ class Program
|
|||||||
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;";
|
||||||
|
Reference in New Issue
Block a user