我在跑步
./yii migrate/create create_junction_table_for_sales_and_branch_tables --fields="created_at:dateTime"
,我收到以下错误消息:
Error: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known.
尝试创建普通表也会产生相同的错误。我无法解决这个问题。
这是我在common/config/main.php
中的代码的一部分:
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=' . getenv('DB_HOST') . ';port='.
getenv('DB_PORT', 3306) .';dbname=' . getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
],
答案 0 :(得分:0)
配置数据库连接的最佳方法是:
return [
'class' => \yii\db\Connection::class,
'dsn' => 'mysql:host=address:port;dbname=mydatabase',
'username' => 'admin',
'password' => '123321',
'charset' => 'utf8',
];
还可以在cfg文件中定义变量,并使用它们,请记住,所使用的环境变量必须在您的环境中定义 strong>
// You may define env.variable in your system.
$host = 'localhost';
$port = '3306';
$dbname = 'mydatabase';
$username = 'admin';
$password = '123321';
$charset = 'utf8';
return [
'class' => \yii\db\Connection::class,
'dsn' => "mysql:host={$host}:{$port};dbname={$dbname}",
'username' => $username,
'password' => $password,
'charset' => $charset,
];
您可以根据需要自定义变量,也可以在返回连接类之前之前编写方法,以获取所有所需变量。