Mamp Mysqldump



Download

One of the changes in Craft 3 is that, by default, it relies on the mysqldump command to create database backups. That’s the most reliable way to dump a database without hitting PHP resource limits like could happen in Craft 2 with large databases. The problem for MAMP users, however, is that its mysql and mysqldump executables are not available to the PHP process when making requests in the Control Panel via HTTP(S). A few Google searches will surface some hacks that work for some people, and not for others.

Mamp

So I copied the mysqldump command and copied it to the command line and hit enter. Mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect. After this I tried several methods to solve the socket issue after I found the solution from www.razorsql.com.

  1. I am able to connect to the database with the correct credentials and I can run mysqldump from the terminal with the same credentials. Using MAMP Pro and PHP 7.2.
  2. Importing and Exporting Databases in MAMP. 16th January 2018. When using MAMP and the phpMyAdmin interface provided, there may be times when you struggle to import and export large databases, perhaps due to timeout issues.

Craft provides two configuration settings to help with this: backupCommand and restoreCommand. We’ll provide MAMP-compatible commands for you here. We recommend putting them in your .env file so they don’t need to be shared in other environments such as production, staging, or your teammates’ computers.

Edit /.env#

We’ll add two variables: BACKUP_COMMAND and RESTORE_COMMAND. Copy and paste this code block exactly as it is into your .env file, each command on a single line, and double-quotes as they are.

Mamp

If you changed your MAMP preferences for MySQL password, then find and replace -proot with -pyourpassword.

For most database work I use phpMyAdmin. However, on the rare occasion I encounter a strange error — looking at your foreign key constraints — I opt for the Windows command prompt. I do this because a) troubleshooting database issues is far outside of my skillset and b) I need to get stuff done regardless of that. Also, the errors in phpMyAdmin typically don’t exist when performing the same task in the Windows command prompt (for the commands I run).

Dumping and Restoring MySQL databases on the Windows command prompt is a simple process.

Mamp Mysqldump Download

  1. Fire up your Windows command prompt. In Windows 7, click the start orb and type cmd in the Search Programs and Files input and hit enter.

  2. If MySQL has been added to your Windows path variable, continue to step 3. If this is the first time you have run a mysql command in the Windows command prompt, it will return the error captured below.

    Fear not, your computer just needs to know the location of MySQL to run MySQL-related commands. You tell Windows this location by adding the MySQL directory to your path variable. I covered how to add PHP to your Windows path variable using the Windows GUI in a different post. But in the spirit of going commando, let’s add MySQL’s location to the Windows path variable using the command prompt.

    • In your Windows command prompt, execute the command: set path=C:pathtomysqlexecutable, where C:pathtomysqlexecutable corresponds to the location of the mysql.exe file on your machine. I am running MySQL version 5.6.12, which was bundled with WAMP at my time of my download. So, the MySQL executable is located in C:wampbinmysqlmysqlmysql5.6.12bin.

    • After you have added MySQL’s location to your Windows path variable, you should test the mysql command. Below is a screenshot of MySQL being added to the Windows path variable and the mysql command tested on my machine.

      Now that mysql is recognized in the Windows command prompt, you can run the MySQL commands listed below to dump and restore your databases.

  3. To dump/export a MySQL database, execute the following command in the Windows command prompt: mysqldump -u username -p dbname > filename.sql. After entering that command you will be prompted for your password. Once the password is entered your dump file will be available in the root directory for your Windows user – ie: C:Usersusername. An example is shown in the screenshot below.

  4. To restore/import a MySQL database, execute the following command in the Windows command prompt: mysql -u username -p dbname < filename.sql. After entering that command you will be prompted for your password. Once the password is entered your dump file that exists in the root directory for your Windows user will be restored. An example is shown in the screenshot below.

Mamp Mysqldump Vs

That's it! We have successfully dumped and restored a MySQL database using the Windows command prompt.