-1

在我使用“mysql”作为驱动程序之前,我已经使用相同的信息配置了我的 .env 文件和 db.php 文件,但我尝试将其设为空并抛出相同的错误。

数据库.php

<?php
/**
 * Database Configuration
 *
 * All of your system's database connection settings go in here. You can see a
 * list of the available settings in vendor/craftcms/cms/src/config/DbConfig.php.
 *
 * @see craft\config\DbConfig
 */

return [
    'driver' => getenv(''),
    'server' => getenv('localhost'),
    'user' => getenv('root'),
    'password' => getenv('****'),
    'database' => getenv('craftyblog'),
    'schema' => getenv(''),
    'tablePrefix' => getenv(''),
    'port' => getenv('')
];

.env

# The environment Craft is currently running in ('dev', 'staging', 'production', etc.)
ENVIRONMENT="dev"

# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY="******"

# The database driver that will be used ('mysql' or 'pgsql')
DB_DRIVER=""

# The database server name or IP address (usually this is 'localhost' or '127.0.0.1')
DB_SERVER="localhost"

# The database username to connect with
DB_USER="root"

# The database password to connect with
DB_PASSWORD="****"

# The name of the database to select
DB_DATABASE="craftyblog"

# The database schema that will be used (PostgreSQL only)
DB_SCHEMA=""

# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)
DB_TABLE_PREFIX=""

# The port to connect to the database with. Will default to 5432 for PostgreSQL and 3306 for MySQL.
DB_PORT=""

DEFAULT_SITE_URL=""

我在这个版本中使用 WAMP:

PHP 7.1.16 阿帕奇 2.4.33 MySQL 5.7.21

希望能解决问题,谢谢。

4

1 回答 1

0

在您的 config.php 中,您似乎正在尝试通过getenv()提取环境变量,但是您将要用作字符串的实际值getenv()而不是环境变量名称传递给函数。这些值在 .env 文件中设置,因此对于协作开发人员来说更便携。

在那个 .env 文件中,没有为数据库驱动程序设置环境变量,因此您可以只在 config.php 中将字符串传递给“驱动程序”。

如果您想从 .env 文件中提取值,请将变量名称作为环境变量的字符串以预期格式传递getenv(),如下所示:

配置文件

return [
    'driver' => 'mysql',
    'server' => getenv('DB_SERVER'),
    'user' => getenv('DB_USER'),
    'password' => getenv('DB_PASSWORD'),
    'database' => getenv('DB_DATABASE'),
    'schema' => getenv('DB_SCHEMA'),
    'tablePrefix' => getenv('DB_TABLE_PREFIX'),
    'port' => getenv('DB_PORT')
];

您的 .env 文件似乎已经为所有内容设置好了,所以您应该一切顺利。但是,要在 config.php 文件中使用 .env 文件中的值,您需要将变量名称作为字符串传递。希望这可以帮助!

于 2019-08-03T21:24:58.220 回答