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

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}}
shell

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!
shell
# 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.
shell

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.sql exports the local database. frbit db push takes the gzipped file of ddev export-db --file=dump.sql.gz too. ddev auth ssh makes the SSH key available in the container, ddev ssh opens a shell in it.

Written by humans and AI in collaboration.

AI use & editorial processEdit on GitHub ↗