I'm porting my application from traditional Nginx, PHP, Mysql on one host to a kubernetes environment.
I've been following the tutorial provided Here
The TLDR; is that you create a php-fpm processor (workload) listening on php:9000, and an nginx workload that handles the request and proxies to the FPM service. I've added a mysql:5.7.30 workload, and a phpMyAdmin workload, which interact fine.
My site loads, index.php shows up fine, and a test file with phpinfo(); executes correctly. So I know that nginx is passing the scripts to php:9000
correctly, and all my ingresses are working.
Here is where things get weird. If I load a page via browser that invokes mysqli, even just a new mysqli(...);
I get an error saying:
2020/05/10 04:22:56 [error] 6#6: *12 FastCGI sent in stderr: "PHP message: PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory in /var/www/app/db.php on line 12
PHP message: PHP Warning: mysqli::query(): Couldn't fetch mysqli in /var/www/app/script.php on line 8
PHP message: PHP Notice: Trying to get property of non-object in /var/www/app/script.php on line 9"
But if I execute that same script on the FPM pod, it returns as expected, no errors.
My script is just performing a read, where it includes db.php
which contains the mysqli connection which looks like:
$db_host = getenv('MYSQL_HOST', true);
$db_uname = getenv('MYSQL_USER', true);
$db_pwd = getenv('MYSQL_PASS', true);
$db_name=getenv('MYSQL_DB', true);
$db = new \mysqli($db_host,$db_uname,$db_pwd,$db_name);
The \mysqli
comes from this post which unfortunately didn't help.
In this case, the env variable $db_host
is set to a DNS name, called mysql-host
. I know that the cluster DNS is working, because I have another pod running phpMyAdmin that can authenticate to the mysql pod fine (they're all in the same namespace).
Is this a case for having Nginx and PHP co-located as opposed to separating them into services? It seems like the mysqli connection is trying to initiate on the nginx pod, which wouldn't have php or mysql on it. Both the Nginx and fpm pods have the same environment variables, which point at the same host for mysql.