Delphi:如何检查Windows机器上是否安装了MySQL

时间:2022-06-27 07:12:47

I'm developing an installer for my program for Windows OS.

我正在为我的Windows OS程序开发一个安装程序。

I'd like the installer to check if MySQL is already installed and if it isn't silently install MySQL before installation is over.

我希望安装程序检查MySQL是否已经安装,以及在安装结束之前是否默认安装MySQL。

How can I check if MySQL is already on the computer with delphi?

如何检查MySQL是否已经在使用delphi的计算机上?

3 个解决方案

#1


5  

Check for this registry entry for MySQL if present
HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB
If you have MySQL installed then this entry should be present.

如果存在,请检查MySQL的此注册表项HKEY_LOCAL_MACHINE \ SOFTWARE \ MySQL AB如果安装了MySQL,则应该存在此条目。

If you need some thing more, then check for the service if its present

如果你需要更多的东西,那么检查服务是否存在

    function ServiceIsPresent(sMachine, sService: PChar): Boolean;
    var
      SCManHandle, SvcHandle: SC_Handle;
    begin
      // Open service manager handle.
      SCManHandle := OpenSCManager(sMachine, nil, SC_MANAGER_CONNECT);
      if (SCManHandle > 0) then
      begin
        SvcHandle := OpenService(SCManHandle, sService, SERVICE_QUERY_STATUS);
        // if Service installed
        if (SvcHandle > 0) then
          begin   
            Result := True;
            CloseServiceHandle(SvcHandle);
          end;
        else
            Result := False; 
        CloseServiceHandle(SCManHandle);
      end;
    end;

function call

功能调用

  ServiceGetStatus(nil,'MySQL');

I have picked up this service name from my registry entries
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services
But be careful while using service check because some people might have different service names for MySQL service.
As in my case I have 2 installations of MySQL hence 2 services are present MySQL,MySQL501.
Hence it will be bit tedious to find out the correct service name.

我从我的注册表项HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services中选择了这个服务名称但是在使用服务检查时要小心,因为有些人可能有不同的MySQL服务服务名称。在我的情况下,我有2个MySQL安装,因此2个服务存在MySQL,MySQL501。因此找出正确的服务名称会有点乏味。

#2


7  

Regarding MySQL server:

关于MySQL服务器:

  1. As @Shirish11 sayd, you can check HKLM\SOFTWARE\MySQL AB. But that does not guarantee anything, as this registry key is optional. MySQL really does not require any "complex" installation procedure, like SQL Server. You can download MySQL as an installer, as just a ZIP archive. In last case the registry key will be not created. The similar with services. Using mysqld.exe --install Kitty --defaults-file=c:\mysql.cfg command line you can create MySQL service with Kitty name. Or even a service may be not created at all.
  2. 正如@ Shirish11所说,您可以查看HKLM \ SOFTWARE \ MySQL AB。但这并不保证任何内容,因为此注册表项是可选的。 MySQL确实不需要任何“复杂”的安装过程,比如SQL Server。您可以将MySQL作为安装程序下载,就像ZIP存档一样。在最后一种情况下,将不会创建注册表项。与服务类似。使用mysqld.exe --install Kitty --defaults-file = c:\ mysql.cfg命令行,您可以使用Kitty名称创建MySQL服务。甚至可能根本不创建服务。
  3. The MySQL server may be installed not locally, but on a different host. And be administrated by a dedicated DBA. So, you may not need to check / install server at all. Just not your task. If your application is going to install a local server, which will be used only by "this" workstation, then use MySQL Embedded.
  4. MySQL服务器可能不是本地安装,而是安装在不同的主机上。并由专门的DBA管理。因此,您可能根本不需要检查/安装服务器。不是你的任务。如果您的应用程序要安装仅由“this”工作站使用的本地服务器,则使用MySQL Embedded。
  5. In general, you should ask the user about MySQL installation, eg "Do you have MySQL server installed ?". And if user answers "yes", then you can ask him for login parameters (host, port, database, user name, password). If not, then you can suggest him to download it and install, to avoid licensing issues. Because you have to have a license, purchased from Oracle, to distribute the MySQL Server installer with your software.
  6. 通常,您应该询问用户有关MySQL安装的信息,例如“您是否安装了MySQL服务器?”。如果用户回答“是”,那么您可以询问他的登录参数(主机,端口,数据库,用户名,密码)。如果没有,那么您可以建议他下载并安装,以避免许可问题。因为您必须拥有从Oracle购买的许可证,才能使用您的软件分发MySQL Server安装程序。

