Skip to main content

Import MySQL

Overview

This guide explains how to export a MySQL database from your current setup and import it into an app.

Export

Create a dump of your existing MySQL database:

mysqldump -hmyservername -umyusername -pmypassword --single-transaction --routines --triggers databasename > mysqldump.sql

If the MySQL server is in an app, you can export it using this command line in the Web Terminal:

mysqldump -h${CLOUDRON_MYSQL_HOST} -u${CLOUDRON_MYSQL_USERNAME} -p${CLOUDRON_MYSQL_PASSWORD} --single-transaction --routines --triggers --no-tablespaces ${CLOUDRON_MYSQL_DATABASE} > /tmp/mysqldump.sql

Import

  1. Enable Recovery Mode in the Repair section. This pauses the app while you import data.

App Enable Recovery Mode

  1. Open a Web Terminal using the Terminal button in the Console section.

App Terminal

  1. Upload the dump file using the Upload to /tmp button.

App Upload

  1. Clear the existing database:
mysql --user=${CLOUDRON_MYSQL_USERNAME} --password=${CLOUDRON_MYSQL_PASSWORD} --host=${CLOUDRON_MYSQL_HOST} ${CLOUDRON_MYSQL_DATABASE} -Nse 'show tables' | while read table; do mysql --user=${CLOUDRON_MYSQL_USERNAME} --password=${CLOUDRON_MYSQL_PASSWORD} --host=${CLOUDRON_MYSQL_HOST} ${CLOUDRON_MYSQL_DATABASE} -e "SET FOREIGN_KEY_CHECKS = 0; drop table \`$table\`"; done
  1. Import the dump using the mysql command:
mysql --user=${CLOUDRON_MYSQL_USERNAME} --password=${CLOUDRON_MYSQL_PASSWORD} --host=${CLOUDRON_MYSQL_HOST} ${CLOUDRON_MYSQL_DATABASE} < /tmp/mysqldump.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
root@838249e2-d2ae-4a40-80bf-4f1632e0d376:/app/code#

An empty output indicates success.

  1. Click Disable Recovery Mode in the Repair section to restart the app:

App Disable Recovery Mode

Verify

Verify the import:

  1. Click the MySQL button at the top of the terminal to paste the connection command.
  2. Press Enter to access the MySQL shell.

Mysql Shell