MySQL crontab中没有密码的mysqldump

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19904992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:23:18  来源:igfitidea点击:

Mysqldump without password in crontab

mysqlcronbackupmysqldumpcrontab

提问by Marcel Balzer

I try to backup my database with mysqldump and cronjobs.

我尝试使用 mysqldump 和 cronjobs 备份我的数据库。

Well, I added the following command to the crontab of user root:

好吧,我在root用户的crontab中添加了以下命令:

*/30 * * * * mysqldump -u root -pVERYSECUREPASSWORD --all-databases > /var/www/cloud/dump_komplett.sql &> /dev/null

This works fine so far, but the problem is that the password is set in this command.

到目前为止,这工作正常,但问题是密码是在此命令中设置的。

So I want to include a .database.cnf file that look like this

所以我想包含一个看起来像这样的 .database.cnf 文件

[mysqldump]
user=root
password=VERYSECUREPASSWORD

and changed the mysqldump command to

并将 mysqldump 命令更改为

mysqldump --defaults-extra-file="/var/crons/mysql/.database.cnf" --all-databases -u root > /var/www/cloud/dump_komplett.sql

to solve this problem.

来解决这个问题。

But this command fails with the error:

但是此命令失败并显示错误:

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

I don't know what's wrong.

我不知道怎么了。

Here are some commands I also tried:

以下是我也尝试过的一些命令:

mysqldump --defaults-extra-file="/var/crons/mysql/.database.cnf" --all-databases > /var/www/cloud/dump_komplett.sql
mysqldump --defaults-file="/var/crons/mysql/.database.cnf" --all-databases > /var/www/cloud/dump_komplett.sql
mysqldump --defaults-file="/var/crons/mysql/.database.cnf" --all-databases -u root > /var/www/cloud/dump_komplett.sql

and .database.cnf contents I also tried:

和 .database.cnf 内容我也试过:

[client]
user=root
password=VERYSECUREPASSWORD

[mysqldump]
host=localhost
user=root
password=VERYSECUREPASSWORD

[client]
host=localhost
user=root
password=VERYSECUREPASSWORD

采纳答案by u1860929

The user has to be specified in the command and not in the file with the uparameter.

必须在命令中而不是在带有u参数的文件中指定用户。

For more details on scheduling cronjobs using mysqldump, check this answer

有关cron使用调度作业的更多详细信息mysqldump,请查看此答案

回答by NomadHack

I found out that password should be between quotes

我发现密码应该在引号之间

[client]
user=root
password="VERYSECUREPASSWORD"

Took me a while to figure out why it didn't work with passwords with lots of non alfanumeric symbols

我花了一段时间才弄清楚为什么它不能使用包含大量非字母数字符号的密码