0

I have to run a PHP file from the cron job every two hours and I am getting MySQL Database Connection Error when running the PHP script via cron. The script works correctly & exactly as I had expected it to run from the browser and even from the command line.

I have read all the answers I found on Google search results, but none of them could solve my problem. I don't think there is any answer related to my problem so I am asking this question.

Following is the database connection php script:

<?php

$cs = "localhost";
$cu = "root";
$cp = "mypassword";
// $cp = "";
$cn = "mydatabasename";

$conn = @mysqli_connect($cs, $cu, $cp, $cn);

if (!$conn) {
    die("Database connection error... " . mysqli_connect_error());
}

I am running the script from browser & from command line, all is working great as I had expected, just when I run it from cronjob I get the following error:

Database connection error... Access denied for user 'root'@'localhost' (using password: NO)

Assume that root, mypassword, mydatabasename are the correct credentials as I can run the script successfully on the browser and even on command line. I am using absolute path of the PHP files in the cronjob.

I am running the cronjob by this command:

0 0-23/2 * * * php -f /var/www/html/project-name/execute-cronjob.php >> /var/www/html/project-name/output.log

I am using Ubuntu and have a VPS Server.

Coder Amogh
  • 145
  • 1
  • 10
  • 4
    Please don't suppress errors using `@` since that _hides_ potential error messages (which are crucial for debugging). Btw, did you get that error message with the above code or with `$cp = "";` uncommented? I don't see how you would get `using password: NO` otherwise. – M. Eriksson Jun 13 '19 at 14:16
  • Could you *try* `127.0.0.1` instead of `localhost`? – Mark Jun 13 '19 at 14:17
  • @MagnusEriksson on my local machine the password is null, so I had used it locally, and then commented when it got uploaded to the server. I too am really confused, how I am getting problem when I AM using the password! Thanks for clarification of '@'... – Coder Amogh Jun 13 '19 at 14:24
  • Is there any proper way to run PHP files with cronjob which uses and requires MySQL Database Connection? – Coder Amogh Jun 13 '19 at 14:30
  • For a cron job purpose ,you can use shell scipt if you are linux user. batch script if you are a windows user. – snfrox Jun 13 '19 at 14:36
  • There shouldn't be any issues running code that connects to a database through cron. If the credentials are correct in your file, it should work. You should also update the code in your question to match you local environment (like using an empty password). – M. Eriksson Jun 13 '19 at 14:39
  • @snfrox can you explain more on how to use shell script to run PHP script which uses MySQL DB connection? I am using Linux: Ubuntu. – Coder Amogh Jun 13 '19 at 14:40
  • @MagnusEriksson It would not have been a problem if everything: from browser, Command Line and cron job would have failed... There's some issue with cron... – Coder Amogh Jun 13 '19 at 14:41
  • I know that it "shouldn't" be any issues, but here we are. I have made scripts that fetches data from mysql in cron jobs and have never run into this issue so, let's forget what "should" or "shouldn't" work and let's double and triple check everything. – M. Eriksson Jun 13 '19 at 14:43
  • check this https://stackoverflow.com/questions/21196613/run-a-mysql-query-as-a-cron-job/21197783 – snfrox Jun 13 '19 at 14:46
  • @MagnusEriksson Let's check all the things... I have checked all and I require suggestions from the community about what should be done... Last option remains is to use something like cURL to hit the request to the server from our own server and the script will run and my jon will be done... – Coder Amogh Jun 13 '19 at 14:48
  • @snfrox Great!!! Thanks, will do that if I can't find any other solution! Please write that to the answer so that it will be beneficial for all. Thank You All For Helping Me Out!! – Coder Amogh Jun 13 '19 at 14:53
  • _"I have checked all and I require suggestions from the community "_ - Then your question should include all things you've checked and all debugging you've done. Currently, it just says: _"Assume that root, mypassword, mydatabasename are the correct credentials"_. Since we can't debug the code ourselves, we are 100% dependent on the info you give us. – M. Eriksson Jun 13 '19 at 14:56
  • @MagnusEriksson What else do you require Magnus? Please ask directly... I will add it to the question... I have given everything I thought it will be required. – Coder Amogh Jun 13 '19 at 14:59

1 Answers1

-3

Use shell script instead

mysql --user=[username] --password=[password] --database=[db name] --execute="you query goes here"

there is a similar answer here

snfrox
  • 124
  • 7
  • echo "this is how you log to a file" >> /var/log/customlog – snfrox Jun 13 '19 at 15:03
  • This will only work if they want to make the same static query every time. This might be what they want, but we should ask first. If they want to pass some dynamic data or do something with the response, then this won't help. – M. Eriksson Jun 13 '19 at 15:05
  • @MagnusEriksson currently I just want to run a static query, so this would be my last option, but still need a good answer, as I may need to pass more dynamic data in the future. – Coder Amogh Jun 13 '19 at 15:06
  • You can either ignore this warning or read the password from a file – snfrox Jun 13 '19 at 15:16
  • How do I do something with the response I get? Whatever I will get the response back I want to run it as a command as the command to run is stored in database which I want to query and run it every 2 hours – Coder Amogh Jun 13 '19 at 15:24
  • What type of a response you get? lets start a chat – snfrox Jun 13 '19 at 15:31