INTO OUTFILE Error 2

Good day, I am trying to run the following command but it does not seem to work.

[root@ ]# mysql -pPassword asteriskcdrdb -s -b -e "select 'Account ID','Destination','Operator','Provider','Date','BillSec','Rate id','Cost' UNION select accountcode,dst,'PBX',route_name,date_format(calldate,'%Y/%c/%e %H:%i'),billsec as Duration,route_id,round(cost,5) from cdr where calldate > '$prev_date' and calldate < '$cur_date' and cost > 0 and accountcode='$accountcode' UNION select '','','','','','',concat('R',sum(round(cost,5))),'' from cdr where calldate > '$prev_date' and calldate < '$cur_date' and cost > 0 and accountcode='$accountcode' INTO OUTFILE '/tmp/VoipBilling-$date/CDR/$accountcode-$date.csv' FIELDS TERMINATED BY ',';"

Error is: ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-/CDR/-.csv' (Errcode: 2)

Note the filepath in the error code and the filepath specified differs. Almost as if Mariadb does not know to create the folder with the date i.e /tmp/VoipBilling-2019-06-22/CDR/BILLTEST-2019-06-22.csv''

Instead it wants to write it as '/tmp/VoipBilling-/CDR/-.csv'

When I run the same command but change the output path to not use the $date it works. i.e

[root@ ]# mysql -pPassword asteriskcdrdb -s -b -e "select 'Account ID','Destination','Operator','Provider','Date','BillSec','Rate id','Cost' UNION select accountcode,dst,'PBX',route_name,date_format(calldate,'%Y/%c/%e %H:%i'),billsec as Duration,route_id,round(cost,5) from cdr where calldate > '$prev_date' and calldate < '$cur_date' and cost > 0 and accountcode='$accountcode' UNION select '','','','','','',concat('R',sum(round(cost,5))),'' from cdr where calldate > '$prev_date' and calldate < '$cur_date' and cost > 0 and accountcode='$accountcode' INTO OUTFILE '/tmp/VoipBilling/CDR/accountcode.csv' FIELDS TERMINATED BY ',';"

So it is safe to say this is not a folder permision but the command not being able to create the folder with $date specified.

Please assist

Does date have spaces in it like June 10 2019? Plus the date shell variable is likely not being expanded when inside single quotes,
e.g., '$cur_date' .

So I change use $cur_date as:

$cur_date

instead of

'$cur_date'

?

--- Post updated at 01:20 AM ---

Also,

Date should be displayed as 2019-06-22

What you have to do (I did this with oracle) is to "format stuff" to preserve translated (expanded values) of you variables. For oracle the single quotes had to still be in there to delimit the "fixed" strings I am guessing you have the same requirement.

Simplified example:

#!/bin/bash
# example.sh
cur_date="2019-06-22"
printf " '%s' " "$cur_date"

run example.sh

$./example.sh
 '2019-06-22'
$

If you need more help get back to us.

The start of my script contains

#!/bin/bash
date=`date +%F`
prev_date=`date --date='-1 month' '+%Y-%m-25 00:00:00'`
cur_date=`date --date='now' '+%Y-%m-25 00:00:00'`
mkdir /tmp/VoipBilling-$date
mkdir /tmp/VoipBilling-$date/CDR
mkdir /tmp/VoipBilling-$date/Summary

When I do the following it creates the file with the date specified

mkdir /file-$date

--- Post updated at 11:47 AM ---

When I run the script I get the following error

ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-23/Summary/POSTPAID-SUMMARY-2019-06-23.csv' (Errcode: 2)
ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-23/Summary/PREPAID-SUMMARY-2019-06-23.csv' (Errcode: 2)
ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-23/Summary/VIRTUAL-PBX-SUMMARY-2019-06-23.csv' (Errcode: 2)
ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-23/CDR/BILLTEST-2019-06-23.csv' (Errcode: 2)
ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-23/CDR/BILLTEST2-2019-06-23.csv' (Errcode: 2)

So it is creating the dates correctly but it seems to not be able to write/create the file. Folder permisions should be fine. I run chmod -R 777 /temp/* at the start of the script

#!/bin/bash
date=`date +%F`
prev_date=`date --date='-1 month' '+%Y-%m-25 00:00:00'`
cur_date=`date --date='now' '+%Y-%m-25 00:00:00'`
mkdir /tmp/VoipBilling-$date
mkdir /tmp/VoipBilling-$date/CDR
mkdir /tmp/VoipBilling-$date/Summary

chmod -R 777 /tmp/*

A script that is creating files with names containing a date set on the 25th day of a few months is not creating diagnostics for dates set on the 23rd day of the month because it was unable to create the files with dates on the 25th of the month. Therefore, the diagnostics you showed us did not come from the script you showed us.

Are other variables in your script trying to create files with dates from the 23rd of the month? Do you need to be creating files with the day of the month set to the 23rd instead of the 25th?

Hi,

No it needs to be on the 25th of the month but I would like to test to see if the script works fine.

I changed the cur_date to the following to test

cur_date=`date --date='now' '+%Y-%m-26 00:00:00'`

to test the script now but still get

ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-26/Summary/POSTPAID-SUMMARY-2019-06-26.csv' (Errcode: 2)
ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-26/Summary/PREPAID-SUMMARY-2019-06-26.csv' (Errcode: 2)
ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-26/Summary/VIRTUAL-PBX-SUMMARY-2019-06-26.csv' (Errcode: 2)
ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-26/CDR/BILLTEST-2019-06-26.csv' (Errcode: 2)
ERROR 1 (HY000) at line 1: Can't create/write to file '/tmp/VoipBilling-2019-06-26/CDR/BILLTEST2-2019-06-26.csv' (Errcode: 2)

Don't leave people guessing, provide adequate info. Check your mysql manual:

  • What does "ERROR 1" mean?

  • What does "Errcode: 2" mean?

  • What's the code in "line 1" that tries to "create/write" the files?

Do the necessary directories exist?
What be their permisions?
Can you create files in there by using shell commands?
Can other users create those files?
Show the results of your attempts.

Here is a link to the error codes:

MySQL :: MySQL 5.6 Reference Manual :: B.3.1 Server Error Message Reference

Error number: 1002; Symbol: ER_NO; SQLSTATE: HY000

Message: NO

Used in the construction of other messages.