MySql服务初始化、安装、启动
时间:2021-07-01 10:21:17
帮助过:1人阅读
<summary>
/// 安装并开启服务
/// </summary>
public static bool InitAndStartService(
string serviceName,
string mysqliniPath,
string mysqlPath,
string mysqlBinPath,
string mysqlDataPath)
{
try
{
A_InitDataBase(mysqlBinPath);
B_ResetMySqlConfig(mysqliniPath, mysqlPath, mysqlDataPath);
C_InstallService(mysqlBinPath, serviceName, mysqliniPath);
D_StartService(serviceName);
E_SetDefaultPassword(mysqliniPath, api.MySqlPassword);
}
catch (Exception ex) { api.logger.addToLog(ex,
"InitAndStartService方法出错!");
return false; }
return true;
}
/// <summary>
/// 初始化数据库,如果已经发生过初始化,则不会再初始化,注意,初始化完成后是空密码。
/// </summary>
/// <param name="mysqlBinPath"></param>
/// <returns></returns>
public static void A_InitDataBase(
string mysqlBinPath)
{
if (!
Directory.Exists(api.MysqlDataPath))
{
try { Directory.CreateDirectory(api.MysqlDataPath); }
catch { }
}
//新建初始数据库
var log = exec.RunCmd(mysqlBinPath,
"mysqld --initialize-insecure --console").Trim();
//--user=mysql
api.logger.addToLog(
"A_InitDataBase方法日志:\r\n" +
log);
}
/// <summary>
/// 配置数据库ini文件,在初始化之后,启动服务之前调用。
/// </summary>
public static void B_ResetMySqlConfig(
string mysqlIniPath,
string mysqlPath,
string mysqlDataPath)
{
IniHelper ini =
new IniHelper(mysqlIniPath);
ini.Write("mysqld",
"basedir", (mysqlPath +
"").TrimEnd(
‘\\‘));
ini.Write("mysqld",
"datadir", (mysqlDataPath +
"").TrimEnd(
‘\\‘));
}
/// <summary>
/// 安装服务,会执行卸载再装。
/// </summary>
public static void C_InstallService(
string mysqlBinPath,
string serviceName,
string mysqliniPath)
{
exec.RunCmd(mysqlBinPath, string.Format(
"mysqld.exe -remove {0}", serviceName));
var log = exec.RunCmd(mysqlBinPath,
string.Format(
"mysqld.exe -install {0} --defaults-file=\"{1}\"", serviceName, mysqliniPath));
api.logger.addToLog("C_InstallService方法日志:\r\n" +
log);
}
/// <summary>
/// 启动服务
/// </summary>
public static bool D_StartService(
string serviceName)
{
//return Cocon90.Lib.Util.Window.Service.ServiceHelper.StartService(serviceName, TimeSpan.FromSeconds(10));//开启服务
try
{
ServiceController[] services =
ServiceController.GetServices();
ServiceController service = services.Where(p => p.ServiceName ==
serviceName).FirstOrDefault();
if (service ==
null)
return false;
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
return service.Status ==
ServiceControllerStatus.Running;
}
catch (Exception ex) { api.logger.addToLog(ex,
"D_StartService方法出错:");
return false; }
}
/// <summary>
/// 设置初始密码。将空密码修改为指定密码。
/// </summary>
public static bool E_SetDefaultPassword(
string mysqliniPath,
string newPassword)
{
var port =
GetDbPort(mysqliniPath);
return sql.SetDefaultPassword(port,
"", newPassword);
}
/// <summary>
/// 检测数据库是否存在(或者已初始化过)
/// </summary>
public static bool GetIsDbFileExisted(
string mysqlDataPath)
{
return !(!Directory.Exists(mysqlDataPath) || Directory.GetFiles(mysqlDataPath).Length ==
0 || !Directory.Exists(Path.Combine(mysqlDataPath,
"mysql")));
//检查目录下有无文件,或数据目录下有无mysql文件夹
//if (ifNotExistIsInit)
//{
// //新建初始数据库
// string log = exec.RunCmd(mysqlBinPath, "mysqld --initialize-insecure --user=mysql --console").Trim();
// if (log.Length > 12)
// {
// //获得初始密码
// initPassword = log.Substring(log.Length - 12, 12);
// }
//}
}
/// <summary>
/// 获取当前端口
/// </summary>
public static string GetDbPort(
string mysqlIniPath)
{
IniHelper ini =
new IniHelper(mysqlIniPath);
return ini.Read(
"mysqld",
"port");
}
public static bool SetDefaultPassword(object port, object initPassword, string newPassword)
{
string connStr = string.Format("server=localhost;user=root;port={0};password=‘{1}‘;logging=true;", port, initPassword);
MySqlConnection conn = new MySqlConnection(connStr);
try
{
conn.Open();
var command = new MySqlCommand(string.Format("alter user ‘root‘@‘localhost‘ identified by ‘{0}‘;", newPassword), conn);
var eff = command.ExecuteNonQuery();
conn.Close();
return true;
}
catch (Exception ex)
{
conn.Close();
return false;
}
}
MySql服务初始化、安装、启动
标签: