问题
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.
回答1:
The syntax for the password flag is different. Remove the space after the -p flag and give it another shot.
回答2:
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
回答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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/1867797/php-script-to-backup-mysql-database