Delphi:如何检查 Windows 机器上是否安装了 MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9379995/
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
Delphi: How to check if MySQL is installed on windows machine
提问by SimonM
I'm developing an installer for my program for Windows OS.
我正在为我的 Windows 操作系统程序开发安装程序。
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?
如何使用delphi检查MySQL是否已经在计算机上?
采纳答案by Shirish11
Check for this registry entry for MySQL if presentHKEY_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 entriesHKEY_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。
因此,找出正确的服务名称会有点乏味。
回答by da-soft
Regarding MySQL server:
关于 MySQL 服务器:
- 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. Usingmysqld.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. - 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.
- 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.
- 正如@ Shirish11 所说,您可以检查
HKLM\SOFTWARE\MySQL AB
. 但这并不能保证任何事情,因为此注册表项是可选的。MySQL 确实不需要任何“复杂”的安装过程,如 SQL Server。您可以将 MySQL 作为安装程序下载,就像 ZIP 存档一样。在最后一种情况下,不会创建注册表项。与服务类似。使用mysqld.exe --install Kitty --defaults-file=c:\mysql.cfg
命令行,您可以使用 Kitty 名称创建 MySQL 服务。甚至可能根本没有创建服务。 - MySQL 服务器可能不是安装在本地,而是安装在不同的主机上。并由专门的 DBA 进行管理。因此,您可能根本不需要检查/安装服务器。只是不是你的任务。如果您的应用程序将安装一个本地服务器,该服务器将仅由“此”工作站使用,则使用 MySQL Embedded。
- 通常,您应该询问用户有关 MySQL 安装的信息,例如“您是否安装了 MySQL 服务器?”。如果用户回答“是”,那么您可以向他询问登录参数(主机、端口、数据库、用户名、密码)。如果没有,那么您可以建议他下载并安装,以避免许可问题。因为您必须拥有从 Oracle 购买的许可证才能随软件分发 MySQL 服务器安装程序。
Regarding MySQL client:
关于 MySQL 客户端:
- There is no other signs, that
libmysql.dll
is "installed", than thelibmysql.dll
presence. You can check forlibmysql.dll
at Windows folder, atPATH
folders. More simple - you can always put it with your EXE. - 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. - See note (3) regarding server licensing. It applies to client too.
- 除了存在之外,没有其他迹象,即
libmysql.dll
“安装”libmysql.dll
。您可以libmysql.dll
在 Windows 文件夹中检查文件PATH
夹。更简单 - 你可以随时把它放在你的 EXE 中。 - 如果您使用的是 dbExpress,那么您应该使用
libmysql.dll
EMBT dbExpress 驱动程序支持的特定版本。所以,再一次,最好把它放在你的 EXE 中。 - 请参阅有关服务器许可的注释 (3)。它也适用于客户端。
回答by nawfal
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 AB
,即使卸载 MySQL,您也可以保留在注册表中。删除的是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
. 这取决于您在机器上的服务器。