简体   繁体   中英

Find the windows service name of MySql server instance

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.

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:

# 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.

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

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

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.

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

PS: The entries may can be found here:

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

(xy is the version like: 57 for 5.7)

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

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

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

Read this value and map to the windows service.

In my c# code:

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM