我正在使用Eloquent模型从AWS上运行的MySQL数据库获取数据。当我尝试迁移数据库时,它可以正常工作,因此我知道连接不是问题。另外,我从现有的数据库中导出了模型,效果也很好。但是,当我尝试在控制器中使用模型时,出现SQLSTATE [HY000] [2002]连接被拒绝(SQL:从about
中选择*)(大约是表名)错误。
编辑: 经过堆栈跟踪后,似乎建立了错误的连接?当我的环境指定另一个连接时,不确定为什么参数为“ mysql:host = 127.0.0.1; port = 3306; dbname = laravel”吗?可以解决这个问题吗?
*/
public function connect(array $config)
{
$dsn = $this->getDsn($config);
$options = $this->getOptions($config);
// We need to grab the PDO options that should be used while making the brand
// new connection instance. The PDO options control various aspects of the
// connection's behavior, and some might be specified by the developers.
$connection = $this->createConnection($dsn, $config, $options);
if (! empty($config['database'])) {
$connection->exec("use `{$config['database']}`;");
}
$this->configureEncoding($connection, $config);
// Next, we will check to see if a timezone has been specified in this config
// and if it has we will issue a statement to modify the timezone with the
// database. Setting this DB timezone is an optional configuration item.
$this->configureTimezone($connection, $config);
$this->setModes($connection, $config);
return $connection;
}
/**
* Set the connection character set and collation.
*
Arguments
"mysql:host=127.0.0.1;port=3306;dbname=laravel"
array:15 [▶]
array:5 [▶]
这是我的.env配置
DB_CONNECTION=mysql
DB_HOST=***.***.us-east-1.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=sprCoffee
DB_USERNAME=admin
DB_PASSWORD=password
这是我的模型班。
<?php
/**
* Created by Reliese Model.
* Date: Tue, 11 Jun 2019 21:26:12 +0000.
*/
namespace App\Models;
use Reliese\Database\Eloquent\Model as Eloquent;
/**
* Class About
*
* @property int $id
* @property string $description
* @property string $image
* @property string $name
* @property int $position
*
* @package App\Models
*/
class About extends Eloquent
{
protected $table = 'about';
public $incrementing = false;
public $timestamps = false;
protected $casts = [
'id' => 'int',
'position' => 'int'
];
protected $fillable = [
'description',
'image',
'name',
'position'
];
}
这是我的.env配置
DB_CONNECTION=mysql
DB_HOST=***.***.us-east-1.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=sprCoffee
DB_USERNAME=admin
DB_PASSWORD=password
主要是,当我迁移数据库时,它确实在数据库中创建了一个新的迁移表,因此我认为我的连接不是问题。我在rds中有我的安全组来接受“我的ip”的入站和出站
答案 0 :(得分:0)
重新启动服务器,它现在可以工作... 无法正常工作,因为配置环境变量后没有重新启动PHP服务器。必须重新启动服务器才能使更改生效。