在laravel 5.2中,我们希望有不同的读/写连接,所以我遵循了laravel文档中提供的建议。但是,默认情况下,它只创建默认的 mysql 命名连接而不是2个不同的读/写连接,因此它选择读取连接用于 INSERT / UPDATE 等操作。
调试后,发现在 DatabaseManager.php 文件中,作为参数传递给makeconnection()
的连接是 mysql 而不是 mysql: :read或mysql :: write 。
之前
配置/ database.php中
'mysql' => [
//we need to have this nested options for both read/write
'read' => [
'host' => env('DB_READ_HOST'),
],
'write' => [
'host' => env('DB_WRITE_HOST'),
],
'host' => env('DB_READ_HOST'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'driver' => 'mysql',
'database' => env('DB_DATABASE'),
'collation' => 'utf8_unicode_ci',
'port' => env('DB_PORT', '3306'),
'charset' => 'utf8',
'prefix' => '',
'strict' => false,
],
档案 - vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php
public function connection($name = null)
{
list($name, $type) = $this->parseConnectionName($name);
// If we haven't created this connection, we'll create it based on the config
// provided in the application. Once we've created the connections we will
// set the "fetch mode" for PDO which determines the query return types.
if (! isset($this->connections[$name])) {
$connection = $this->makeConnection($name);
$this->setPdoForType($connection, $type);
$this->connections[$name] = $this->prepare($connection);
}
return $this->connections[$name];
}
所以,我们在文件中添加了一个小的更改,在添加之后,它现在创建两个不同的连接 mysql.read/mysql.write 并根据给定sql操作 SELECT,INSERT,UPDATE
如果这是一个可行的解决方案,您需要反馈吗?
更改文件后
config/database.php
'mysql' => [
//we need to have this nested options for both read/write
'read' => [
'host' => env('DB_READ_HOST'),
'username' => env('DB_READ_USERNAME'),
'password' => env('DB_READ_PASSWORD'),
'driver' => 'mysql',
'database' => env('DB_DATABASE'),
'collation' => 'utf8_unicode_ci',
'port' => env('DB_PORT', '3306'),
'charset' => 'utf8',
'prefix' => '',
'strict' => false,
],
'write' => [
'host' => env('DB_WRITE_HOST'),
'username' => env('DB_WRITE_USERNAME'),
'password' => env('DB_WRITE_PASSWORD'),
'driver' => 'mysql',
'database' => env('DB_DATABASE'),
'collation' => 'utf8_unicode_ci',
'port' => env('DB_PORT', '3306'),
'charset' => 'utf8',
'prefix' => '',
'strict' => false,
],
'host' => env('DB_READ_HOST'),
'username' => env('DB_READ_USERNAME'),
'password' => env('DB_READ_PASSWORD'),
'driver' => 'mysql',
'database' => env('DB_DATABASE'),
'collation' => 'utf8_unicode_ci',
'port' => env('DB_PORT', '3306'),
'charset' => 'utf8',
'prefix' => '',
'strict' => false,
],
档案 - vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php
public function connection($name = null)
{
list($name, $type) = $this->parseConnectionName($name);
// we check if the $type is read/write and store appropriate connections
if( $type != null ) {
$name .= '.' . $type;
}
//end
// If we haven't created this connection, we'll create it based on the config
// provided in the application. Once we've created the connections we will
// set the "fetch mode" for PDO which determines the query return types.
if (! isset($this->connections[$name])) {
$connection = $this->makeConnection($name);
$this->setPdoForType($connection, $type);
$this->connections[$name] = $this->prepare($connection);
}
return $this->connections[$name];
}