Shresth Suman
Shresth Suman

Reputation: 29

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

I am trying to do a mysqldump using the following command: mysqldump -u root -A -R -E --triggers --single-transaction > /home/backups/full_backup_${TODAY}.sql

This command runs fine and no error comes when I login and run as a root user. However, while running it as a daily job using systemd/timers, this command throws an error -

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

Here are my config details -

I have saved my credentials in ~/.my.cnf as:

[client]
password = "mypassword"

[mysqldump]
user = root
host = localhost

permission levels of /etc/systemd/system/backup.service and /etc/systemd/system/backup.timer

-rwxrwxrwx  1 root root   90 Nov  5 17:34 backup.service*
-rwxrwxrwx  1 root root  132 Nov 11 10:15 backup.timer*

permission levels of ~/.my.cnf

-rw-------  1 root root    76 Nov  5 15:57 .my.cnf

permission level of /home/backups and backup.sh which is executed by backup.timer and backup.service

-rw-------  1 root         root   1872 Nov 11 10:11 backup.sh
drw-------  2 iftvdev      users  4096 Nov 11 10:27 backups/

My backup.timer file -

[Unit]
Description=backup

[Timer]
OnCalendar=*-*-* 10:17:00
Persistent=true
Unit=backup.service

[Install]
WantedBy=timers.target

and backup.service file -

[Unit]
Description=backup job

[Service]
Type=oneshot
ExecStart=/bin/bash /home/backup.sh

Upvotes: 0

Views: 6877

Answers (1)

Shresth Suman
Shresth Suman

Reputation: 29

I just created a new folder inside my home directory and passed defaults-extra-file with my mysqldump command and it worked.

This fixed it - https://serverfault.com/questions/671759/cronjob-executing-mysql-and-get-root-access-denied

Upvotes: 1

Related Questions