|
有的时候,我们在开发的过程中需要连接多个数据库。
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配置多数据库的资料请关注脚本之家其它相关文章!
来源:https://www.jb51.net/article/265600.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
|