如何在没有密码提示的情况下执行 mysqldump?
我想知道在不提示输入密码的情况下执行数据库 mysqldump 的命令.
I would like to know the command to perform a mysqldump of a database without the prompt for the password.
原因:我想运行一个 cron 作业,它每天对数据库进行一次 mysqldump.因此,我将无法在提示时插入密码.
REASON: I would like to run a cron job, which takes a mysqldump of the database once everyday. Therefore, I won't be able to insert the password when prompted.
我该如何解决这个问题?
How could I solve this ?
推荐答案
由于您使用的是 Ubuntu,因此您只需在主目录中添加一个文件,它就会禁用 mysqldump 密码提示.这是通过创建文件 ~/.my.cnf
(权限需要为 600)来完成的.
Since you are using Ubuntu, all you need to do is just to add a file in your home directory and it will disable the mysqldump password prompting. This is done by creating the file ~/.my.cnf
(permissions need to be 600).
将此添加到 .my.cnf
文件
[mysqldump]
user=mysqluser
password=secret
这使您可以作为需要密码的 MySQL 用户进行连接,而无需实际输入密码.您甚至不需要 -p 或 --password.
This lets you connect as a MySQL user who requires a password without having to actually enter the password. You don't even need the -p or --password.
对编写 mysql 脚本非常方便 &mysqldump 命令.
Very handy for scripting mysql & mysqldump commands.
实现此目的的步骤可以在 此链接.
The steps to achieve this can be found in this link.
或者,您可以使用以下命令:
Alternatively, you could use the following command:
mysqldump -u [user name] -p[password] [database name] > [dump file]
但请注意,它本质上是不安全的,因为在转储运行时,系统上的任何其他用户都可以使用简单的 ps ax
命令查看整个命令(包括密码).
but be aware that it is inherently insecure, as the entire command (including password) can be viewed by any other user on the system while the dump is running, with a simple ps ax
command.
相关文章