文章詳情頁
PHP laravel實現配置使用多數據庫
瀏覽:309日期:2022-06-06 09:11:52
有的時候,我們在開發的過程中需要連接多個數據庫。
Laravel框架中早已為我們想到了這樣的需求。
配置很簡單。
在根目錄下的/config/database.php文件中配置:
Laravel中為我們提前預設了多種類型的數據庫連接
"connections" => [
"sqlite" => [
"driver" => "sqlite",
"url" => env("DATABASE_URL"),
"database" => env("DB_DATABASE", database_path("database.sqlite")),
"prefix" => "",
"foreign_key_constraints" => env("DB_FOREIGN_KEYS", true),
],
"mysql" => [
"driver" => "mysql",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "127.0.0.1"),
"port" => env("DB_PORT", "3306"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"unix_socket" => env("DB_SOCKET", ""),
"charset" => "utf8mb4",
"collation" => "utf8mb4_unicode_ci",
"prefix" => "",
"prefix_indexes" => true,
"strict" => true,
"engine" => null,
"options" => extension_loaded("pdo_mysql") ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env("MYSQL_ATTR_SSL_CA"),
]) : [],
],
"pgsql" => [
"driver" => "pgsql",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "127.0.0.1"),
"port" => env("DB_PORT", "5432"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"charset" => "utf8",
"prefix" => "",
"prefix_indexes" => true,
"schema" => "public",
"sslmode" => "prefer",
],
"sqlsrv" => [
"driver" => "sqlsrv",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "localhost"),
"port" => env("DB_PORT", "1433"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"charset" => "utf8",
"prefix" => "",
"prefix_indexes" => true,
],
],
默認連接的是mysql配置,配置項是存儲在根目錄下的.env文件中的。
如果使用多數據連接,則只需要,根據你的數據庫類型,在配置你需要的數據庫連接就好了。
如下面所示:
我增加了一個mysql數據庫連接和oracle數據庫連接
"connections" => [
"sqlite" => [
"driver" => "sqlite",
"url" => env("DATABASE_URL"),
"database" => env("DB_DATABASE", database_path("database.sqlite")),
"prefix" => "",
"foreign_key_constraints" => env("DB_FOREIGN_KEYS", true),
],
// 本地數據庫,存儲本地數據
"mysql" => [
"driver" => "mysql",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "xxxxx"),
"port" => env("DB_PORT", "3306"),
"database" => env("DB_DATABASE", xxxx),
"username" => env("DB_USERNAME", xxxx),
"password" => env("DB_PASSWORD", xxxxxxx"),
"unix_socket" => env("DB_SOCKET", ""),
"charset" => "utf8mb4",
"collation" => "utf8mb4_unicode_ci",
"prefix" => "",
"prefix_indexes" => true,
"strict" => false,
"engine" => null,
"options" => extension_loaded("pdo_mysql") ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env("MYSQL_ATTR_SSL_CA"),
]) : [],
],
// 線上數據庫,將本地數據,同步至線上數據
"mysql_base" => [
"driver" => "mysql",
"read" => [
"host" => env("DB_HOST_BASE", "xxxxx"),
],
"write" => [
"host" => env("DB_HOST_BASE", "xxxxx")
],
"port" => env("DB_PORT_BASE", xxxx),
"database" => env("DB_DATABASE_BASE", xxxx),
"username" => env("DB_USERNAME_BASE", xxxxxx),
"password" => env("DB_PASSWORD_BASE", "xxxxx"),
"unix_socket" => env("DB_SOCKET", ""),
"charset" => "utf8mb4",
"collation" => "utf8mb4_unicode_ci",
"prefix" => "",
"strict" => false,
"engine" => null,
],
"pgsql" => [
"driver" => "pgsql",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "127.0.0.1"),
"port" => env("DB_PORT", "5432"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"charset" => "utf8",
"prefix" => "",
"prefix_indexes" => true,
"schema" => "public",
"sslmode" => "prefer",
],
"sqlsrv" => [
"driver" => "sqlsrv",
"url" => env("DATABASE_URL"),
"host" => env("DB_HOST", "localhost"),
"port" => env("DB_PORT", "1433"),
"database" => env("DB_DATABASE", "forge"),
"username" => env("DB_USERNAME", "forge"),
"password" => env("DB_PASSWORD", ""),
"charset" => "utf8",
"prefix" => "",
"prefix_indexes" => true,
],
// oracle 數據庫連接
"oracle" => [
"driver" => "oracle",
"host" => "210.30.64.37",
"port" => xxx,
"database" => "",
"service_name" => xxxxx,
"username" => xxx_xxxx,
"password" => xxxx_xxxx,
"charset" => "AL32UTF8",
"prefix" => "",
]
],
我們在使用其余的數據庫連接,如下方所示:
1:使用原生sql
DB::connection("oracle")->select("SELECT * from xxxxx WHERE XH = "{$personal_id}" AND XM = "{$name}"")
2:使用laravel封裝的數據庫方法:
$userinfo = DB::connection("mysql_base")->table("user")
->where("personal_id", "=", $data["personal_id"])
->where("name", "=", $data["name"])
->where("school_id", "=", $data["school_id"])
->select("id", "type", "school_id", "personal_id", "name", "email", "ilab_user", "ilab_pwd")
->first();
最后,還是強調一句,數據庫連接相關的用戶名,連接,密碼之類的,一般還是寫在.env文件中的,方便我們統一管理。
以上就是PHP laravel實現配置使用多數據庫的詳細內容,更多關于PHP laravel配置多數據庫的資料請關注其它相關文章!
標簽:
PHP
排行榜

網公網安備