一:安装 Mysqli
composer require easyswoole/mysqli
二:安装ORM
composer require easyswoole/orm
三:dev.php 中填写配置信息,可以填写多个数据库连接,还有redis,七牛云等一些配置信息,这里主要写连接多个数据的
return [
'SERVER_NAME' => "EasySwoole",
'MAIN_SERVER' => [
'LISTEN_ADDRESS' => '0.0.0.0',
'PORT' => 9502,
'SERVER_TYPE' => EASYSWOOLE_WEB_SERVER, //可选为 EASYSWOOLE_SERVER EASYSWOOLE_WEB_SERVER EASYSWOOLE_WEB_SOCKET_SERVER,EASYSWOOLE_REDIS_SERVER
'SOCK_TYPE' => SWOOLE_TCP,
'RUN_MODEL' => SWOOLE_PROCESS,
'SETTING' => [
'worker_num' => 8,
'reload_async' => true,
'max_wait_time'=>3
],
'TASK'=>[
'workerNum'=>4,
'maxRunningNum'=>128,
'timeout'=>15
]
],
'TEMP_DIR' => null,
'LOG_DIR' => null,
/*################ MYSQL CONFIG ##################*/
'MYSQL' => [
//数据库配置
'host' => '127.0.0.1',//数据库连接ip
'user' => '',//数据库用户名
'password' => '',//数据库密码
'database' => '',//数据库
'port' => '3306',//端口
'timeout' => '30',//超时时间
'connect_timeout' => '5',//连接超时时间
'charset' => 'utf8mb4',//字符编码
'strict_type' => false, //开启严格模式,返回的字段将自动转为数字类型
'fetch_mode' => false,//开启fetch模式, 可与pdo一样使用fetch/fetchAll逐行或获取全部结果集(4.0版本以上)
'alias' => '',//子查询别名
'isSubQuery' => false,//是否为子查询
'max_reconnect_times ' => '3',//最大重连次数
],
/*################ Company MYSQL CONFIG ##################*/
'MYSQL_CASE' => [
//数据库配置
'host' => '',//数据库连接ip
'user' => '',//数据库用户名
'password' => '',//数据库密码
'database' => '',//数据库
'port' => '3306',//端口
'timeout' => '30',//超时时间
'connect_timeout' => '5',//连接超时时间
'charset' => 'utf8mb4',//字符编码
'strict_type' => false, //开启严格模式,返回的字段将自动转为数字类型
'fetch_mode' => false,//开启fetch模式, 可与pdo一样使用fetch/fetchAll逐行或获取全部结果集(4.0版本以上)
'alias' => '',//子查询别名
'isSubQuery' => false,//是否为子查询
'max_reconnect_times ' => '3',//最大重连次数
],
];
四:EasyswooleEvent.php 文件中配置数据库连接,预连接
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\Bridge\Exception;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\Db\Config;
use EasySwoole\EasySwoole\Config as GlobalConfig;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
}
public static function mainServerCreate(EventRegister $register)
{
// TODO: Implement mainServerCreate() method.
//连接数据库配置
$config = new Config(GlobalConfig::getInstance()->getConf("MYSQL"));
$configs = new Config(GlobalConfig::getInstance()->getConf("MYSQL_CASE"));
try{
DbManager::getInstance()->addConnection(new Connection($config),'mysql');
DbManager::getInstance()->addConnection(new Connection($configs),'mysql_case');
}catch (Exception $e){
}
}
public static function onRequest(Request $request, Response $response): bool
{
// TODO: Implement onRequest() method.
return true;
}
public static function afterRequest(Request $request, Response $response): void
{
// TODO: Implement afterAction() method.
}
}
五:在Model中的处理
namespace App\Models;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Utility\Schema\Table;
class UserModel extends AbstractModel
{
//选择连接的数据库
protected $connectionName = 'mysql';
//选择表
protected $tableName = 'k_user';
}
到此为止多数据库的连接就完成了。