Fixing MySQL Gone Away Errors in MAMP 4

If you are a regular MAMP user then you are probably familiar with coming across issues that crop up with MySQL, particularly the classic #2006 - MySQL server has gone away that appears even when MAMP suggests the MySQL service is still running. Fixing MySQL gone away errors in MAMP 4 can be achieved by following a few simple steps.

Firstly, it is worth checking out your MAMP MySQL configuration settings. This can be done by running the following command.

$ /Applications/MAMP/Library/bin/mysqld --verbose --help

You should see a complete list of the configuration, including the MySQL variables. As the output suggests near the bottom, you can also only view the variables if you wish by running the below command.

$ /Applications/MAMP/Library/bin/mysqladmin variables

Usually with MySQL gone away errors, MySQL requires an increase in the value of the max_allowed_packet variable.

To do this, you can edit the my.cnf MySQL configuration file. MAMP by default may not come with this file, however you can create one and add in your changes. As seen using the Applications/MAMP/Library/bin/mysqld --verbose --help command, the my.cnf file can reside in the following locations:

  • /etc/my.cnf
  • /etc/mysql/my.cnf
  • /Applications/MAMP/conf/my.cnf
  • ~/.my.cnf

MySQL will read these files in the above order, so ~/.my.cnf would take priority.

Create the file and add in your variables.

max_allowed_packet=128M

You can also set this variable on your MySQL server as it’s running. This can be done by logging into MySQL (assuming your username and password are both the default value, root.

$ /Applications/MAMP/Library/bin/mysql -uroot -p

Then when logged in, you can set the max_allowed_packet value by running the below statement.

set global max_allowed_packet=134217728;

Whatever method you choose, ensure your restart MySQL afterwards and you shouldn’t need to worry about any more MySQL gone away errors.

There may be other variables whose values you wish to increase, such as max_connections and max_user_connections. Amend what you need to ensure that any MySQL related issues are kept to a minimum.