如何使用PHP的PDO强制创建新的数据库连接以连接到MySQL

时间:2020-12-15 19:15:48

I use PHP to connect to MySQL.

我使用PHP连接到MySQL。

At last, I switched to the PDO interface from the deprecated mysql_! I love it, and it works great, but one lingering issue...

最后,我从弃用的mysql_切换到PDO接口!我喜欢它,它很有效,但是一个挥之不去的问题......

MY QUESTION: Does a new PDO (using the same credentials) always create a new connection to the database?
If that's the case, then I'm all set!
I am aware of the option of "Persistent connections" in PDO; I do NOT use that option.

我的问题:新的PDO(使用相同的凭据)是否始终创建与数据库的新连接?如果是这样的话,那我就定了!我知道PDO中“持久连接”的选项;我不使用那个选项。

With the old mysql_connect() function, I could FORCE a new database connection with the new_link flag. Am I correct in my understanding that with a new PDO, one ALWAYS gets a new database connection? (Unless requesting a "persistent connection" - which, once again, I do NOT do.)

使用旧的mysql_connect()函数,我可以使用new_link标志FORCE一个新的数据库连接。我是否正确理解,使用新的PDO,一个ALWAYS会获得新的数据库连接? (除非要求“持久连接” - 再一次,我不这样做。)

If I understand correctly, PDO is the opposite of mysql_connect in that (assuming identical database credentials) PDO always gives a new connection, unless specified otherwise (i.e. unless requesting a "persistent connection") - WHEREAS by default mysql_connect would give the same old connection, unless you forced a new one.

如果我理解正确,PDO与mysql_connect相反(假设相同的数据库凭证)PDO总是给出一个新连接,除非另有说明(即除非请求“持久连接”) - 默认情况下,mysql_connect会给出相同的旧连接,除非你强迫新的。


Side note: as to WHY I want to force a new connection, it's part of my implementation of a more robust SQL query execution mechanism. I discovered over the years that, when a PHP script is used to serve large files, occasionally a new SQL query gets a lost database connection error ("the database has gone away"); in those cases, my remedy - which worked perfectly for years - has been the following algorithm:
1) try to run the SQL query
2) in case of error, force a new database connection [critical step!] and then re-run the SQL query. It if fails a 2nd time, give up and issue an error/log; but, in most cases, the problem goes away and the 2nd attempt works :)

旁注:至于为什么我想强制建立新连接,这是我实现更强大的SQL查询执行机制的一部分。多年来我发现,当一个PHP脚本用于提供大型文件时,偶尔新的SQL查询会丢失数据库连接错误(“数据库已经消失”);在这些情况下,我的补救措施 - 多年来运行良好 - 一直是以下算法:1)尝试运行SQL查询2)如果出现错误,强制新的数据库连接[关键步骤!]然后重新运行SQL查询。如果第二次失败,放弃并发出错误/日志;但是,在大多数情况下,问题消失了,第二次尝试工作:)

I'm trying to replicate that robust function with PDO... I found excellent guides on the mysql -> PDO switching (such as http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers ), but I'm still hazy whether instantiating a new PDO object implies a new database connection in cases where an earlier PDO object was created with the same credentials.
Thanks!!

我正在尝试用PDO复制那个健壮的函数......我找到了关于mysql的优秀指南 - > PDO切换(例如http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers),但是我仍然朦胧是否实例化在使用相同凭据创建早期PDO对象的情况下,新PDO对象意味着新的数据库连接。谢谢!!

1 个解决方案

#1


0  

Yes it's a new instances of the connection using the credentials given. You can see this with MySQL SHOW FULL PROCESSLIST via MySQL command-line, as it creates each new connection and query, just have to be fast about it or run some slow queries.

是的,它是使用给定凭据的连接的新实例。您可以通过MySQL命令行使用MySQL SHOW FULL PROCESSLIST看到这一点,因为它创建了每个新连接和查询,只需要快速了解它或运行一些慢速查询。

On a side note; running the insert again is a bit dirty workaround to an unoptimized process with the database design and mysql config, I recommend revisiting the items in question and find a better method to the process. Best

在旁注;再次运行插入对于使用数据库设计和mysql配置的未经优化的进程来说是一个有点脏的解决方法,我建议重新访问有问题的项目并找到更好的方法。最好

#1


0  

Yes it's a new instances of the connection using the credentials given. You can see this with MySQL SHOW FULL PROCESSLIST via MySQL command-line, as it creates each new connection and query, just have to be fast about it or run some slow queries.

是的,它是使用给定凭据的连接的新实例。您可以通过MySQL命令行使用MySQL SHOW FULL PROCESSLIST看到这一点,因为它创建了每个新连接和查询,只需要快速了解它或运行一些慢速查询。

On a side note; running the insert again is a bit dirty workaround to an unoptimized process with the database design and mysql config, I recommend revisiting the items in question and find a better method to the process. Best

在旁注;再次运行插入对于使用数据库设计和mysql配置的未经优化的进程来说是一个有点脏的解决方法,我建议重新访问有问题的项目并找到更好的方法。最好