您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-配置數據庫-使用多個數據庫(數據庫切換)
发布时间:2022-01-31 22:57:39编辑:雪饮阅读()
數據庫切換或者説是指定使用的數據庫(驅動,這裏說的數據庫切換或者指定數據庫都是指數據庫驅動,這裏說的數據庫驅動都是指config/database.php中可以有多組數據庫連接)可以使用Db的方法connection來實現。
首先需要config/database.php中有多組可用數據庫驅動,那麽配置如:
<?php
return [
// 默认数据库
'default' => 'mysql',
// 各种数据库配置
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'workerman',
'username' => 'xy',
'password' => 'xy',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'phome_',
'strict' => true,
'engine' => null,
],
'mysql2' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'workerman',
'username' => 'xy',
'password' => 'xy',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'phome_',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => '192.168.1.3',
'port' => 5432,
'database' => 'webman',
'username' => 'postgres',
'password' => 'xy',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
];
在數據庫沒有指定或者沒有切換時候則相當於connection指定的mysql,一般的默認是mysql。
那麽多個數據庫的使用或者説是切換的具體實現在app/controller/Index.php控制器中如:
<?php
namespace app\controller;
use support\Request;
use support\Db;
class Index
{
public function index(Request $request)
{
$mysql0 = Db::select("select * from phome_ecms_news where id=86");
//與上面等價
$mysql1 = Db::connection("mysql")->select("select * from phome_ecms_news where id=86");
$mysql2 = Db::connection("mysql2")->select("select * from phome_ecms_news_data_1 where id=86");
$pgsql0 = Db::connection('pgsql')->select("select * from public.user");
var_dump(compact("mysql0","mysql1","mysql2","pgsql0"));
return response("ok");
}
public function view(Request $request)
{
return view('index/view', ['name' => 'webman']);
}
public function json(Request $request)
{
return json(['code' => 0, 'msg' => 'ok']);
}
}
然後服務啓動后,被訪問到app/controller/Index.php控制器中的index方法時:
/www/wwwroot/webman/webman/app/controller/Index.php update and reload
Workerman[start.php] reloading
訪問了全局中間件!
array(4) {
["mysql0"]=>
array(1) {
[0]=>
object(stdClass)#51 (34) {
["id"]=>
int(86)
["classid"]=>
int(67)
["ttid"]=>
int(0)
["onclick"]=>
int(4227)
["plnum"]=>
int(0)
["totaldown"]=>
int(0)
["newspath"]=>
string(0) ""
["filename"]=>
string(2) "86"
["userid"]=>
int(1)
["username"]=>
string(6) "雪饮"
["firsttitle"]=>
int(0)
["isgood"]=>
int(0)
["ispic"]=>
int(1)
["istop"]=>
int(0)
["isqf"]=>
int(0)
["ismember"]=>
int(0)
["isurl"]=>
int(0)
["truetime"]=>
int(1423470979)
["lastdotime"]=>
int(1425343586)
["havehtml"]=>
int(1)
["groupid"]=>
int(0)
["userfen"]=>
int(0)
["titlefont"]=>
string(0) ""
["titleurl"]=>
string(37) "/mobanfenxiang/gerenbokemoban/86.html"
["stb"]=>
int(1)
["fstb"]=>
int(1)
["restb"]=>
int(1)
["keyboard"]=>
string(0) ""
["title"]=>
string(39) "个人博客模板《世界杯来袭》"
["newstime"]=>
int(1423470931)
["titlepic"]=>
string(84) "/d/file/mobanfenxiang/gerenbokemoban/2015-02-09/ba82931d7ee52bc0a6d353385d1e63c4.jpg"
["ftitle"]=>
string(0) ""
["smalltext"]=>
string(241) " 个人博客模板《世界杯来袭》,博客模板色调以绿色和黄色为主色、布局简单随意,以世界杯为主题,绿色清新、黄色活泼、醒目... 此模板暂不提供html下载,喜欢的朋友可以下载"
["diggtop"]=>
int(0)
}
}
["mysql1"]=>
array(1) {
[0]=>
object(stdClass)#56 (34) {
["id"]=>
int(86)
["classid"]=>
int(67)
["ttid"]=>
int(0)
["onclick"]=>
int(4227)
["plnum"]=>
int(0)
["totaldown"]=>
int(0)
["newspath"]=>
string(0) ""
["filename"]=>
string(2) "86"
["userid"]=>
int(1)
["username"]=>
string(6) "雪饮"
["firsttitle"]=>
int(0)
["isgood"]=>
int(0)
["ispic"]=>
int(1)
["istop"]=>
int(0)
["isqf"]=>
int(0)
["ismember"]=>
int(0)
["isurl"]=>
int(0)
["truetime"]=>
int(1423470979)
["lastdotime"]=>
int(1425343586)
["havehtml"]=>
int(1)
["groupid"]=>
int(0)
["userfen"]=>
int(0)
["titlefont"]=>
string(0) ""
["titleurl"]=>
string(37) "/mobanfenxiang/gerenbokemoban/86.html"
["stb"]=>
int(1)
["fstb"]=>
int(1)
["restb"]=>
int(1)
["keyboard"]=>
string(0) ""
["title"]=>
string(39) "个人博客模板《世界杯来袭》"
["newstime"]=>
int(1423470931)
["titlepic"]=>
string(84) "/d/file/mobanfenxiang/gerenbokemoban/2015-02-09/ba82931d7ee52bc0a6d353385d1e63c4.jpg"
["ftitle"]=>
string(0) ""
["smalltext"]=>
string(241) " 个人博客模板《世界杯来袭》,博客模板色调以绿色和黄色为主色、布局简单随意,以世界杯为主题,绿色清新、黄色活泼、醒目... 此模板暂不提供html下载,喜欢的朋友可以下载"
["diggtop"]=>
int(0)
}
}
["mysql2"]=>
array(1) {
[0]=>
object(stdClass)#61 (11) {
["id"]=>
int(86)
["classid"]=>
int(67)
["keyid"]=>
string(0) ""
["dokey"]=>
int(1)
["newstempid"]=>
int(0)
["closepl"]=>
int(0)
["haveaddfen"]=>
int(0)
["infotags"]=>
string(0) ""
["writer"]=>
string(0) ""
["befrom"]=>
string(0) ""
["newstext"]=>
string(6) "雪饮"
}
}
["pgsql0"]=>
array(1) {
[0]=>
object(stdClass)#66 (2) {
["id"]=>
int(1)
["name"]=>
string(6) "雪飲"
}
}
}
訪問者接收到的響應如:
[root@localhost pdo_pgsql]# elinks http://127.0.0.1:8787/Index/index --dump
ok
关键字词:webman,配置,數據庫,使用,多個,切換