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.
htdocs/
--cron_test/
-- -- File to run from index.php//cron
First of all, I tried to log out the current date without suddenly operating the database.
crontab
***/usr/bin/php/Applications/MAMP/htdocs/cron_test/index.php>/Applications/MAMP/htdocs/cron_test/cron.log2>&1
whichphp
describes the PHP path returned.
The php file you want to run is in the cron_test directory in htdocs.
index.php
<?php
date_default_timezone_set('Asia/Tokyo');
$date = new DateTime();
$datetime = $date->format('Y-m-d H:i:s');
error_log("$datetime\n", 3, dirname(__FILE__)."/debug.log");
Results
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.
index.php
date_default_timezone_set('Asia/Tokyo');
$date = new DateTime();
$datetime = $date->format('Y-m-d H:i:s');
try{
$pdo = new PDO(
'mysql:dbname=testdb;host=localhost;charset=utf8mb4',
'root',
'root',
[
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC,
]
);
$stmt = $pdo->query('SELECT* FROM users');
$rows = $stmt->fetchAll();
} catch(PDOException$e){
header('Content-Type:text/plain; charset=UTF-8', true, 500);
exit($e->getMessage());
}
foreach($rows as$val){
$user_id = $val ['user_id'];
$send_id = $val ['send_id'];
$name = $val ['name'];
$email=$val['email'];
$comment = $val ['comment'];
$status = $val ['status'];
$created_at=$val['created_at'].'...';
error_log("$user_id.$send_id.$name.$email.$comment.$status.$created_at.$datetime.\n", 3, dirname(__FILE__)."/debug.log");
}
cron.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.
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.
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.
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.
© 2024 OneMinuteCode. All rights reserved.