用于备份 MySQL 数据库的 PHP 脚本

发布于 2024-08-13 21:08:42 字数 979 浏览 3 评论 0原文

我正在尝试编写一个 PHP 脚本来备份 MySQL 数据库:

   if ( $db_resource = mysql_connect($db_server, $db_username, $db_password, $db_newlink) )
   {
      if ( mysql_select_db( $db_name, $db_resource ) )
      {
         $backupFile = $db_name."_".date( "Y-m-d-H-i-s" ).".gz";
         $command = "mysqldump --opt -h ".$db_server." -u ".$db_username." -p ".$db_password." ".$db_name." | gzip > ".$db_save_dir."/".$backupFile;
         system( $command );
      }
   }

   mysql_close( $db_resource );

当我从 shell 终端运行它时,我得到以下信息:

[stingray]$ php /[绝对 路径]/db_backup.php

输入密码:[我输入密码]

mysqldump:出现错误:1044:访问 用户被拒绝 '[用户名]'@'208.113.128.0/255.255.128.0' 到数据库“[PASSWORD]”时 选择数据库

好吧,现在我真正不明白的是为什么它调用数据库作为我的密码。如果我将网络浏览器指向该文件,它就会正常运行。有谁知道我应该做什么?就我个人而言,我真的不在乎它是 PHP、Python、CGI 等,只要它能在 Apache 服务器上运行即可。

谢谢。

I'm trying to write a PHP script to backup a MySQL database:

   if ( $db_resource = mysql_connect($db_server, $db_username, $db_password, $db_newlink) )
   {
      if ( mysql_select_db( $db_name, $db_resource ) )
      {
         $backupFile = $db_name."_".date( "Y-m-d-H-i-s" ).".gz";
         $command = "mysqldump --opt -h ".$db_server." -u ".$db_username." -p ".$db_password." ".$db_name." | gzip > ".$db_save_dir."/".$backupFile;
         system( $command );
      }
   }

   mysql_close( $db_resource );

When I run it from the shell terminal, I get this:

[stingray]$ php /[ABSOLUTE
PATH
]/db_backup.php

Enter password: [I INPUT PASSWORD]

mysqldump: Got error: 1044: Access
denied for user
'[USERNAME]'@'208.113.128.0/255.255.128.0'
to database '[PASSWORD]' when
selecting the database

Okay, now what I really don't understand is why it is calling the database as my password. If I point my web browser at the file, it runs just fine. Does anyone know what I should be doing? Personally, I really don't care if it's PHP, Python, CGI, etc., just so long as it can run on an Apache server.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感性 2024-08-20 21:08:42

密码标志的语法不同。删除 -p 标志后面的空格并再试一次。

The syntax for the password flag is different. Remove the space after the -p flag and give it another shot.

半枫 2024-08-20 21:08:42

尝试使用完整的选项名称;我遇到了类似的问题,但没有时间解决失败的真正根源;然而这对我有用:

/usr/bin/mysqldump \
--user=username \
--password=password \
dbname > mysqldump.sql

try use the full option names; i had a similar issue but no time to tackle the real root of the failure; however this worked and works for me:

/usr/bin/mysqldump \
--user=username \
--password=password \
dbname > mysqldump.sql
花心好男孩 2024-08-20 21:08:42

1:
-p 和 PASSWORD 之间没有空格
例如:-p“.$db_password。”

2:为该数据库的特定用户授予所有(如果需要)权限。

3:给予文件正确的权限来执行命令。

1:
no space between -p and PASSWORD
ex: -p".$db_password."

2: grant All (if you want) privileges for a specific user for that database.

3: give the file correct permission to execute the command.

情愿 2024-08-20 21:08:42

如果你只是备份东西,我强烈建议使用 这个非常简单、非常可配置的开源 cron 备份脚本。它太棒了,我想如果这就是你所追求的,你会喜欢它的。

如果确实需要通过 apache 运行,您可以使用反引号远程触发脚本,然后免费获得备份轮换和每周/每月备份、压缩和日志记录。

If you're just backing things up I would strongly recommend using this very easy, very configurable, open source cron backup script. It's superb and I think you'll like it if that's all you're after.

If it's really needed to be run through apache you could use backticks to remotely trigger the script and then you get backup rotations and weekly/monthly backups, compression and logging for free.

以为你会在 2024-08-20 21:08:42

我建议你使用SqlBuddy,它解决了我的问题,就像你的一样。

仅当您想使用 cron 或自动化脚本时才继续使用自定义脚本。

I suggest you to use SqlBuddy, it solved my problem like your.

Proceed with a custom script only if you want to use cron or an automated script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文