mySQL:测试与查询的连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4957155/
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-08-31 18:42:22  来源:igfitidea点击:

mySQL: Testing connection with query?

mysqlunit-testingtesting

提问by Industrial

I am writing some unit tests to ensure that everything is working as supposed in my application and thought it would be an good idea to write a short test script to ensure that the mySQL connection is working as intended.

我正在编写一些单元测试以确保我的应用程序中的一切都按预期工作,并认为编写一个简短的测试脚本以确保 mySQL 连接按预期工作是个好主意。

Is there any query I can run that will always output something sweet that I can verify the connection upon, without having to think about eventual stored data in the mySQL database?

是否有任何我可以运行的查询总是会输出一些我可以验证连接的东西,而不必考虑最终存储在 mySQL 数据库中的数据?

回答by a_horse_with_no_name

is there any query I can run that will always output something sweet

是否有任何我可以运行的查询总是会输出一些甜蜜的东西

This should do it

这应该做

SELECT 'Something sweet'

Edit
If you don't want something sweet you can always use the built-in functions:

编辑
如果你不想要甜的东西,你可以随时使用内置函数:

SELECT version()

for more ideas check out the manual:
http://dev.mysql.com/doc/refman/5.1/en/information-functions.html

有关更多想法,请查看手册:http:
//dev.mysql.com/doc/refman/5.1/en/information-functions.html

回答by yarson

To get more details you can also use SHOW statement:

要获取更多详细信息,您还可以使用 SHOW 语句:

SHOW VARIABLES LIKE 'version%';

+---------------------------------+---------------------------+
| Variable_name                   | Value                     |
+---------------------------------+---------------------------+
| version                         | 5.1.6-alpha-log           |
| version_comment                 | Source distribution       |
| version_compile_machine         | i686                      |
| version_compile_os              | suse-linux                |
+---------------------------------+---------------------------+

http://dev.mysql.com/doc/refman/5.1/en/show-variables.html

http://dev.mysql.com/doc/refman/5.1/en/show-variables.html

回答by hpavc

Most db drivers have a ping() method where they have a mechanism that does exactly what your peers are suggesting.

大多数数据库驱动程序都有一个 ping() 方法,其中它们有一种机制可以完全按照您的同行建议的方式执行。

However show variables and selecting from nothing doesnt expose anything health wise except the database engine, storage could be down, indexes could be corrupt, errors everywhere.

然而,显示变量和从无中选择不会暴露任何健康明智的东西,除了数据库引擎,存储可能会关闭,索引可能会损坏,到处都是错误。