【问题标题】:Laravel database configuration working on local machine but not in google cloudLaravel 数据库配置在本地机器上工作,但不在谷歌云中
【发布时间】:2014-08-01 17:13:46
【问题描述】:

您好,我正在使用带有云 sql 的谷歌应用引擎,但在部署基于 laravel 的应用时遇到问题。

一切都在我的本地计算机上运行,​​但在将数据库部署到云时我无法查询数据库。我收到以下错误。

InvalidArgumentException 未配置数据库 [phlingo_user]。

这就是我在 \app\config\database.php 中的内容

<?php

if(isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) {
    $u_array = array(
        'driver'        =>  'mysql',
        'unix_socket'   =>  getenv('PRODUCTION_CLOUD_SQL_INSTANCE'),
        'host'          =>  '',
        'database'      =>  getenv('PRODUCTION_U_DB_NAME'),
        'username'      =>  getenv('PRODUCTION_DB_USERNAME'),
        'password'      =>  getenv('PRODUCTION_DB_PASSWORD'),
        'charset'       =>  'utf8',
        'collation'     =>  'utf8_unicode_ci',
        'prefix'        =>  ''
    );
    $c_array = array(
        'driver'        =>  'mysql',
        'unix_socket'   =>  getenv('PRODUCTION_CLOUD_SQL_INSTANCE'),
        'host'          =>  '',
        'database'      =>  getenv('PRODUCTION_C_DB_NAME'),
        'username'      =>  getenv('PRODUCTION_DB_USERNAME'),
        'password'      =>  getenv('PRODUCTION_DB_PASSWORD'),
        'charset'       =>  'utf8',
        'collation'     =>  'utf8_unicode_ci',
        'prefix'        =>  ''
    );
    $p_array = array(
        'driver'        =>  'mysql',
        'unix_socket'   =>  getenv('PRODUCTION_CLOUD_SQL_INSTANCE'),
        'host'          =>  '',
        'database'      =>  getenv('PRODUCTION_P_DB_NAME'),
        'username'      =>  getenv('PRODUCTION_DB_USERNAME'),
        'password'      =>  getenv('PRODUCTION_DB_PASSWORD'),
        'charset'       =>  'utf8',
        'collation'     =>  'utf8_unicode_ci',
        'prefix'        =>  ''
    );
} else {
    $u_array = array(
        'driver'    =>  'mysql',
        'host'      =>  getenv('DEVELOPMENT_DB_HOST'),
        'database'  =>  getenv('DEVELOPMENT_U_DB_NAME'),
        'username'  =>  getenv('DEVELOPMENT_DB_USERNAME'),
        'password'  =>  getenv('DEVELOPMENT_DB_PASSWORD'),
        'charset'   =>  'utf8',
        'collation' =>  'utf8_unicode_ci',
        'prefix'    =>  ''
    );
    $c_array = array(
        'driver'    =>  'mysql',
        'host'      =>  getenv('DEVELOPMENT_DB_HOST'),
        'database'  =>  getenv('DEVELOPMENT_C_DB_NAME'),
        'username'  =>  getenv('DEVELOPMENT_DB_USERNAME'),
        'password'  =>  getenv('DEVELOPMENT_DB_PASSWORD'),
        'charset'   =>  'utf8',
        'collation' =>  'utf8_unicode_ci',
        'prefix'    =>  ''
    );
    $p_array = array(
        'driver'    =>  'mysql',
        'host'      =>  getenv('DEVELOPMENT_DB_HOST'),
        'database'  =>  getenv('DEVELOPMENT_P_DB_NAME'),
        'username'  =>  getenv('DEVELOPMENT_DB_USERNAME'),
        'password'  =>  getenv('DEVELOPMENT_DB_PASSWORD'),
        'charset'   =>  'utf8',
        'collation' =>  'utf8_unicode_ci',
        'prefix'    =>  ''
    );
}