Regarding MySQL client:

关于MySQL客户端:

  1. There is no other signs, that libmysql.dll is "installed", than the libmysql.dll presence. You can check for libmysql.dll at Windows folder, at PATH folders. More simple - you can always put it with your EXE.
  2. 没有其他迹象表明libmysql.dll是“已安装”的,而不是libmysql.dll的存在。您可以在Windows文件夹,PATH文件夹中检查libmysql.dll。更简单 - 您可以随身携带EXE。
  3. If you are using dbExpress, then you should use specific libmysql.dll version, supported by EMBT dbExpress driver. So, again, better to put it with your EXE.
  4. 如果您使用的是dbExpress,那么您应该使用EMBT dbExpress驱动程序支持的特定libmysql.dll版本。所以,再次,最好把它与你的EXE。
  5. See note (3) regarding server licensing. It applies to client too.
  6. 请参阅有关服务器许可的注释(3)。它也适用于客户。

#3


0  

Mind you HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB can remain in registry even on uninstall of MySQL. What gets removed is HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB\MySQL Server 5.1 but since we do not know the MySQL Server 5.1 part in advance, its not easy to go for this approach. Moreover MySQL need not be installed as such.

请注意HKEY_LOCAL_MACHINE \ SOFTWARE \即使卸载MySQL,MySQL AB也可以保留在注册表中。删除的是HKEY_LOCAL_MACHINE \ SOFTWARE \ MySQL AB \ MySQL Server 5.1,但由于我们事先不知道MySQL Server 5.1部分,因此不容易采用这种方法。此外,MySQL不需要像这样安装。

What is possible is to check HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MySQL\ImagePath and then see if the path specified by the value exists. The second part is important because MySQL need not remove the service key during uninstallation if service is already turned off. So this registry value can remain even after uninstallation. So the best option is to get the path of the executable from the above mentioned registry and then see if the mysqld.exe or (whatever it is) exists. MySQL is not installed if either the registry value doesn't exist or if the file doesnt exist. The catch here is that this logic fails if MySQL is installed but not configured to start its service (which means there is no key HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MySQL). But thats trivial, since running MySQL installations will anyway have it and most machines will have MySQL running. Now even if the installed MySQL is not configured to have a service, its quite okay to install another version of your MySQL and register your service first.

可能的是检查HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ MySQL \ ImagePath,然后查看该值指定的路径是否存在。第二部分很重要,因为如果服务已经关闭,MySQL在卸载过程中无需删除服务密钥。因此,即使在卸载后,此注册表值仍可保留。因此,最好的选择是从上面提到的注册表中获取可执行文件的路径,然后查看mysqld.exe或(无论它是否存在)。如果注册表值不存在或文件不存在,则不安装MySQL。这里的问题是,如果MySQL已安装但未配置为启动其服务,则此逻辑将失败(这意味着没有密钥HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ MySQL)。但这是微不足道的,因为运行MySQL安装无论如何都会拥有它并且大多数机器都将运行MySQL。现在即使安装的MySQL没有配置为具有服务,也可以安装另一个版本的MySQL并首先注册您的服务。

I'm unsure of a perfect solution, the reason being MySQL being not a proprietary software. I mean anyone can bundle MySQL and install in their own flavours. For example, wampserver comes with their own MySQL. Client machines most certainly wont have servers installed that way, but we can not rule out the option and we may not want another MySQL instance (Note that a separate instance of MySQL runs fine along with MySQL in wamp server). What I do is check the above, if I did not find, I check for the image path of wamp service, like HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\wampmysqld\ImagePath. It depends on the server you have on a machine.

我不确定一个完美的解决方案,原因是MySQL不是专有软件。我的意思是任何人都可以捆绑MySQL并安装自己的风格。例如,wampserver附带了自己的MySQL。客户端机器肯定不会以这种方式安装服务器,但我们不能排除该选项,我们可能不需要另一个MySQL实例(请注意,单独的MySQL实例与wamp服务器中的MySQL一起运行良好)。我做的是检查上面,如果我没有找到,我检查wamp服务的图像路径,如HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ wampmysqld \ ImagePath。这取决于您在计算机上的服务器。

#1


5  

Check for this registry entry for MySQL if present
HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB
If you have MySQL installed then this entry should be present.

如果存在,请检查MySQL的此注册表项HKEY_LOCAL_MACHINE \ SOFTWARE \ MySQL AB如果安装了MySQL,则应该存在此条目。

If you need some thing more, then check for the service if its present

如果你需要更多的东西,那么检查服务是否存在

    function ServiceIsPresent(sMachine, sService: PChar): Boolean;
    var
      SCManHandle, SvcHandle: SC_Handle;
    begin
      // Open service manager handle.
      SCManHandle := OpenSCManager(sMachine, nil, SC_MANAGER_CONNECT);
      if (SCManHandle > 0) then
      begin
        SvcHandle := OpenService(SCManHandle, sService, SERVICE_QUERY_STATUS);
        // if Service installed
        if (SvcHandle > 0) then
          begin   
            Result := True;
            CloseServiceHandle(SvcHandle);
          end;
        else
            Result := False; 
        CloseServiceHandle(SCManHandle);
      end;
    end;

function call

功能调用

  ServiceGetStatus(nil,'MySQL');

I have picked up this service name from my registry entries
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services
But be careful while using service check because some people might have different service names for MySQL service.
As in my case I have 2 installations of MySQL hence 2 services are present MySQL,MySQL501.
Hence it will be bit tedious to find out the correct service name.

我从我的注册表项HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services中选择了这个服务名称但是在使用服务检查时要小心,因为有些人可能有不同的MySQL服务服务名称。在我的情况下,我有2个MySQL安装,因此2个服务存在MySQL,MySQL501。因此找出正确的服务名称会有点乏味。

#2


7  

Regarding MySQL server:

关于MySQL服务器:

  1. As @Shirish11 sayd, you can check HKLM\SOFTWARE\MySQL AB. But that does not guarantee anything, as this registry key is optional. MySQL really does not require any "complex" installation procedure, like SQL Server. You can download MySQL as an installer, as just a ZIP archive. In last case the registry key will be not created. The similar with services. Using mysqld.exe --install Kitty --defaults-file=c:\mysql.cfg command line you can create MySQL service with Kitty name. Or even a service may be not created at all.
  2. 正如@ Shirish11所说,您可以查看HKLM \ SOFTWARE \ MySQL AB。但这并不保证任何内容,因为此注册表项是可选的。 MySQL确实不需要任何“复杂”的安装过程,比如SQL Server。您可以将MySQL作为安装程序下载,就像ZIP存档一样。在最后一种情况下,将不会创建注册表项。与服务类似。使用mysqld.exe --install Kitty --defaults-file = c:\ mysql.cfg命令行,您可以使用Kitty名称创建MySQL服务。甚至可能根本不创建服务。
  3. The MySQL server may be installed not locally, but on a different host. And be administrated by a dedicated DBA. So, you may not need to check / install server at all. Just not your task. If your application is going to install a local server, which will be used only by "this" workstation, then use MySQL Embedded.
  4. MySQL服务器可能不是本地安装,而是安装在不同的主机上。并由专门的DBA管理。因此,您可能根本不需要检查/安装服务器。不是你的任务。如果您的应用程序要安装仅由“this”工作站使用的本地服务器,则使用MySQL Embedded。
  5. In general, you should ask the user about MySQL installation, eg "Do you have MySQL server installed ?". And if user answers "yes", then you can ask him for login parameters (host, port, database, user name, password). If not, then you can suggest him to download it and install, to avoid licensing issues. Because you have to have a license, purchased from Oracle, to distribute the MySQL Server installer with your software.
  6. 通常,您应该询问用户有关MySQL安装的信息,例如“您是否安装了MySQL服务器?”。如果用户回答“是”,那么您可以询问他的登录参数(主机,端口,数据库,用户名,密码)。如果没有,那么您可以建议他下载并安装,以避免许可问题。因为您必须拥有从Oracle购买的许可证,才能使用您的软件分发MySQL Server安装程序。

