Correct MySQL username/password, but getting Access Denied error when run from script

Posted by Nick on Server Fault See other posts from Server Fault or by Nick
Published on 2011-08-03T04:20:25Z Indexed on 2014/08/21 10:22 UTC
Read the original article Hit count: 302

Filed under:

I'm currently trying to run the following command from within a shell script.

/usr/bin/mysql -u username -ppassword -h localhost database

It works perfectly fine when executed manually, and not from within a script. When I try to execute a script that contains that command, I get the following error:

ERROR 1045 (28000) at line 3: Access denied for user 'username'@'localhost' (using password: YES)

I literally copied and pasted the working command into the script. Why the error? As a sidenote: the ultimate intent is to run the script with cron.

EDIT: Here is a stripped down version of my script that I'm trying to run. You can ignore most of it up until the point where it connects to MySQL around line 19.

#!/bin/sh
#Run download script to download product data
cd /home/dir/Scripts/Linux
/bin/sh script1.sh

#Run import script to import product data to MySQL
cd /home/dir/Mysql
/bin/sh script2.sh

#Download inventory stats spreadsheet and rename it
cd /home/dir
/usr/bin/wget http://www.url.com/file1.txt
mv file1.txt sheet1.csv

#Remove existing export spreadsheet
rm /tmp/sheet2.csv

#Run MySQL queries in "here document" format
/usr/bin/mysql -u username -ppassword -h localhost database << EOF

--Drop old inventory stats table
truncate table table_name1;

--Load new inventory stats into table
Load data local infile '/home/dir/sheet1.csv' into table table_name1 fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';

--MySQL queries to combine product data and inventory stats here

--Export combined data in spreadsheet format
group by p.value into outfile '/tmp/sheet2.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';
EOF

EDIT 2: After some more testing, the issue is with the << EOF that is at the end of the command. This is there for the "here document". When removed, the command works fine. The problem is that I need << EOF there so that the MySQL queries will run.

© Server Fault or respective owner

Related posts about mysql