本地Laravel环境未连接到AWS RDS数据库

时间:2019-07-24 09:29:55

标签: laravel

我已经为开发目的设置了本地Laravel环境。难以连接到远程AWS RDS Database数据库。

.env设置

APP_NAME=Laravel
APP_ENV=local
APP_KEY=<APP-KEY>
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=<AWS-RDS-DATABASE-PATH>
DB_PORT=3306
DB_DATABASE=<DB-NAME>
DB_USERNAME=<DB-USERNAME>
DB_PASSWORD=<DB-PASSWORD>

config / database.php

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '<AWS-RDS-DATABASE-PATH>'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', '<DB-NAME>'),
    'username' => env('DB_USERNAME', '<DB-USERNAME>'),
    'password' => env('DB_PASSWORD', '<DB-PASSWORD>'),
    'unix_socket' => env('DB_SOCKET', '/tmp/mysql.sock'),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
],

它适用于CodeIgniter -config / database.php [设置相同]

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '<AWS-RDS-DATABASE-PATH>',
    'username' => '<DB-USERNAME>',
    'password' => '<DB-PASSWORD>',
    'database' => '<DB-NAME>',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

可能是什么问题?

3 个答案:

答案 0 :(得分:0)

您收到错误消息吗?这可能会有所帮助。

答案 1 :(得分:0)

在全新安装Laravel之后,它现在可以正常工作。

答案 2 :(得分:0)

将此代码添加到您的/config/database.php顶部

use Illuminate\Support\Str;

define('RDS_HOSTNAME', (isset($_SERVER['RDS_HOSTNAME'])) ? $_SERVER['RDS_HOSTNAME'] : null);
define('RDS_USERNAME', (isset($_SERVER['RDS_USERNAME'])) ? $_SERVER['RDS_USERNAME'] : null);
define('RDS_PASSWORD', (isset($_SERVER['RDS_PASSWORD'])) ? $_SERVER['RDS_PASSWORD'] : null);
define('RDS_DB_NAME', (isset($_SERVER['RDS_DB_NAME'])) ? $_SERVER['RDS_DB_NAME'] : null);

并将您的MySQL连接更改为:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => (RDS_HOSTNAME) ? RDS_HOSTNAME : env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => (RDS_DB_NAME) ? RDS_DB_NAME : env('DB_DATABASE', 'forge'),
            'username' => (RDS_USERNAME) ? RDS_USERNAME : env('DB_USERNAME', 'forge'),
            'password' => (RDS_PASSWORD) ? RDS_PASSWORD : env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => false,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],