建議ThinkOrm.php中的定時器,增加對配置項“default”限制。
否則,如果設(shè)置'default' => 'mongo',只能注釋掉mysql配置項,不然會報錯。
======================= 問題更新 ==========================
think-orm的配置文件如下(代碼二)所示,數(shù)據(jù)庫默認(rèn)采用mongo,此時,并未使用到mysql,所以mysql的配置項不一定是正確的,但又不想注釋或刪掉mysql配置。而ThinkOrm.php并未判斷當(dāng)前默認(rèn)使用的是哪個數(shù)據(jù)庫,導(dǎo)致只要有mysql配置項就去連接。
建議將/vendor/webman/think-orm/src/ThinkOrm.php里代碼改成如下(代碼一)所示
<?php
namespace Webman\ThinkOrm;
use Webman\Bootstrap;
use Workerman\Timer;
use think\facade\Db;
class ThinkOrm implements Bootstrap
{
// 進程啟動時調(diào)用
public static function start($worker)
{
// 配置
Db::setConfig(config('thinkorm'));
// 維持mysql心跳
if ($worker) {
Timer::add(55, function () {
$default = config('thinkorm.default', '');
$connections = config('thinkorm.connections', []);
if($connections[$default]['type'] == 'mysql') {
Db::connect($default)->query('select 1');
}
});
}
}
}
<?php
return [
'default' => 'mongo',
'connections' => [
'mysql' => [
// 數(shù)據(jù)庫類型
'type' => 'mysql',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫名
'database' => 'test',
// 數(shù)據(jù)庫用戶名
'username' => 'root',
// 數(shù)據(jù)庫密碼
'password' => '123456',
// 數(shù)據(jù)庫連接端口
'hostport' => '3306',
// 數(shù)據(jù)庫連接參數(shù)
'params' => [],
// 數(shù)據(jù)庫編碼默認(rèn)采用utf8
'charset' => 'utf8',
// 數(shù)據(jù)庫表前綴
'prefix' => '',
// 斷線重連
'break_reconnect' => true,
// 關(guān)閉SQL監(jiān)聽日志
'trigger_sql' => false,
],
'mongo' => [
// 數(shù)據(jù)庫類型
'type' => 'mongo',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫名
'database' => 'test',
// 是否是復(fù)制集
'is_replica_set' => false,
// 用戶名
'username' => '',
// 密碼
'password' => '',
// 端口
'hostport' => '27017',
// 連接dsn
'dsn' => '',
// 數(shù)據(jù)庫連接參數(shù)
'params' => [],
// 數(shù)據(jù)庫編碼默認(rèn)采用utf8
'charset' => 'utf8',
// 主鍵名
'pk' => '_id',
// 主鍵類型
'pk_type' => 'ObjectID',
// 數(shù)據(jù)庫表前綴
'prefix' => 'my_',
// 數(shù)據(jù)庫部署方式:0 集中式(單一服務(wù)器),1 分布式(主從服務(wù)器)
'deploy' => 0,
// 數(shù)據(jù)庫讀寫是否分離 主從式有效
'rw_separate' => false,
// 讀寫分離后 主服務(wù)器數(shù)量
'master_num' => 1,
// 指定從服務(wù)器序號
'slave_no' => '',
// 是否嚴(yán)格檢查字段是否存在
'fields_strict' => false,
// 開啟字段緩存
'fields_cache' => false,
// 監(jiān)聽SQL
'trigger_sql' => true,
// 自動寫入時間戳字段
'auto_timestamp' => false,
// 時間字段取出后的默認(rèn)時間格式
'datetime_format' => 'Y-m-d H:i:s',
// 是否_id轉(zhuǎn)換為id
'pk_convert_id' => true,
// typeMap
'type_map' => ['root' => 'array', 'document' => 'array'],
],
],
];
沒看明白你說的是什么問題,你說的 “增加對配置項“default”限制” 是在哪個文件哪個位置加?報錯是具體什么報錯?
webman/think-orm有個定時器定時發(fā)送心跳用來mysql連接,文件為 https://github.com/webman-php/think-orm/blob/main/src/ThinkOrm.php ,內(nèi)容如下
<?php
namespace Webman\ThinkOrm;
use Webman\Bootstrap;
use Workerman\Timer;
use think\facade\Db;
class ThinkOrm implements Bootstrap
{
// 進程啟動時調(diào)用
public static function start($worker)
{
// 配置
Db::setConfig(config('thinkorm'));
// 維持mysql心跳
if ($worker) {
Timer::add(55, function () {
$connections = config('thinkorm.connections', []);
foreach ($connections as $key => $item) {
if ($item['type'] == 'mysql') {
Db::connect($key)->query('select 1');
}
}
});
}
}
}