我想知道在没有提示密码的情况下执行数据库mysqldump的命令。

原因: 我想运行一个cron作业,每天对数据库进行一次mysqldump。因此,当提示时,我将无法插入密码。

我怎么解决这个问题呢?


当前回答

当然,我认为把完整的cmd行放在根crontab中会更好,更安全。 至少crontab编辑被限制(可读)给已经知道密码的人。所以不用担心用纯文本显示它……

如果需要一个简单的mysqldump…只需放置一个bash脚本,接受cred作为参数,并在里面执行所有设施…

bas文件在simple

#!/bin/bash
mysqldump -u$1 -p$2 yourdbname > /your/path/save.sql

在Crontab中:

0 0 * * * bash /path/to/above/bash/file.sh root secretpwd 2>&1 /var/log/mycustomMysqlDump.log

其他回答

检查你的密码!

Took me a while to notice that I was not using the correct user name and password in ~/.my.cnf Check the user/pass basics before adding in extra options to crontab backup entries If specifying --defaults-extra-file in mysqldump then this has to be the first option A cron job works fine with .my.cnf in the home folder so there is no need to specify --defaults-extra-file If using mysqlpump (not mysqldump) amend .my.cnf accordingly The ~/.my.cnf needs permissions set so only the owner has read/write access with: chmod 600 ~/.my.cnf

下面是一个例子。my.cnf:

[mysql]
host = localhost
port = 3306
user = BACKUP_USER
password = CORRECTBATTERYHORSESTAPLE
[mysqldump]
host = localhost
port = 3306
user = BACKUP_USER
password = CORRECTBATTERYHORSESTAPLE
[mysqlpump]
host = localhost
port = 3306
user = BACKUP_USER
password = CORRECTBATTERYHORSESTAPLE

localhost不需要主机和端口条目 如果您在linux中的用户名与备份时使用的用户名相同,则不需要使用user

另一个提示,当你为mysqldump执行cronjob条目时,你可以用ionice -c将它设置为低优先级任务。结合InnoDB的——single-transaction选项,你可以运行备份,不会锁定表或锁定其他地方可能需要的资源。

我用不同的方式,使用Plink(Putty命令行)连接到remotehost,然后下面的命令是在远程服务器上运行的Plink文件中,然后我使用RSYNC从windows获取它并备份到onprem NAS。

sudo mysqldump -u root --all-databases --events --routines --single-transaction > dump.sql

我在远程主机上设置了密钥,并使用PowerShell,通过任务调度器每周运行一次。

——password="" 在5.1.51上运行对我有用

mysqldump -h localhost -u <user> --password="<password>"

对我来说,使用MariaDB我必须这样做:添加文件~/.my.cnf,并通过执行chmod 600 ~/.my.cnf更改权限。然后将您的凭据添加到文件中。神奇的是,我错过了密码需要在客户端块下(参考:docs),就像这样:

[client]
password = "my_password"

[mysqldump]
user = root
host = localhost

如果你碰巧来这里寻找如何用MariaDB做mysqldump。将密码放在[client]块下,然后将用户放在[mysqldump]块下。

下面是Docker在/bin/sh脚本中的解决方案:

docker exec [MYSQL_CONTAINER_NAME] sh -c 'exec echo "[client]" > /root/mysql-credentials.cnf'

docker exec [MYSQL_CONTAINER_NAME] sh -c 'exec echo "user=root" >> /root/mysql-credentials.cnf'

docker exec [MYSQL_CONTAINER_NAME] sh -c 'exec echo "password=$MYSQL_ROOT_PASSWORD" >> /root/mysql-credentials.cnf'

docker exec [MYSQL_CONTAINER_NAME] sh -c 'exec mysqldump --defaults-extra-file=/root/mysql-credentials.cnf --all-databases'

替换[MYSQL_CONTAINER_NAME],并确保在容器中设置了环境变量MYSQL_ROOT_PASSWORD。

希望它能像帮助我一样帮助你!