在webman中使用thinkorm组件配置主从多个数据库,连接错误

9527

配置thinorm主从数据库,读写异常

参考文档: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监听日志
            '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”分布式配置;请教各位,谢谢;

986 1 0
1个回答

鲁达

hostname 换成字符串格式,用逗号隔开试试

  • 9527 2023-03-12

    经过测试,hostname 更换成字符串逗号分隔可以正常工作;
    但是换成数组不可以;

年代过于久远,无法发表回答
🔝