Laravel connection to MySQL works when Migrating but not in Controller

I am using an Eloquent Model to get data from a MySQL DB running on AWS. When I try to migrate the database it works so I know that the connection is not an issue. Also, I exported my models from an existing DB, and that worked fine as well. However, when I try to use the model in a controller I am getting a SQLSTATE[HY000] [2002] Connection refused (SQL: select * from about) (about is table name) error.

EDIT: After going through the stack trace, it seems like a wrong connection is being set up? Not sure why the argument is "mysql:host=127.0.0.1;port=3306;dbname=laravel", when my env specifies another connection? Can this be something to do with the problem?

 */
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 [▶]

Here is my .env configuration

DB_CONNECTION=mysql
DB_HOST=***.***.us-east-1.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=sprCoffee
DB_USERNAME=admin
DB_PASSWORD=password

Here is my model class.

<?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'
    ];
}

Here is my .env configuration

DB_CONNECTION=mysql
DB_HOST=***.***.us-east-1.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=sprCoffee
DB_USERNAME=admin
DB_PASSWORD=password

The main thing is that when I migrate the database, it does create a new migration table in my database, so I wouldn't think my connection is an issue. I have my security group in rds to accept inbound and outbound from "my ip"

1 answer

  • answered 2019-06-11 23:39 Peter

    Restarted server and it works now... Was not working because I did not restart the PHP server after configuring environmental variables. Must restart server for changes to take effect.