參考文檔:https://www.kancloud.cn/manual/think-orm/1258000 配置如下:
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
// 數據庫部署方式:0 集中式(單一服務器),1 分布式(主從服務器)
'deploy' => 1,
// 數據庫讀寫是否分離 主從式有效(hostname第一個為主,其它為從;)
'rw_separate' => true,
// 讀寫分離后 主服務器數量
'master_num' => 1,
// 指定從服務器序號
//'slave_no' => '',
// 模型寫入后自動讀取主服務器, 等價于 EloquentOrm 的 sticky 參數
'read_master' => false,
// 數據庫類型
'type' => 'mysql',
// 服務器地址
'hostname' => [getenv('DB1_HOST'),getenv('DB2_HOST')],
// 數據庫名
'database' => getenv('DB_NAME'),
// 數據庫用戶名
'username' => getenv('DB_USER'),
// 數據庫密碼
'password' => getenv('DB_PASSWORD'),
// 數據庫連接端口
'hostport' => getenv('DB_PORT'),
// 數據庫連接參數
'params' => [
// 連接超時3秒
\PDO::ATTR_TIMEOUT => 3,
],
// 數據庫編碼默認采用utf8
'charset' => getenv('DB_CHARSET'),
// 數據庫表前綴
'prefix' => '',
// 斷線重連
'break_reconnect' => true,
// 關閉SQL監(jiān)聽日志
'trigger_sql' => false,
// 自定義分頁類
'bootstrap' => ''
],
],
];
如上配置,使用了數組配置了兩個數據庫:
'hostname' => [getenv('DB1_HOST'),getenv('DB2_HOST')],
錯誤:
? php plugin/commerce/scripts/debug.php ─╯
PHP Fatal error: Uncaught ErrorException: Array to string conversion in /Volumes/webman/api/vendor/topthink/think-orm/src/db/PDOConnection.php:338
Stack trace:
#0 /Volumes/webman/api/vendor/topthink/think-orm/src/db/PDOConnection.php(338): {closure}(2, 'Array to string...', '/Volumes/Media/...', 338)
#1 /Volumes/webman/api/vendor/topthink/think-orm/src/db/PDOConnection.php(356): think\db\PDOConnection->getSchemaCacheKey('commercee.custo...')
#2 /Volumes/webman/api/vendor/topthink/think-orm/src/db/PDOConnection.php(411): think\db\PDOConnection->getSchemaInfo('customer_entity')
#3 /Volumes/webman/api/vendor/topthink/think-orm/src/db/PDOConnection.php(461): think\db\PDOConnection->getTableInfo('customer_entity', 'pk')
#4 /Volumes/webman/api/vendor/topthink/think-orm/src/db/Query.php(290): think\db\PDOConnection->getPk('customer_entity')
#5 /Volumes/webman/api/vendor/topthink/think-orm/src/db/BaseQuery.php(1279): think\db\Query->getPk()
#6 /Volumes/webman/api/vendor/topthink/think-orm/src/db/BaseQuery.php(1156): think\db\BaseQuery->parsePkWhere(1)
#7 /Volumes/webman/api/plugin/commerce/scripts/debug.php(8): think\db\BaseQuery->find(1)
#8 {main}
貌似是沒有讀取到 “deploy”分布式配置;請教各位,謝謝;