windows 找到MySql服务器实例的windows服务名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8513587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:39:47  来源:igfitidea点击:

Find the windows service name of MySql server instance

mysqlwindows

提问by Shoar

The default name of windows service which holds MySQL server instance is 'MySQL'. But MySQL 5.5 allows user to specify any name for it after installation. Now I need to know the name of MySQL windows service but I can't understand where my application should look for this name. I was trying to specially make this name unusual and then find where MySQL stored it in the system. But this unusual name was only in the services list of windows registry. my.ini, my-huge.ini, and other ini files of MySQL server does not contain information about how it's windows service is named. So where MySQL stores this name? Windows version: 7 X86/X64, MySQL version: 5.5. Thanks in advance.

保存 MySQL 服务器实例的 Windows 服务的默认名称是“MySQL”。但是 MySQL 5.5 允许用户在安装后为其指定任何名称。现在我需要知道 MySQL windows 服务的名称,但我不明白我的应用程序应该在哪里查找这个名称。我试图特地让这个名字与众不同,然后找到 MySQL 在系统中存储它的位置。但这个不寻常的名字只出现在 windows 注册表的服务列表中。my.ini、my-huge.ini 和 MySQL 服务器的其他 ini 文件不包含有关其 windows 服务如何命名的信息。那么 MySQL 在哪里存储这个名字呢?Windows 版本:7 X86/X64,MySQL 版本:5.5。提前致谢。

回答by Rigo M

New to MySQL, I ran into the same question. What I've done to workaround this is set the instance shared_memory_base_name to the same as the service name in the ini:

刚接触 MySQL,我遇到了同样的问题。我为解决此问题所做的是将实例 shared_memory_base_name 设置为与 ini 中的服务名称相同:

# shared_memory
shared_memory_base_name=MySQLServiceName

This make it available via the system variable @@shared_memory_base_name. I don't actually enable the shared-memory connections, just set the variable.

这使它通过系统变量@@shared_memory_base_name 可用。我实际上并没有启用共享内存连接,只是设置了变量。

I haven't tried it on Linux, though maybe setting the @@socket system variable could provide the same.

我还没有在 Linux 上尝试过,但也许设置 @@socket 系统变量可以提供相同的结果。

I realize this is a bit late to help you now, but someone may find it useful...

我意识到现在帮助你有点晚了,但有人可能会觉得它很有用......

回答by Paddy

It's stored in the registry. The MySQL Instance Configuration Wizard reads the registry which contains the name and also the path to the corresponded service .exe.

它存储在注册表中。MySQL 实例配置向导读取包含名称和对应服务 .exe 路径的注册表。

But I don't exactly know how to find these registry entries programmatically.

但我不完全知道如何以编程方式找到这些注册表项。

PS: The entries may can be found here:

PS:可以在这里找到条目:

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL[xy]

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL[xy]

(xy is the version like: 57 for 5.7)

(xy 是这样的版本:57 表示 5.7)

The ServiceName is stored here in the key "DisplayName" and the path to the .exe in "ImagePath".

ServiceName 存储在此处的键“DisplayName”和“ImagePath”中的 .exe 路径中。

Note: These path may contains start parameters like the path to the my.ini file also.

注意:这些路径也可能包含启动参数,例如 my.ini 文件的路径。

回答by Ivan Gusev

MySql variable @@pid_file contains path to the file contains proccess id of mysql instanse.

MySql 变量@@pid_file 包含文件的路径,该文件包含 mysql 实例的进程 ID。

Read this value and map to the windows service.

读取此值并映射到 windows 服务。

In my c# code:

在我的 C# 代码中:

public static string GetServiceNameFromPid(uint pid)
{
    string result = null;
    var searcher = new System.Management.ManagementObjectSearcher($"SELECT NAME FROM WIN32_SERVICE WHERE PROCESSID = {pid}");

    foreach (var managementBaseObject in searcher.Get()){
        if (result != null){
            throw new InvalidOperationException("PID correspond two or above service name");
        }
        result =(string) managementBaseObject["NAME"];
    }
    return result;
}

回答by Jim Garrison

MySQL does not need to store this name anywhere. The server process gets launched by Windows service controller, and the client communicates with the server via the network, so it does not need to know the name either. As you discovered, the only place this will appear is in the services list in the registry. Consider that on Linux, daemon processes don't have special names the way Windows services do. The service name is an artifact of running on Windows, and MySQL doesn't know or care about it.

MySQL 不需要将此名称存储在任何地方。服务器进程由 Windows 服务控制器启动,客户端通过网络与服务器通信,因此它也不需要知道名称。正如您所发现的,它只会出现在注册表的服务列表中。考虑到在 Linux 上,守护进程没有 Windows 服务那样的特殊名称。服务名是运行在Windows上的神器,MySQL不知道也不关心。