PHP laravel实现配置使用多数据库

有的时候,我们在开发的过程中需要连接多个数据库。

Laravel框架中早已为我们想到了这样的需求。

配置很简单。

在根目录下的/config/database.php文件中配置:

Laravel中为我们提前预设了多种类型的数据库连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'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数据库连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'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

1
DB::connection('oracle')->select("SELECT * from  xxxxx WHERE XH = '{$personal_id}' AND XM = '{$name}'")

2:使用laravel封装的数据库方法:

1
2
3
4
5
6
$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文件中的,方便我们统一管理。

原文链接:https://juejin.cn/post/7156773436001124389

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容