return array(

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_CLASS,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => 'phlingo_user',

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => array(

        'sqlite' => array(
            'driver'   => 'sqlite',
            'database' => __DIR__.'/../database/production.sqlite',
            'prefix'   => '',
        ),

                getenv('MYSQL_U')      =>  $u_array,

                getenv('MYSQL_C')      =>  $c_array,

                getenv('MYSQL_P')   =>  $p_array,

        'pgsql' => array(
            'driver'   => 'pgsql',
            'host'     => 'localhost',
            'database' => 'forge',
            'username' => 'forge',
            'password' => '',
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
        ),

        'sqlsrv' => array(
            'driver'   => 'sqlsrv',
            'host'     => 'localhost',
            'database' => 'database',
            'username' => 'root',
            'password' => '',
            'prefix'   => '',
        ),

    ),

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => array(

        'cluster' => false,

        'default' => array(
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 0,
        ),

    ),

);

[phlingo_user] 指的是 $u_array。我是否缺少完成配置的内容?

【问题讨论】:

  • 你的 app.yaml 中是否设置了这些环境变量?你可以在你的 laravel 配置文件中设置它们以使事情变得简单。连接到云 sql 实例时,Google 将为您锁定安全性。
  • 不,他们不是。 Laravel 不会检测 app.yaml 中设置的环境变量。然而,它们位于根文件夹中名为 .env.php 的文件中,laravel 平台使用该文件
  • 我还没有在 GAE 上为我的 laravel 应用程序尝试过 .env.php 的东西,所以不确定它们是否有效。我在配置中使用了一个 gae 文件夹,它工作正常。我现在将信息直接放入以进行测试。您需要做更多的工作来移动存储路径并让它们正常工作。
  • 好吧,我做到了,它成功了,但我仍然想知道你如何能够在你的 laravel 应用程序中从你的 app.yaml 访问环境变量?我正在使用 laravel 4.2 你在配置文件夹中添加了什么?
  • @user3813559 你能给我发你的电子邮件吗,我想问你一些关于 Laravel 和 Google SQL 的事情

标签: php google-app-engine laravel laravel-4 google-cloud-sql


【解决方案1】:

这可能会提供一个有用的示例: https://gae-php-tips.appspot.com/2013/10/22/getting-started-with-laravel-on-php-for-app-engine/.

如那篇文章所述,您可以在 app.yaml 中定义环境变量,然后修改您的 database.php 文件以访问它们。看起来您可能缺少设置的 app.yaml 部分。 在应用引擎项目的上下文中(包括在开发服务器上本地运行),这些变量将被设置和访问。

【讨论】:

  • 我试过了,但它不适用于最新的 laravel 迭代。
【解决方案2】:

我在尝试在 App 引擎上部署我的应用程序时遇到了类似的困难。我将与您分享我是如何修复它的。

  • 从 api.php 中删除路由(我的应用程序不需要这些)
  • 启用云SQL APIenable
  • 按照本教程tutorial 在部署前按照接下来的两个步骤进行
  • 对 composer.json 文件进行以下更改。教程不正确。
"post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize",
            "chmod -R 755 bootstrap\/cache"
        ]
  • 像这样配置您的 app.yaml 文件:
  • runtime: php
    env: flex
    
    runtime_config:
      document_root: public
    
    env_variables:
      # Put production environment variables here.
      APP_ENV: production
      APP_LOG: errorlog
      APP_KEY: APP_KEY (DO NOT USE QUOTES)
      CACHE_DRIVER: database
      SESSION_DRIVER: database
      ## Set these environment variables according to your CloudSQL configuration.
      DB_HOST: localhost
      DB_PORT: 3306
      DB_CONNECTION: mysql
      DB_DATABASE: DATABASE_NAME (DO NOT USE QUOTES)
      DB_USERNAME: USERNAME (DO NOT USE QUOTES)
      DB_PASSWORD: PASSWORD (DO NOT USE QUOTES)
      DB_SOCKET: /cloudsql/YOUR_INSTANCE_CONNECTION_NAME (DO NOT USE QUOTES)
    
      QUEUE_DRIVER: database
    
    beta_settings:
        # for Cloud SQL, set this value to the Cloud SQL connection name,
        # e.g. "project:region:cloudsql-instance"
        cloud_sql_instances: "YOUR_INSTANCE_CONNECTION_NAME"
    

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2019-01-17
      • 2019-04-25
      • 2022-12-17
      • 2017-08-22
      • 2022-08-16
      • 2016-11-06
      • 2017-02-17
      • 1970-01-01
      相关资源
      最近更新 更多