gpt4 book ai didi

mysql - laravel 5.8 连接第二个数据库时出错

转载 作者:行者123 更新时间:2023-11-29 15:39:25 25 4
gpt4 key购买 nike

我在 MySql 中有一个数据库处于本地模式,另一个数据库位于 MariaDB 的另一台服务器中

我在我的database.php文件中配置了这么多如 .env 中所示

数据库.php

 'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
],

'asterisk' => [
'driver' => 'mysql',
'host' => env('ASTERISK_HOST', 'localhost'),
'database' => env('ASTERISK_DATABASE', 'forge'),
'username' => env('ASTERISK_USERNAME', 'forge'),
'password' => env('ASTERISK_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=''

ASTERISK_HOST=192.168.2.212
ASTERISK_PORT=3306
ASTERISK_DATABASE=database
ASTERISK_USERNAME=usuario
ASTERISK_PASSWORD='password'

我测试连接的测试路径是 Route::get('vicidial','Vicidial\VicidialPruebaController@index');

Controller 如下

Vicidial\VicidialPruebaController.php

<?php

namespace App\Http\Controllers\Vicidial;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\VicidialModel\VicidialList;
class VicidialPruebaController extends Controller
{
public function index(){
$list = VicidialList::find(1);
dd($list);
}
}

模型是

VicidialModel\VicidialList.php

<?php

namespace App\VicidialModel;

use Illuminate\Database\Eloquent\Model;

class VicidialList extends Model{

/**
* The connection name for the model.
*
* @var string
* @author Luis Morales
*/
protected $connection = 'asterisk';

/**
* The table associated with the model.
*
* @var string
* @author Luis Morales
*/
protected $table = 'vicidial_list';

/**
* The primary key associated with the table.
*
* @var string
* @author Luis Morales
*/
protected $primaryKey = 'lead_id';

/**
* The attributes that are mass assignable.
*
* @var array
* @author Luis Morales
*/
protected $fillable = [
'entry_date',
'modify_date',
'status',
'user',
'vendor_lead_code',
'source_id',
'list_id',
'gmt_offset_now',
'called_since_last_reset',
'phone_code',
'phone_number',
'title',
'first_name',
'middle_initial',
'last_name',
'address1',
'address2',
'address3',
'city',
'state',
'province',
'postal_code',
'country_code',
'gender',
'date_of_birth',
'alt_phone',
'email',
'security_phrase',
'comments',
'called_count',
'last_local_call_time',
'rank',
'owner',
'entry_list_id'
];

/**
* Indicates if the model should be timestamped.
*
* @var bool
* @author Luis Morales
*/
public $timestamps = false;

}

An error occurred during the connection attempt because the connected party did not respond properly after a period of time, or an error occurred in the established connection since the connected host could not

enter image description here

这是对第二个数据库 Asterisk 的首次测试。通过第一个,我的系统可以正常工作

最佳答案

您应该检查ASTERISK_HOST:

  1. mariadb 实例正在运行。
  2. 防火墙阻止 3306 端口。顺便说一句,您的 .env 文件中有一个条目 ASTERISK_PORT=3306,但您没有在 'asterisk' 连接中使用它(没问题因为它应该是默认端口,但最好也检查一下)。
  3. mariadb配置为远程客户端访问,您可以在官方docs的具体页面中找到更多信息.

关于mysql - laravel 5.8 连接第二个数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57836024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com