Laravel数据库配置工作在本地机器上,而不是在谷歌云


Laravel database configuration working on local machine but not in google cloud

嗨,我正在使用谷歌应用程序引擎与云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。我是否遗漏了一些东西来完成配置?

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

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

我遇到了类似的困难,试图在App引擎上部署我的应用程序。我将与你分享我是如何解决它的。

  • 从api.php中删除路由(我不需要这些应用程序)
  • 启用云SQL api
  • 在部署
  • 之前,请遵循以下两个步骤
  • 对编写器进行以下更改。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"