Access denied for user 'root'@'localhost'

Hello all, hope you are doing well.
i’m trying to automate a mysqldump on the mariadb-container.
I’ve created a brand new container with docker-compose:
version: “2”
image: linuxserver/mariadb
container_name: mariadb
- ./mariadb.env
- /volume1/docker/mariadb:/config
- 4002:3306
restart: unless-stopped

I want to have a daily backup of my db on this container. I use this command for the backup:
docker exec mariadb bash -c ‘mysqldump -u root gitea > /config/backup/gitea-sqlbkp_date +"%Y%m%d".sql’

i have put the username and password in the custom.cnf under [mysqldump]

when i wan to execute the scrips as root i got the following error:
mysqldump: Got error: 1045: “Access denied for user ‘root’@‘localhost’ (using password: NO)” when trying to connect

when i runn the comand separately in the container no error ocures:
mysqldump -u root -p gitea > /config/backup/gitea-sqlbkp_date +"%Y%m%d".sql

do i miss something?
i run the container on a synology…

any help is really apreciated!!!

regards ETfraXoR

Are you connecting via SSH?

Hi, yes Sir, i’m using SSH.
i run the command above in a with executable rights.
on my Synology i have a Cronjob that runs the script once a day.
As i understand, the Cronjob is run as root, such i don’t need the sudo in my script.
Also to mention, when i have the Option in the custom.cnf
then the error wont show up, but is absolutely NOT a good practice to have this option enabled.

it tells you the issue in the error result, you didn’t provide the password.
Access denied for user ‘root’@‘localhost’ (using password: NO)
apparently however you’ve set your password for custom.cnf isn’t correct.

skip-grant-tables only works because you’ve completely eliminated all credential reading.

i’m totaly agree with the pints you’ve mentioned!
I’ve testet the following:

  1. in the shell sudo docker exec mariadb bash -c 'mysqldump -u root mysql > /config/backup/mysql-sqlbkp_date +"%Y%m%d".sql'
  2. run the script with sudo sh which contains the command in stap 1. (without the sudo)
  3. run the cronjob from GUI

all 3 resulting the error:
mysqldump: Got error: 1045: "Access denied for user 'root'@'localhost' (using password: YES)" when trying to connect
the output of using password: YES is due that i added the -u root in the command:

whtat i have checked:

  • access on the config.cnf
  • access on the bash-script and if it it’s executable
  • trippe checked the password in the custom.cnf

do i have to check something else?

As i sayed, when i’m connected into the container via Terminal, and execute the mysqldump command prompts me for the PW and when i insert the PW everything works just as it should.

thanks agian for the help

password: yes would be from providing a password, which typically would be the -p option.

You are simply either not passing the correct password from your cnf or localhost isn’t allowed to login (which would be pretty weird, as the default for root is localhost only)

the fact that you can do this from the terminal further proves the issue is either your script or your .cnf file.