Database up via terminal
Published
Updated
Export the local MySQL database with mysqldump, open an SSH tunnel to the fortrabbit environment, and import the dump with the mysql client. The import replaces tables of the same name.
Get ready
- Database access intro
- Database export/import intro
- How to connect to the remote database via terminal
- Optional: the frbit CLI, installed and signed in.
With the frbit CLI
# Export the local database
$ mysqldump --set-gtid-purged=OFF -u{{local-db-user}} -p {{local-db-name}} > dump.sql
# Import it on the environment
$ frbit db push dump.sql {{app-env-id}}
frbit db push runs the import on the environment over SSH, with no tunnel and no MySQL password. It asks to type the environment ID first, then dumps the current database to /data/home/ and prints the command to restore it when the import fails. Every table in the dump is dropped and recreated, tables that exist only on the environment stay.
Without the frbit CLI
The following commands are run in your local development environment. mysql and mysqldump are required.
# Terminal 1
# Export database from your local machine
$ mysqldump --set-gtid-purged=OFF -u{{local-db-user}} -p {{local-db-name}} > dump.sql
# You'll be prompted for your local database password
# Open the SSH tunnel
ssh -N -L 13306:mysql:3306 {{app-env-id}}@ssh.{{region}}.frbit.app
# No output. Open a new terminal window!
# Terminal 2 (new window)
# Import Database
mysql -h127.0.0.1 -P13306 -u{{app-env-id}} -p {{app-env-id}} < dump.sql
# You'll be prompted for your MySQL password (found in the dashboard).
# The `dump.sql` file is from the first step.
# Can take a while, depends on size.
Development environments
Container-based setups need extra steps:
- Docker: run the commands in the database container with
docker exec -i {container} …. - DDEV:
ddev export-db --gzip=false > dump.sqlexports the local database.frbit db pushtakes the gzipped file ofddev export-db --file=dump.sql.gztoo.ddev auth sshmakes the SSH key available in the container,ddev sshopens a shell in it.