mysqldump --password 真的按照它说的做吗?
我正在尝试使用 mysqldump 转储模式,它基本上可以工作,但我遇到了一个好奇心:-p
或 --password
选项似乎除了设置密码之外还做了其他事情(正如 man
页面和 --help
输出所说的那样)。
具体来说,它看起来正在执行此处所示的操作: http://snippets.dzone.com/posts /show/360 - 即将数据库设置为转储。
为了支持我有点奇怪的主张,我可以告诉您,如果我不指定 --password
(或 -p
)选项,该命令将打印使用语句并退出有错误。 如果我确实指定了它,系统会立即提示我输入密码 (!),然后转储 --password
选项中指定的数据库(或者在通常情况下给出错误,即指定的密码与任何数据库名称都不匹配)。
这是一份文字记录:
$ mysqldump -u test -h myhost --no-data --tables --password lose Enter password: -- MySQL dump 10.10 mysqldump: Got error: 1044: Access denied for user 'test'@'%' to database 'lose' when selecting the database
那么,是什么给出了呢? 这是应该的工作方式吗? 它看起来显然没有意义,也不符合官方文档。 最后,如果这就是它的工作方式,我该如何指定在自动化作业中使用的密码? 使用 expect
???
我正在使用 mysqldump Ver 10.10 Distrib 5.0.22,适用于 pc-linux-gnu (i486)。
I'm trying to use mysqldump
to dump a schema, and it mostly works but I ran into one curiosity: the -p
or --password
option seems like it is doing something other than setting the password (as the man
page and --help
output say it should).
Specifically, it looks like it's doing what is indicated here: http://snippets.dzone.com/posts/show/360 - that is, setting the database to dump.
To support my somewhat outlandish claim, I can tell you that if I do not specify the --password
(or -p
) option, the command prints the usage statement and exits with an error. If I do specify it, I am immediately prompted to enter a password (!), and then the database specified in the --password
option is dumped (or an error is given in the usual case that a password not matching any database name was specified).
Here's a transcript:
$ mysqldump -u test -h myhost --no-data --tables --password lose Enter password: -- MySQL dump 10.10 mysqldump: Got error: 1044: Access denied for user 'test'@'%' to database 'lose' when selecting the database
So, what gives? Is this the way this is supposed to work? It surely does not appear to make sense nor does it match the official documentation. And finally, if this just the way it works, how am I meant to specify the password to be used in an automated job? Using expect
???
I'm using mysqldump Ver 10.10 Distrib 5.0.22, for pc-linux-gnu (i486)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
来自 man mysqldump:
从语法上讲,您没有正确使用 --password 开关。 因此,命令行解析器将您使用的“lose”视为独立参数,mysqldump 将其解释为数据库名称,就像您尝试使用 mysqldump Lose 这样的更简单的命令一样
。更正此问题,尝试使用
--password=lose
或-plose
或仅使用-p
或--password
并在出现提示时输入密码。From man mysqldump:
Syntactically, you are not using the --password switch correctly. As such, the command line parser is seeing your use of "lose" as a stand-alone argument which mysqldump interprets as the database name as it would if you were to attempt a simpler command like
mysqldump lose
To correct this, try using
--password=lose
or-plose
or simply use-p
or--password
and type the password when prompted.另一种选择是创建文件~/.my.cnf(权限需要为600)。
将其添加到 .my.cnf 文件中
这使您可以作为需要密码的 MySQL 用户进行连接,而无需实际输入密码。 您甚至不需要 -p 或 --password。
对于编写 mysql 和 mysql 脚本非常方便 mysqldump 命令。
Another option is to create the file ~/.my.cnf (permissions need to be 600).
Add this to the .my.cnf file
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.
Very handy for scripting mysql & mysqldump commands.
我发现如果您的密码中包含特殊字符,就会发生这种情况。 这里的mysql密码有一个! 在其中,所以我必须执行
--password='xxx!xxxx'
才能正常工作。 注意 ' 标记。I found that this happens if your password has special characters in it. The mysql password here has a ! in it, so I have to do
--password='xxx!xxxx'
for it to work corrrectly. Note the ' marks.尝试在 --password loss 之间放置一个“=”,例如:
如果使用 -p,则 -p 和密码之间不能有空格,即“-plose”。
Try placing a '=' in between --password lose like:
If you use -p, then there can be no space between the -p and the password, i.e. '-plose'.
您尝试过 --password=whatever-password-is 吗?
也许我错过了这个问题,但这就是我运行该工具的方法。
Did you try --password=whatever-password-is ?
Perhaps I'm missing the question, but that is what I do to run the tool.
我不确定它是否适用于 --password 版本,但是如果您使用 -p 您可以在之后立即指定密码(关键是不要包含空格):
mysqldump -pmypassword ...
I am not sure if it works for the --password version, but if you use -p you can specify the password immediately afterwards (the key is not to include a space):
mysqldump -pmypassword ...
如果您使用不带参数的 -p 或 --password,您将收到提示,要求插入密码。
如果要在命令行上指示密码,则必须使用 -pYOURPASSWORD 或 --password=YOURPASSWORD。 请注意,-p 后面没有空格,--password 后面有一个“=”符号。
在您的示例中,mysqldump 要求输入密码,然后将“lose”视为数据库名称。 如果这是您的密码,您应该包含一个“=”
If you use the -p or --password without an argument, you will get a prompt, asking to insert a password.
If you want to indicate a password on the command line, you must use -pYOURPASSWORD or --password=YOURPASSWORD. Notice that there is no space after -p, and there is an "=" sign after --password.
In your example, mysqldump asks for a password, and then treats "lose" as the database name. If that was your password, you should have included a "="
-p 选项不需要参数。 您只需输入 -p 或 --password 来指示您将使用密码来访问数据库。 它转储在 -p 之后命名的数据库的原因是 mysqldump 的最后一个参数应该是您要转储的数据库的名称(如果您想要全部数据库,则为 --all-databases )。
@Nathan 的答案也是正确的。 您可以在 -p 开关之后立即指定密码(在脚本中非常有用,在执行命令后无法手动输入密码的情况下)。
The -p option does not require an argument. You just put -p or --password to indicate that you're going to use a password to access the database. The reason it's dumping the database named whatever you put after -p is that the last argument for mysqldump should be the name of the database you want to dump (or --all-databases if you want them all).
@Nathan's answer is also true. You can specify the password immediately following the -p switch (useful in scripts and such where you can't enter it by hand after executing the command).
--password[=password]
这是文档
--password[=password]
Here is the documentation
也许您的用户“test”没有访问您的“lose”数据库的权限?
Maybe your user "test" doesn't have the permission to access your "lose" database?