簡體   English   中英

使用Laravel 5.7設置本地開發數據庫

[英]Setting up local development database with Laravel 5.7

我在托管平台Cloudways上創建了一個新的Laravel應用程序(Laravel 5.7)。 Cloudways很方便,因為您可以直接從其界面安裝新的Laravel構建,而不是在本地創建並推送到服務器。

無論如何,我創建了構建,並將文件拉到本地計算機上。 在Laravel的早期版本中,我似乎記得只有能夠編輯.env文件以指向我的本地數據庫(當前通過MAMP / phpMyAdmin運行)。

我確保創建了一個新數據庫,然后更新了.env文件以指向該數據庫。

但是,每當我嘗試運行遷移時,它仍然要指向database.php配置文件中指定的數據庫連接。

更新.env文件后,我DID嘗試同時運行兩個:

php artisan config:clear
php artisan cache:clear

但似乎都沒有影響。 每次嘗試運行遷移時,我仍然會收到錯誤消息。

我似乎找不到關於如何從生產數據庫中指定其他本地開發數據庫的其他文檔。

有人可以提供任何幫助嗎?

編輯:

在MAMP上,它的本地設置如下:

Host:   localhost
Port:   8889
User:   root
Password:   root

我當前的.env文件內容:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Z2ndcYy3H7GMfmEW1rKYxYAxMMjyyAWAsoS+9Q3yppc=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=scoutsafe
DB_USERNAME=root
DB_PASSWORD=root

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

和我當前的database.php內容:

    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' => 'mysql',

    /*
    |--------------------------------------------------------------------------
    | 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'   => '',
            ),


            'mysql' => array(
                    'driver'    => 'mysql',
                    'host'      => '127.0.0.1',
                    'database'  => 'scoutsafe',
                    'username'  => 'root',
                    'password'  => 'root',
                    'charset'   => 'utf8',
                    'collation' => 'utf8_unicode_ci',
                    'prefix'    => '',
            ),

            '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,
            ),

    ),

);

您的mysql配置是硬編碼的。 您必須像這樣更新您的database.php才能使用.env的變量:

'connections' => [
    // Other connection options

    'mysql' => [
        // Other mysql options
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        // Other mysql options
    ],
],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM