I want to run PHP on crontab, but I can't connect to mysql

Asked 2 months ago, Updated 2 months ago, 4 views

I am currently studying cron, and I found it difficult to solve myself, so I would like to ask you a question.

When I tried to retrieve data from MySQL and write it to a text log in the test, SQLSTATE [HY000][2002] The error No such file or directory appears and does not work.

  • macOS
  • MAMP 5.5
  • PHP Version 7.3.8
  • MySQL 5.7.26
    -- -- File to run from index.php//cron


First of all, I tried to log out the current date without suddenly operating the database.



whichphp describes the PHP path returned.
The php file you want to run is in the cron_test directory in htdocs.


$date = new DateTime();
$datetime = $date->format('Y-m-d H:i:s');

error_log("$datetime\n", 3, dirname(__FILE__)."/debug.log");


2020-05-29 11:35:00
2020-05-29 11:36:00
2020-05-29 11:37:00
2020-05-29 11:38:00

The results show that PHP runs every minute.
Then, while keeping the crontab configuration intact, we retrieved test data from Mysql and tried periodic log file output.

The source code is as follows.

$date = new DateTime();
$datetime = $date->format('Y-m-d H:i:s');

  $pdo = new PDO(
  $stmt = $pdo->query('SELECT* FROM users');
  $rows = $stmt->fetchAll();

  } catch(PDOException$e){
    header('Content-Type:text/plain; charset=UTF-8', true, 500);

  foreach($rows as$val){
    $user_id = $val ['user_id'];
    $send_id = $val ['send_id'];
    $name = $val ['name'];
    $comment = $val ['comment'];
    $status = $val ['status'];

    error_log("$user_id.$send_id.$name.$email.$comment.$status.$created_at.$datetime.\n", 3, dirname(__FILE__)."/debug.log");


SQLSTATE [HY000] [2002] No such file or directory

The above error was printed in the cron log file and could not be read properly.

If you run the PHP file (reload your browser) without using cron, it will work.

I also checked the php.ini setting after reading this article.

The destination was /Applications/MAMP/tmp/mysql/mysql.sock, and the MAMP mysql socket was configured.

  • Change host to
    As localhost was specified, I tried changing it to

The No such file or directory error disappeared, but another message appeared.

SQLSTATE [HY000][1045] Access denied for user 'root' @'localhost' (using password: NO)

I tried to connect to MYSQL with PHP, but I got an error - Yahoo! Chiebukuro

I saw this article, but I did not manipulate the MAMP root user settings.
I didn't know why because I could connect when I run PHP without using cron.

  • Cron is able to run PHP files, but when I try to access mysql, I get an error
  • localhost gets in trouble without a directory
  • gets in trouble for not being able to access it on root

I sometimes think that using MAMP may be the cause, but I haven't come up with a fundamental solution.
I would appreciate it if you could let me know if you know how to solve this problem.
Thank you for your cooperation.

php cron

2022-09-30 11:40

1 Answers

The PHP path was rewritten to /Applications/MAMP/bin/php/php 7.x.x/bin/php and executed.

This post was posted as a community wiki based on @keisuke1223's comments.

2022-09-30 11:40

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.