user398341
user398341

Reputation: 6577

MySQL database restore using PHP

I'm trying to restore MySQL dump created the following way:

$file = '/path/to/file.sql';
exec('mysqldump -u '.DB_USER.' -p'.DB_PASS.' '.DB_NAME.' > '.$file);

the above creates the dump as expected, then to restore I'm trying to use the following:

$file = '/path/to/file.sql';
exec('mysql -u '.DB_USER.' -p'.DB_PASS.' '.DB_NAME.' < '.$file);

but for some reason it doesn't do anything.

Please note that the constants contain the relevant database connection parameters.

Any idea what I'm doing wrong?

Upvotes: 1

Views: 3748

Answers (2)

MacMac
MacMac

Reputation: 35301

$file = realpath('file.sql');
exec('mysqldump -u ' . DB_USER . ' -p' . DB_PASS . ' ' . DB_NAME . ' > ' . $file);

Perhaps try this.

Upvotes: 0

chx
chx

Reputation: 11760

use mysql -e 'source $file' instead of redirection

Upvotes: 1

Related Questions