从database.php中的mysql导入数据库信息[复制]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从database.php中的mysql导入数据库信息[复制]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我想获取存储在我的数据库中的数据库信息,并将其添加到database.php中。 我试图获取数据库中的所有信息,但我总是有这样的错误:
[2019-04-18 20:22:20] laravel.ERROR:在null {“exception”上调用成员函数connection():“[object](Symfony Component Debug Exception FatalThrowableError(code:0) :在/var/www/vhosts/xxxx.net/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1249上调用null上的成员函数connection())
我在database.php的顶部添加了这个:
<?php
use AppCampaign;
$database_infos = Campaign::all();
$campains_db = array();
foreach ($database_infos as $info) {
if (isset($info->db_name)) {
$campains_db[$info->keyword] = array(
'driver' => 'mysql',
'host' => $info->db_host,
'database' => $info->db_name,
'username' => $info->db_user,
'password' => decrypt($info->db_password),
'prefix' => $info->db_prefix,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => false,
'engine' => null,
);
}
}
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
$campains_db,
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'wordpress' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql' => [
'driver' => 'mysql',
'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',
'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',
'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,
],
],
'migrations' => 'migrations',
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
];
我在另一篇文章中发现了这个:
最简单的解决方案是在运行时设置数据库配置。 Laravel可能希望从config / database.php文件加载这些设置,但这并不意味着您以后无法设置或更改它们。 从config / database.php加载的配置作为数据库存储在Laravel配置中。意思是,config / database.php中的connections数组存储在database.connections中。
Config::set("database.connections.mysql", [
"host" => "...",
"database" => "...",
"username" => "...",
"password" => "..."
]);
以上是关于从database.php中的mysql导入数据库信息[复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何正确地将数据从 *.csv 文件导入 mysql 数据库中的给定表
MYSQL 使用 LOAD DATA INFILE 从 csv 导入数据
MYSQL 使用 LOAD DATA INFILE 从 csv 导入数据