gpt4 book ai didi

php - 数据库复制 : intelligent database selection within PHP?

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:31 25 4
gpt4 key购买 nike

我将 PHP 与复制的 MySQL 数据库一起使用。

我想做一些数据库负载平衡。目前我有相当粗糙的:

$ran = rand(1,10);
if ($ran < 5) {
$db = '10.0.0.2:3306';
} else {
$db = '10.0.0.3:3306';
}
// connect to the database
$con = mysql_connect($db,'elastic1','oag4Chai')
or die('Could not connect to the server!');

但很明显,如果其中一个数据库出现故障,那么我们的网页将有一半时间显示错误消息。

是否有更可靠的方法来执行此操作 - 即在连接之前检查数据库是否已启动?

我猜它可能只是一个 try/catch 语句,或者我可以做一些更复杂的负载平衡 - 非常感谢收到的建议。

最佳答案

显示错误的不是网页,而是您

die('Could not connect to the server!')

die() 现在与“真实代码”无关。

$ran = rand(1,10);
if ($ran < 5) {
$db = '10.0.0.2:3306';
} else {
$db = '10.0.0.3:3306';
}
// connect to the database
$con = mysql_connect($db,'elastic1','oag4Chai');
if (!$con) { /* Connect to the other one */ }

关于php - 数据库复制 : intelligent database selection within PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8233474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com