MySQL 为什么数据库表上的基本 MySQLdump 会因“权限被拒绝”而失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8663454/
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 22:11:21  来源:igfitidea点击:

why does basic MySQLdump on db table fail with "Permission denied"

mysqlmysqldump

提问by govinda

This should be quick and simple, but after researching on Google quite a bit I am still stumped. I am mostly newbie with: server admin, CLI, MySQL.

这应该是快速而简单的,但在对谷歌进行了相当多的研究后,我仍然被难住了。我主要是新手:服务器管理员、CLI、MySQL。

I am developing my PHP site locally, and now need to move some new MySQL tables from my local dev setup to the remote testing site. First step for me is just to dump the tables, one at a time.

我正在本地开发我的 PHP 站点,现在需要将一些新的 MySQL 表从我的本地开发设置移动到远程测试站点。对我来说,第一步只是转储表格,一次一个。

I successfully login to my local MySQL like so:

我像这样成功登录到我的本地 MySQL:

Govind% /usr/local/mysql/bin/mysql -uroot

but while in this dir (and NOT logged into MySQL):

但是在这个目录中(并且没有登录到 MySQL):

/usr/local/mysql/bin

...when I try this

...当我尝试这个时

mysqldump -uroot -p myDBname myTableName > myTestDumpedTable.sql

..then I keep getting this:

..然后我不断得到这个:

"myTestDumpedTable.sql: Permission denied."

Same result if I do any variation on that (try to dump the whole db, drop the '-p', etc.)

如果我对此进行任何更改,结果相同(尝试转储整个数据库,删除“-p”等)

I am embarrassed as I am sure this is going to be incredibly simple, or just reveal a gaping (basic) hole in my knowledge. .. but please help ;-)

我很尴尬,因为我确信这将非常简单,或者只是揭示了我知识中的一个巨大(基本)漏洞。..但请帮忙;-)

回答by govinda

The answer came from a helpful person on the MySQL list:
As you guys (Anson and krazybean) were thinking - I did not have permission to be writing to the /usr/local/mysql/bin/dir. But starting from any other directory, calls to mysqldumpwere failing because my shell PATH var (if I said that right) is not yet set up to handle mysqldumpfrom another dir. Also, for some reason I do not really understand yet, I also needed to use a full path on the output, even if I was calling mysqldump effectively, and even if I had permission to write to the output dir (e.g. ~/myTestDumpedTable.sql. So here was my ticket, for now (quick answer):

答案来自 MySQL 列表中的一个乐于助人的人:
正如你们(Anson 和 krazybean)所想的那样 - 我没有写信给/usr/local/mysql/bin/目录的权限。但是从任何其他目录开始,对 的调用mysqldump都失败了,因为我的 shell PATH var(如果我说的是对的)尚未设置为mysqldump从另一个目录处理。此外,由于某种原因,我还不太明白,我还需要在输出上使用完整路径,即使我有效地调用了 mysqldump,即使我有写入输出目录的权限(例如~/myTestDumpedTable.sql。所以这里是我的票,现在(快速回答):

Govind% /usr/local/mysql/bin/mysqldump -uroot -p myDBname myTableName > /Users/Govind/myTestDumpedTable.sql

You can write to wherever your shell user has permission to do so. I just chose my user's home dir.

您可以写入 shell 用户有权这样做的任何地方。我只是选择了我用户的主目录。

Hope this helps someone someday.
Cheers.

希望有一天这对某人有所帮助。
干杯。

回答by krazybean

Generally I stick with defining the hostname anyways, but as you being root doesn't seem like it would be the problem, I would question where are you writing this to? What happens when you dump to > ~/myTestDumpedTable.sql

一般来说,我仍然坚持定义主机名,但是由于您是 root 用户,这似乎不是问题,我会问您要将它写到哪里?当你转储到 > ~/myTestDumpedTable.sql 时会发生什么

回答by Pathros

In my case I'd created the directory with $ sudo mkdir /directory/to/store/sql/files. The owner of that directory is root. So changing the owner by using $ sudo chown me:me /directory/to/store/sql/filesand also changing permissions to maybe $ sudo chmod 744 /directory/to/store/sql/filesdid the trick for me.

就我而言,我使用$ sudo mkdir /directory/to/store/sql/files. 该目录的所有者是root. 因此,通过使用$ sudo chown me:me /directory/to/store/sql/files和更改权限来更改所有者可能$ sudo chmod 744 /directory/to/store/sql/files对我有用。

回答by Anson

Take a look at the man page for mysqldump for correct argument usage. You need a space between the -uflag and the username, like so:

查看 mysqldump 的手册页以了解正确的参数用法。您需要在-u标志和用户名之间留一个空格,如下所示:

mysqldump -u root -p myDBname myTableName > myTestDumpedTable.sql

Alternatively you can do

或者你可以做

mysqldump --user=root -p myDBname myTableName > myTestDumpedTable.sql

Since you're not providing a password in the list of arguments, you should be prompted for one. You can always provide the password in the list of arguments, but the downside to that is it appears in cleartext and will show up in the shell's command history.

由于您没有在参数列表中提供密码,因此应该提示您输入密码。您始终可以在参数列表中提供密码,但缺点是它以明文形式出现,并将显示在 shell 的命令历史记录中。

回答by Lisong

You should provide with a full path for SQL backup file, such as

您应该提供 SQL 备份文件的完整路径,例如

mysqldump -u root -p databasexxx > /Users/yourusername/Sites/yoursqlfile.sql

回答by Agusti Febrer

I think you're missing the ./from the command, try: being inside

我认为您错过了./命令,请尝试:在里面

/usr/local/mysql/bin$ ./mysqldump -u root -p myDBname > "/Users/yourUserName/Documents/myTestDumpedTable.sql"

So it is a script, and in linux you execute a script with ./myscript. I found it just today, and for me, in my mac OSX, I didn't use the -p, maybe because password not needed, don't know already. I mean, try also:

所以它是一个脚本,在 linux 中你用./myscript. 我今天才发现它,对我来说,在我的 Mac OSX 中,我没有使用-p,也许是因为不需要密码,我还不知道。我的意思是,也尝试:

./mysqldump -u root myDBname > "/Users/yourUserName/Documents/myTestDumpedTable.sql"

回答by Jeevan

Even I was facing the same problem, the issue is with user access to 'root/bin' dir.

即使我遇到了同样的问题,问题也在于用户对“root/bin”目录的访问权限。

switch your user access as root

以 root 身份切换您的用户访问权限

    sudo -s

Then execute the command

然后执行命令

mysqldump -uroot -p homestayadvisorDB > homestayadvisor_backup.sql

This will resolve the issue. Let me know if this doesn't work.

这将解决问题。如果这不起作用,请告诉我。