Regarding MySQL client:

关于MySQL客户端:

  1. There is no other signs, that libmysql.dll is "installed", than the libmysql.dll presence. You can check for libmysql.dll at Windows folder, at PATH folders. More simple - you can always put it with your EXE.
  2. 没有其他迹象表明libmysql.dll是“已安装”的,而不是libmysql.dll的存在。您可以在Windows文件夹,PATH文件夹中检查libmysql.dll。更简单 - 您可以随身携带EXE。
  3. If you are using dbExpress, then you should use specific libmysql.dll version, supported by EMBT dbExpress driver. So, again, better to put it with your EXE.
  4. 如果您使用的是dbExpress,那么您应该使用EMBT dbExpress驱动程序支持的特定libmysql.dll版本。所以,再次,最好把它与你的EXE。
  5. See note (3) regarding server licensing. It applies to client too.
  6. 请参阅有关服务器许可的注释(3)。它也适用于客户。

#3


0  

Mind you HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB can remain in registry even on uninstall of MySQL. What gets removed is HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB\MySQL Server 5.1 but since we do not know the MySQL Server 5.1 part in advance, its not easy to go for this approach. Moreover MySQL need not be installed as such.

请注意HKEY_LOCAL_MACHINE \ SOFTWARE \即使卸载MySQL,MySQL AB也可以保留在注册表中。删除的是HKEY_LOCAL_MACHINE \ SOFTWARE \ MySQL AB \ MySQL Server 5.1,但由于我们事先不知道MySQL Server 5.1部分,因此不容易采用这种方法。此外,MySQL不需要像这样安装。

What is possible is to check HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MySQL\ImagePath and then see if the path specified by the value exists. The second part is important because MySQL need not remove the service key during uninstallation if service is already turned off. So this registry value can remain even after uninstallation. So the best option is to get the path of the executable from the above mentioned registry and then see if the mysqld.exe or (whatever it is) exists. MySQL is not installed if either the registry value doesn't exist or if the file doesnt exist. The catch here is that this logic fails if MySQL is installed but not configured to start its service (which means there is no key HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MySQL). But thats trivial, since running MySQL installations will anyway have it and most machines will have MySQL running. Now even if the installed MySQL is not configured to have a service, its quite okay to install another version of your MySQL and register your service first.

可能的是检查HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ MySQL \ ImagePath,然后查看该值指定的路径是否存在。第二部分很重要,因为如果服务已经关闭,MySQL在卸载过程中无需删除服务密钥。因此,即使在卸载后,此注册表值仍可保留。因此,最好的选择是从上面提到的注册表中获取可执行文件的路径,然后查看mysqld.exe或(无论它是否存在)。如果注册表值不存在或文件不存在,则不安装MySQL。这里的问题是,如果MySQL已安装但未配置为启动其服务,则此逻辑将失败(这意味着没有密钥HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ MySQL)。但这是微不足道的,因为运行MySQL安装无论如何都会拥有它并且大多数机器都将运行MySQL。现在即使安装的MySQL没有配置为具有服务,也可以安装另一个版本的MySQL并首先注册您的服务。

I'm unsure of a perfect solution, the reason being MySQL being not a proprietary software. I mean anyone can bundle MySQL and install in their own flavours. For example, wampserver comes with their own MySQL. Client machines most certainly wont have servers installed that way, but we can not rule out the option and we may not want another MySQL instance (Note that a separate instance of MySQL runs fine along with MySQL in wamp server). What I do is check the above, if I did not find, I check for the image path of wamp service, like HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\wampmysqld\ImagePath. It depends on the server you have on a machine.

我不确定一个完美的解决方案,原因是MySQL不是专有软件。我的意思是任何人都可以捆绑MySQL并安装自己的风格。例如,wampserver附带了自己的MySQL。客户端机器肯定不会以这种方式安装服务器,但我们不能排除该选项,我们可能不需要另一个MySQL实例(请注意,单独的MySQL实例与wamp服务器中的MySQL一起运行良好)。我做的是检查上面,如果我没有找到,我检查wamp服务的图像路径,如HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ wampmysqld \ ImagePath。这取决于您在计算机上的服务器。