Score:-1

Running a bash shell using a password with a cron job

cn flag

I have a bash shell to back up a database that requires a password to open it. I want to run on a cron job, but how do I make it run automatically without filling in the database password?

ubuntu server version : 20.04.3 Mysql version : 8.0.26

The command i tried : mysqldump -u root -p "dbname" > backupname.sql when I press enter, the system asks for password. when i remove -p, error occurs.

David avatar
cn flag
What does the script look like?
cn flag
Ray
Does [this](https://dba.stackexchange.com/questions/72417/credentials-file-for-running-mysql-related-jobs-via-script) help?
in flag
If you are using MySQL on a modern version of Ubuntu, and you haven't intentionally overridden the permissions mechanism of the software, then you do not need to supply a password when doing a backup via `mysqldump`. The `root` system account will connect to the `root` MySQL account without a password, as this is part of administration
Rendiero avatar
cn flag
@David I haven't written the command yet because I want to know how to do it first
Rendiero avatar
cn flag
@Ray it looks like entering user password, not mysql database password
Rendiero avatar
cn flag
@matigo I have tried, but the system asks to enter the mysql database password
in flag
Please [edit] your question to answer these questions: (1) What version of Ubuntu are you running? (2) What version of MySQL are you running? (3) What *exactly* have you tried? (with code samples) — Until these three questions are answered, it will be almost impossible for anyone to offer any sort of solution.
Rendiero avatar
cn flag
@matigo I have edited it, can you help?
Score:0
in flag

In order to use mysqldump with the root MySQL account, you will need to prefix your command with sudo:

sudo mysqldump database_name > backup.sql

If you would like to run this via a cron job, then you can do something like this:

0 3 * * * mysqldump database_name > /path/to/backups/backup.sql

As a bonus, if you would like to compress that backup and have the file name contain the date, you can do something like this:

0 3 * * * mysqldump database_name | gzip > /path/to/backups/backup_$(date +\%Y-\%m-\%d).gz

Be sure to include the necessary --routines and --triggers options in your mysqldump command if your database contains either stored procedures or triggers.

Rendiero avatar
cn flag
Thank you, i'll try.
Score:0
in flag

Basically, you can either add the following to you my.cnffile

[mysqldump]
user=mysqluser
password=secret

Alternatively, you can export the following before calling mysqldump

MYSQL_PWD=yourverysecretpassword

Using the -p parameter as you tried is strongly discouraged for security reasons.

More info and extended solutions on https://stackoverflow.com/questions/9293042/how-to-perform-a-mysqldump-without-a-password-prompt

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.