1 <?php 2 3 function _make_mysql($host, $port, $username, $password, $dbs, $prefix="mysql_") { 4 $data = []; 5 $hosts = explode('|', $host); 6 if (sizeof($hosts) == 1) { 7 $host = ['host' => $hosts[0]]; 8 } else { 9 $host = [ 10 'write' => ['host' => $hosts[0]], 11 'read' => ['host' => $hosts[1]] 12 ]; 13 } 14 15 foreach ($dbs as $key => $db) { 16 if (!is_string($key)) { 17 $key = $db; 18 } 19 $config = [ 20 'driver' => 'mysql', 21 'port' => $port, 22 'database' => $db, 23 'username' => $username, 24 'password' => $password, 25 'charset' => 'utf8', 26 'collation' => 'utf8_unicode_ci', 27 'prefix' => '', 28 'strict' => false 29 ]; 30 $data[$prefix.$key] = array_merge($config, $host); 31 } 32 return $data; 33 } 34 35 function _make_mongo($host, $port, $dbs, $prefix="mongo_") { 36 $data = []; 37 foreach ($dbs as $db) { 38 $data[$prefix.$db] = [ 39 'driver' => 'mongodb', 40 'host' => $host, 41 'port' => $port, 42 'database' => $db 43 ]; 44 } 45 return $data; 46 } 47 48 $admin_db = env('APP_MODE') == 0 ? 'admin' : 'cooperate'; 49 $gege_db = env('APP_ENV') == 'production' ? 'gege' : 'moge'; 50 $connections = array_merge( 51 _make_mysql(env('DB_MYSQL_HOST', '127.0.0.1'), env('DB_MYSQL_PORT', 3306), env('DB_MYSQL_USERNAME', 'root'), env('DB_MYSQL_PASSWORD', ''), 52 ['admin' => $admin_db]), 53 _make_mysql(env('DB_MYSQL_EBOX_HOST', '127.0.0.1'), env('DB_MYSQL_EBOX_PORT', 3306), env('DB_MYSQL_EBOX_USERNAME', 'root'), env('DB_MYSQL_EBOX_PASSWORD', ''), 54 ['ebox_v2','ebox_kz', 'ebox_order', 'ebox_pickup', 'ebox_notify', 'ebox_media', 'ebox_charge','gege' => $gege_db], 'mysql_'), 55 _make_mongo(env('DB_MONGO_HOST', '127.0.0.1'), env('DB_MONGO_PORT', 27017), 56 ['user', 'weixin', 'delivery', 'ebox','store', 'media']), 57 _make_mongo(env('DB_MONGO_OPENPLATFORM_HOST', '127.0.0.1'), env('DB_MONGO_OPENPLATFORM_PORT', 27017), 58 ['kuaidi_weixin','openplatform' ], "mongoext_"), 59 60 _make_mysql(env('DB_MYSQL_STATS_HOST', '127.0.0.1'), env('DB_MYSQL_STATS_PORT', 3306), env('DB_MYSQL_STATS_USERNAME', 'root'), env('DB_MYSQL_STATS_PASSWORD', ''), 61 ['user'], 'stats_'), 62 _make_mysql(env('DB_MYSQL_SYNC_HOST', '127.0.0.1'), env('DB_MYSQL_SYNC_PORT', 3306), env('DB_MYSQL_SYNC_USERNAME', 'root'), env('DB_MYSQL_SYNC_PASSWORD', ''), 63 ['syncdb'], 'syncdb_') 64 ); 65 66 return [ 67 'fetch' => PDO::FETCH_CLASS, 68 'default' => env('DB_DEFAULT_CONNECTION', 'mysql_admin'), 69 'connections' => array_merge([], $connections), 70 71 'migrations' => 'migrations', 72 'redis' => [ 73 'cluster' => false, 74 'default' => [ 75 'host' => env('DB_REDIS_HOST', '127.0.0.1'), 76 'port' => 6379, 77 'database' => 0, 78 ], 79 ], 80 81 ];
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/8985799.html