MySQL Database Files Location on macOS: Migration Guide for Developers


2 views



When running MySQL on macOS (OS X), the default data directory location varies depending on the installation method:

  • Homebrew installation: /usr/local/var/mysql
  • DMG package installation: /usr/local/mysql/data
  • MAMP installation: /Applications/MAMP/db/mysql

To verify your exact data directory location, connect to MySQL and run:

SHOW VARIABLES LIKE 'datadir';

Each database is stored as a separate directory containing these key files:

db_name/
    db_name.frm (table structure)
    db_name.ibd (InnoDB data for MySQL 5.6+)
    db_name.MYD (MyISAM data)
    db_name.MYI (MyISAM index)

Follow these steps to move MySQL databases between macOS servers:

# 1. Stop MySQL service
sudo mysql.server stop

# 2. Archive the database directory
tar -czvf mysql_data_backup.tar.gz /path/to/mysql/data/directory

# 3. Transfer the archive to new server
scp mysql_data_backup.tar.gz user@newserver:/tmp/

# 4. On new server, stop MySQL and backup existing data
sudo mv /path/to/mysql/data /path/to/mysql/data_backup

# 5. Extract and set permissions
sudo tar -xzvf /tmp/mysql_data_backup.tar.gz -C /path/to/mysql/
sudo chown -R _mysql:_mysql /path/to/mysql/data
sudo chmod -R 750 /path/to/mysql/data

# 6. Start MySQL service
sudo mysql.server start

For InnoDB tables using the system tablespace (ibdata1), you must:

  1. Migrate the entire data directory
  2. Ensure identical innodb_file_per_table settings
  3. Maintain the same MySQL major version

After migration, connect to MySQL and verify:

SHOW DATABASES;
USE migrated_db;
SHOW TABLES;
SELECT COUNT(*) FROM important_table;

Check the error log for any issues:

tail -f /usr/local/var/mysql/hostname.err

For smaller databases or cross-version migrations, consider using mysqldump:

# Export
mysqldump -u root -p --databases db1 db2 > backup.sql

# Import
mysql -u root -p < backup.sql

This method avoids file permission issues and is more version-flexible.


On macOS systems with default MySQL installations (including Homebrew installations), the database files are typically stored in:

/usr/local/var/mysql/

You can confirm the exact data directory location by checking the MySQL configuration or running this command:

mysql -uroot -p -e "SHOW VARIABLES LIKE 'datadir';"

Sample output:

+---------------+------------------------+
| Variable_name | Value                  |
+---------------+------------------------+
| datadir       | /usr/local/var/mysql/  |
+---------------+------------------------+

The data directory contains several important components:

  • Database directories (one per database, named after the database)
  • System tables (mysql, performance_schema, sys)
  • Log files (error log, binary logs)
  • Configuration files (my.cnf)

Follow these steps to properly migrate MySQL data between macOS servers:

# 1. Stop MySQL on both servers
sudo /usr/local/bin/mysql.server stop

# 2. Archive the data directory on source server
tar -czvf mysql_data_backup.tar.gz /usr/local/var/mysql/

# 3. Transfer the archive to the new server
scp mysql_data_backup.tar.gz user@newserver:/tmp/

# 4. On destination server, remove existing data (if any)
sudo rm -rf /usr/local/var/mysql/*

# 5. Extract the archive
sudo tar -xzvf /tmp/mysql_data_backup.tar.gz -C /usr/local/var/mysql/

# 6. Set proper permissions
sudo chown -R _mysql:_mysql /usr/local/var/mysql/
sudo chmod -R 755 /usr/local/var/mysql/

# 7. Start MySQL service
sudo /usr/local/bin/mysql.server start

For Homebrew installations, you might need to adjust the data directory path in the MySQL plist file:

# For Homebrew services
brew services stop mysql
# Edit the plist file if needed
nano /usr/local/opt/mysql/homebrew.mxcl.mysql.plist

After migration, verify data integrity by checking:

mysqlcheck -uroot -p --all-databases --check-upgrade

And confirm all databases are accessible:

mysql -uroot -p -e "SHOW DATABASES;"