Recently had a problem with Yii2's migrate when trying to connect to a local database.
[email@example.com yii-application]# ./yii migrate Yii Migration Tool (based on Yii v126.96.36.199) Error: SQLSTATE[HY000]  php_network_getaddresses: getaddrinfo failed: Name or service not known
Searched a lot on the web and everything pointed to a recent system update. I'd just done one and was worried that it'd broken something. As it turned out it was completely unrelated. I had configured the database connection settings in the common folder but I had forgotten that the console section of Yii2 uses its own configuration outside if the common/frontend/backend part.
The reason for the error was simply that Yii2 could not resolve the hostname in the default configuration.
return [ 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=host_address;dbname=database_name', 'username' => 'username', 'password' => 'password', 'charset' => 'utf8' ],
I added the settings from common and Hey Presto! everything works.