I created a bash script that backs up my database, db_backup.sh

It basically just runs these commands:

mysqldump --login-path=local db_name | gzip > "$DB"

When I run it from the command line as linuxuser or as root, it works.

But when I add it to cron.d, I get a mysql error and the sql.gz file is empty.

mysqldump: Got error: 1045: Access denied for user 'linuxuser'@'localhost' (using password: NO) when trying to connect

(I get the same type of error when running the cron as root, too)

How can I fix this? Could it be a problem with environment variables?

I tried changing:




... but that did not help.

2 Answers 2


Just copy the PATH declaration in your env which able to run the mysql --login-path and paste into your script file for cron to execute.

The facts is that the cron won't read your mysql path if you does not pass the PATH.

Cron tasks do not run as user=you. So, all necessary paths need to be specified.


mysqldump needs -u and -p for some mysql "user".

  • mysqldump needs -u and -p for some mysql "user". Um, no it doesn't? As I said, the script works when run from the command line. It only fails in cron. Are you familiar with --login-path=local ? Commented Apr 17, 2015 at 4:09
  • cron runs as someone else, perhaps "nobody".
    – Rick James
    Commented Apr 17, 2015 at 4:43
  • Actually, I have to clarify the error. The user that is denied is the system "linuxuser", but the user specified in --login-path=local directive would be a mysql user and the password IS specified there. When I run the script from the terminal, the mysql user/password are passed in, but it seems like they are not when run from CRON. So mysql, I think, defaults to the current system user ("linuxuser" is specified in the cron), and no password is supplied. --login-path is not working in the cron. Commented Apr 17, 2015 at 7:09
  • So, in a sense, you are right that mysqldump needs a user and password, but the --login-path is supposed to supply it and it's not working in the cron. Commented Apr 17, 2015 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.