PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known.
Closed this issue · 8 comments
Hi there,
Firstly nice work.
Secondly I can't seem to be able to seed my db as the following error occurs...
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known.
I am running on Docker so my host is my service container name db
.
My application has had no trouble connecting to the database and inserting entries, it's all configured in config.php
.
Config file
Registering DB Service
Registering Yarak Service
Yarak file
Connecting manually seems to work?
try {
$pdo = new \PDO('mysql:host=dev_db;dbname=realworlddb;port=3306;charset=utf8', 'root', 'secret');
$stmt = $pdo->query('SELECT * FROM users');
$result = $stmt->fetchAll();
var_dump($result); exit;
} catch (\Exception $e) {
var_dump($e); exit;
}
UPDATE:
It fails at this line...
https://github.com/zachleigh/yarak/blob/master/src/DB/Factories/ModelFactoryBuilder.php#L148
Though I can run the same type of code in a controller so maybe it's a CLI issue?
Do you know how I can debug this problem?
Thanks
Hi there. It seems to be set up fine. Were you able to run migrations or use anything else that required the db? Is it only failing with seeders?
Thanks for the reply Zach, I haven't tried a migration yet.
In my application controllers/models running new ModelClass(['my' => 'modelData'])
is fine but on this line it won't work:
https://github.com/zachleigh/yarak/blob/master/src/DB/Factories/ModelFactoryBuilder.php#L148
I can try the other DB reliant stuff.
@zachleigh Yeah migrate also throws the same error.
Could it be the way it's bootstrapped in the Yarak file?
I just cloned your repo and played around with it. I was able to get migrations and seeds running using the setup you have so my guess is that Docker is to blame.
What happens if you call Yarak in your code, maybe in a controller:
use Yarak\Yarak;
Yarak::call('db:seed');
Yeah I'm wondering if it is Docker or something about my development environment.
Yarah::call('db:seed')
works like a charm.
Thanks for taking the time trying it out on my repo. We can rule out Yarak for now.
Thanks again :)
Some things that could help you debug:
In the Yarak file, dump out the app config before the call to kernel::handle():
var_dump($di->get('config'));
die();
$kernel->handle();
Make sure your db config info is there.
In /src/DB/ConnectionResolver, comment out line 28. That line isnt necessary any more, but could be messing up your db connection.
In that same file, above line 28, create a PDO class and see if it creates it successfully and uses the correct credentials:
var_dump(new $dbClass($dbConfig));
die();
Calling yarak::migrate should trigger this.
Also, what OS are you using? Have you ever had trouble connecting to the DB from the command line before? Might try creating a simple php file that just connects to the db and then calling it from the command line. Also, check your cli php.ini file.
These are all good suggestions, thanks for debugging tips. Will try them out and report back.
@zachleigh it seems to be a Docker related problem, I think you can close this issue :)