0

I run my website on Debian 9, Apache 2.4, PHP 7.2. MySQL database is remote. The website works smoothly using mysqli module. But when I try to run PHP file through command line it fails and displays the error:

PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory in ... Database connection failed: No such file or directory (2002)

The PHP file I've been trying to run has this line in the beginning:

require_once(__DIR__ . '/../private/initialize.php');

The initialize.php file has this line: $database = db_connect();, which calls these functions:

function db_connect() {
    $connection = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    confirm_db_connect($connection);
    return $connection;
}

function confirm_db_connect($connection) {
    if ($connection->connect_errno) {
        $msg = 'Database connection failed: ';
        $msg .= $connection->connect_error;
        $msg .= ' (' . $connection->connect_errno . ')';
        exit($msg);
    }
}

For security reasons database credentials is set as constants and is taken from environmental variables by the following code:

define('DB_SERVER', getenv('DB_SERVER'));
define('DB_USER', getenv('DB_USER'));
define('DB_PASS', getenv('DB_PASS'));
define('DB_NAME', getenv('DB_NAME'));

Environmental variables defined in /etc/apache2/envvars file through export command:

export DB_SERVER='*here db credential*'
export DB_USER='*here db credential*'
export DB_PASS='*here db credential*'
export DB_NAME='*here db credential*'

I have checked whether the environmental variables are being taken by the script and they weren't.

echo getenv('DB_SERVER');
echo getenv('DB_USER');
echo getenv('DB_PASS');
echo getenv('DB_NAME');

It seems that the variables are set improperly.

Can anybody help with the issue? I want to run PHP files with mysqli extension from command line so that to use CRON Jobs.

Thanks in advance!

Andrew Shaban
  • 129
  • 2
  • 16
  • You didn't need to touch your configuration files; if the module wasn't loaded PHP would have failed with an "unknown function" fatal error. What you should be doing is checking the errors returned in the mysqli_* functions, and showing us the code that's causing problems. – miken32 Dec 07 '18 at 03:04
  • This sounds like you need to set the correct value in php.ini for `mysqli.default_socket`. The file which was not found, is the localhost connection socket identified by that line. You would need to check your MySQL configuration to find out what the correct socket path is. Alternatively, if your MySQL server is listening on TCP, use `127.0.01` as the connection host instead of `localhost`. – Michael Berkowski Dec 07 '18 at 21:03
  • Now to the cron issue - it is possible (probable) that the CLI php run via cron is using a _different_ php.ini file than the web server uses, and therefore `mysqli.default_sock` might be correct in one location but incorrect in the other php.ini location. From the command line check `php -i | less` and look for the loaded configuration file near the top. You might already have had the extension correctly loaded before you tried to modify with `extension=mysqli`, but an incorrect socket. – Michael Berkowski Dec 07 '18 at 21:05
  • @MichaelBerkowski But I use remote database. Should I change not default socket, but default host in php.ini file? – Andrew Shaban Dec 07 '18 at 21:36
  • Hi @miken32! I have edited the question. Maybe now it got clearer. – Andrew Shaban Dec 07 '18 at 21:37
  • @MichaelBerkowski Q says "MySQL database is remote" – miken32 Dec 07 '18 at 21:45
  • So what values are you trying to connect with? What is DB_SERVER? – miken32 Dec 07 '18 at 21:45
  • @miken32, I takes the database credentials from environmental variables. I have change the question adding information about them – Andrew Shaban Dec 07 '18 at 22:14
  • Are you sure those values are making it through to your script though? – miken32 Dec 07 '18 at 22:16
  • @miken32, Yeah! The website works fine in browser. – Andrew Shaban Dec 07 '18 at 22:18
  • Well of course it does, if you're setting the environment variables via your web server... – miken32 Dec 07 '18 at 22:19
  • @miken32, I just checked creating php file which try to echo getenv() for each of environmental variables and it couldn't. Maybe there is a way to set the variables properly so that the script can take them? – Andrew Shaban Dec 07 '18 at 23:08
  • They're *environment* variables, meaning they need to be set in the environment the script is running in. If you're running this manually, put them in your shell profile. If it's a cron job, edit the crontab to include them. – miken32 Dec 07 '18 at 23:24
  • @miken32, I solved the issue. Thank you very much for your help! – Andrew Shaban Dec 10 '18 at 20:28

1 Answers1

0

I solved the issue!

I placed my variables into crontab -e file before cron jobs but without export command.

DB_SERVER='*here db credential*'
DB_USER='*here db credential*'
DB_PASS='*here db credential*'
DB_NAME='*here db credential*'

It makes the variables accessible for all cron jobs.

So when I am using cron jobs it works just fine!

Andrew Shaban
  • 129
  • 2
  • 16
  • Don't forget to ensure permissions are 600 and ownership is appropriate so only the user running cron can get to them. – miken32 Dec 10 '18 at 20:51