Backing Up Your Database With MAMP
Craft relies on the mysqldump
command being available to the PHP process (via exec()
) to create database backups.
This is the most reliable way to dump a database (large or small) without hitting resource limits. The problem for MAMP users, however, is that its mysql
and mysqldump
executables are not available to the PHP process during HTTP requests.
Craft provides two configuration settings to help with this: backupCommand
and restoreCommand
.
You can set these directly via environment overrides, so there’s no need to touch general.php
or any other project files.
CRAFT_BACKUP_COMMAND="/Applications/MAMP/Library/bin/mysql80/bin/mysqldump -h localhost -u root -proot --add-drop-table --comments --create-options --dump-date --no-autocommit --routines --set-charset --triggers --single-transaction --no-data --result-file=\"{file}\" {database} && /Applications/MAMP/Library/bin/mysqldump -h localhost -u root -proot --add-drop-table --comments --create-options --dump-date --no-autocommit --routines --set-charset --triggers --no-create-info --ignore-table={database}.assetindexdata --ignore-table={database}.assettransformindex --ignore-table={database}.cache --ignore-table={database}.sessions --ignore-table={database}.templatecaches --ignore-table={database}.templatecachecriteria --ignore-table={database}.templatecacheelements --ignore-table={database}.templatecachequeries {database} >> \"{file}\""
CRAFT_RESTORE_COMMAND="/Applications/MAMP/Library/bin/mysql80/bin/mysql -h localhost -u root -proot {database} < \"{file}\""
Copy and paste these variables exactly as they appear above into your .env
file, each on their own line. If you changed your MySQL password via MAMP’s preferences, find and replace -proot
with -pyourpassword
.
These commands replace Craft’s defaults by using the full filesystem path to MAMP’s own mysql
and mysqldump
executables (/Applications/MAMP/Library/bin/mysql80/bin/mysqldump
).
In MAMP 7, MySQL binaries were split into version-specific folders. These examples use MAMP’s bundled MySQL 8.0; replace mysql80
with mysql57
to use MySQL 5.7.
Note that Craft 5 requires at least MySQL 8.0.17!
Using a Closure #
As of Craft 5.1 and 4.9, you can set backupCommand
and restoreCommand
to a closure in general.php
and modify only the base executable path:
use craft\config\GeneralConfig;
use craft\helpers\App;
return GeneralConfig::create()
// ...
->backupCommand(function($cmd) {
return $cmd->setCommand('/Applications/MAMP/Libary/bin/mysql80/bin/mysqlbackup');
})
;
This allows Craft to continue using all its default flags and connection details. To make this compatible with other environments, you can check a variable before overriding the command:
use craft\config\GeneralConfig;
use craft\helpers\App;
return GeneralConfig::create()
// ...
->backupCommand(function($cmd) {
if (App::env('USE_MAMP')) {
$cmd->setCommand('/Applications/MAMP/Libary/bin/mysql80/bin/mysqlbackup');
}
return $cmd;
})
;
Then, in your local .env
file, add this variable:
USE_MAMP="true"
In environments where this variable is not defined, the command will be passed through, unmodified!