if ($m != $r) {
$dbMaster = [];
foreach (['username', 'password', 'hostname', 'hostport', 'database', 'dsn', 'charset'] as $name) {
$dbMaster[$name] = isset($_config[$name][$m]) ? $_config[$name][$m] : $_config[$name][0];
}
}
$dbConfig = [];
foreach (['username', 'password', 'hostname', 'hostport', 'database', 'dsn', 'charset'] as $name) {
$dbConfig[$name] = isset($_config[$name][$r]) ? $_config[$name][$r] : $_config[$name][0];
}
return $this->connect($dbConfig, $r, $r == $m ? false : $dbMaster);这里是主从连接的时候,去连接数据库代码,其中最后一行最后一个参数是,主服务器的信息。然后连接数据库里面是 } catch (\PDOException $e) {
if ($autoConnection) {
Log::record($e->getMessage(), 'error');
return $this->connect($autoConnection, $linkNum);
} else {
throw $e;
}
}如果连接失败了,那么取最后一个参数,也就是主数据库的连接信息那么我的理解是:如果说从库没连上,那么读操作会去主库读,是这个意思吗 我只大概看了下Connection.php 这里的代码 理解可能有偏差 有没有官方人员说明下这块?
最佳答案