Skip to content

Commit

Permalink
add import/export docs for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolasburk committed Nov 12, 2019
1 parent d70d8ff commit 0f2491a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
77 changes: 44 additions & 33 deletions docs/import-and-export-data/mysql.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
# Importing and exporting data with MySQL

This document describes how you can export data from and import data into a MySQL database. You can learn more about this topic in the official [PostgreSQL docs](https://www.postgresql.org/docs/9.1/backup-dump.html).
This document describes how you can export data from and import data into a MySQL database. You can learn more about this topic in the official [MySQL docs](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html).

## Data export with `mysqldump`

[mysqldump](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html) is a native MySQL utility you can use to export data from your MySQL database. To see all the options for this command, run `pg_dump --help`.
[`mysqldump`](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html) is a native MySQL command line utility you can use to export data from your MySQL database. To see all the options for this command, run `mysqldump --help`.

From the PostgreSQL docs:
Note that your [MySQL installation](https://dev.mysql.com/doc/refman/8.0/en/installing.html) comes with `mysqldump` by default, typically contained in `/usr/local/mysql/bin` on Mac OS. This means you can either invoke the command by pointing to that directory `/usr/local/mysql/bin/mysqldump` or [adding it to your `PATH`](https://stackoverflow.com/questions/30990488/how-do-i-install-command-line-mysql-client-on-mac#answer-35338119) so that you can run `mysqldump` without specifying the directory.

> The idea behind this dump method is to generate a text file with SQL commands that, when fed back to the server, will recreate the database in the same state as it was at the time of the dump. PostgreSQL provides the utility program `pg_dump` for this purpose.
> `pg_dump` is a regular PostgreSQL client application (albeit a particularly clever one). This means that you can perform this backup procedure from any remote host that has access to the database. But remember that pg_dump does not operate with special permissions. In particular, it must have read access to all tables that you want to back up, so in practice you almost always have to run it as a database superuser.
From the MySQL docs:

> The `mysqldump` client utility performs logical backups, producing a set of SQL statements that can be executed to reproduce the original database object definitions and table data. It dumps one or more MySQL databases for backup or transfer to another SQL server. The `mysqldump` command can also generate output in CSV, other delimited text, or XML format.
The command looks like this:

```psql
pg_dump DB_NAME > OUTPUT_FILE
mysqldump DB_NAME > OUTPUT_FILE
```

You need to replace the `DB_NAME` and `OUTPUT_FILE` placeholders with the respective values for:

- your **database name**
- the name of the desired **output file** (should end on `.sql`)

For example, to export data from a local PostgreSQL server from a database called `mydb` into a file called `mydb.sql`, you can use the following command:
For example, to export data from a local MySQL server from a database called `mydb` into a file called `mydb.sql`, you can use the following command:

```
pg_dump mydb > mydb.sql
mysqldump mydb > mydb.sql
```

If your database schema uses [Object Idenfitifier Types](https://www.postgresql.org/docs/8.1/datatype-oid.html) (OIDs), you'll need to run `pg_dump` with the `--oids` (short: `-o`) option: `pg_dump mydb --oids > mydb.sql`.

#### Providing database credentials

You can add the following arguments to specify the location of your PostgreSQL database server:
You can add the following arguments to specify the location of your MySQL database server:

| Argument | Default | Env var | Description |
| Argument | Default | Description |
| --- | --- | --- | --- |
| `--host` (short: `-h`) | `localhost` | `PGHOST` | The address of the server's host machine |
| `--port` (short: `-p`) | - | `PGPORT` | The port of the server's host machine where the PostgreSQL server is listening |
To authenticate against the PostgreSQL database server, you can use the following argument:
| `--host` (short: `-h`) | `localhost` | The address of the server's host machine |
| `--port` (short: `-p`) | - | The port of the server's host machine where the MySQL server is listening |

To authenticate against the MySQL database server, you can use the following argument:

| Argument | Default | Env var | Description |
| Argument | Default | Description |
| --- | --- | --- | --- |
| `--username` (short: `-U`) | _your current operating system user name_ | `PGUSER` | The name of the database user. |
| `--user` (short: `-u`) | - | The name of the database user. |
| `--password` (short: `-p`) | - | Trigger password prompt. |

For example, if you want to export data from a PostgerSQL database that has the following [connection string](../core/connectors/postgresql):
For example, if you want to export data from a PostgerSQL database that has the following [connection string](../core/connectors/mysql):

```
postgresql://opnmyfngbknppm:XXX@ec2-46-137-91-216.eu-west-1.compute.amazonaws.com:5432/d50rgmkqi2ipus
mysql://opnmyfngbknppm:XXX@ec2-46-137-91-216.eu-west-1.compute.amazonaws.com:5432/d50rgmkqi2ipus
```

You can use the following `pg_dump` command:
You can use the following `mysqldump` command:

```
pg_dump --host ec2-46-137-91-216.eu-west-1.compute.amazonaws.com --port 5432 --user opnmyfngbknppm d50rgmkqi2ipus > heroku_backup.sql
mysqldump --host ec2-46-137-91-216.eu-west-1.compute.amazonaws.com --port --user opnmyfngbknppm --password d50rgmkqi2ipus > backup.sql
```

Note that **this command will trigger a prompt where you need to specify the password** for the provided user.
Expand All @@ -64,38 +65,48 @@ There might be cases where you don't want to dump the _entire_ database, for exa

- dump only the actual data but exclude the [DDL](https://www.postgresql.org/docs/8.4/ddl.html) (i.e. the SQL statements that define your database schema like `CREATE TABLE`,...)
- dump only the DDL but exclude the actual data
- exclude a specific PostgreSQL schema
- exclude large files
- exclude specic tables

Here's an overview of a few command line options you can use in these scenarios:

| Argument | Default | Description |
| --- | --- | --- |
| `--data-only` (short: `-a`) | `false` | Exclude any [DDL](https://www.postgresql.org/docs/8.4/ddl.html) statements and export only data. |
| `--schema-only` (short: `-s`) | `false` | Exclude data and export only [DDL](https://www.postgresql.org/docs/8.4/ddl.html) statements. |
| `--blobs` (short: `-b`) | `true` unless either `-schema`, `--table`, or `--schema-only` options are specified | Include binary large objects. |
| `--no-blobs` (short: `-B`) | `false` | Exclude binary large objects. |
| `--table` (short: `-t`) | _includes all tables by default_ | Explicitly specify the names of the tables to be dumped. |
| `--exclude-table` (short: `-T`) | - | Exclude specific tables from the dump. |
| `--no-create-db` (short: `-n`) | `false` | Exclude any [DDL](https://www.postgresql.org/docs/8.4/ddl.html) statements and export only data. |
| `--no-data` (short: `-d`) | `false` | Exclude data and export only [DDL](https://www.postgresql.org/docs/8.4/ddl.html) statements. |
| `--tables`| _includes all tables by default_ | Explicitly specify the names of the tables to be dumped. |
| `--ignore-table` | - | Exclude specific tables from the dump. |

## Importing data from SQL files

After having used SQL Dump to export your PostgreSQL database as a SQL file, you can restore the state of the database by feeding the SQL file into [`psql`](https://www.postgresql.org/docs/9.3/app-psql.html):
After having used `mysqldump` to export your MySQL database as a SQL file, you can restore the state of the database by feeding the SQL file into [`mysql`](https://dev.mysql.com/doc/refman/8.0/en/mysql.html):

```
psql DB_NAME < INPUT_FILE
mysql DB_NAME INPUT_FILE
```

Note that your [MySQL installation](https://dev.mysql.com/doc/refman/8.0/en/installing.html) comes with `mysql` by default, typically contained in `/usr/local/mysql/bin` on Mac OS. This means you can either invoke the command by pointing to that directory `/usr/local/mysql/bin/mysmysqlqldump` or [adding it to your `PATH`](https://stackoverflow.com/questions/30990488/how-do-i-install-command-line-mysql-client-on-mac#answer-35338119) so that you can run `mysql` without specifying the directory.

You need to replace the `DB_NAME` and `INPUT_FILE` placeholders with the respective values for:

- your **database name** (a database with hat name must be created beforehand!)
- the name of the target **input file** (likely ends on `.sql`)

To create the database `DB_NAME` beforehand, you can use the [`template0`](https://www.postgresql.org/docs/9.5/manage-ag-templatedbs.html) (which creates a plain user database that doesn't contain any site-local additions):
For example:

```
mysql mydb < mydb.sql
```

To authenticate, you can use the `--user` and `--password` options discussed above:

```
mysql --user root --password mydb < mydb.sql
```

To create a database beforehand, you can use the following SQL statement:

```sql
CREATE DATABASE dbname TEMPLATE template0;
CREATE DATABASE mydb;
```


Expand Down
8 changes: 7 additions & 1 deletion docs/import-and-export-data/postresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ postgresql://opnmyfngbknppm:XXX@ec2-46-137-91-216.eu-west-1.compute.amazonaws.co
You can use the following `pg_dump` command:

```
pg_dump --host ec2-46-137-91-216.eu-west-1.compute.amazonaws.com --port 5432 --user opnmyfngbknppm d50rgmkqi2ipus > heroku_backup.sql
pg_dump --host ec2-46-137-91-216.eu-west-1.compute.amazonaws.com --port 5432 --user opnmyfngbknppm d50rgmkqi2ipus > backup.sql
```

Note that **this command will trigger a prompt where you need to specify the password** for the provided user.
Expand Down Expand Up @@ -92,6 +92,12 @@ You need to replace the `DB_NAME` and `INPUT_FILE` placeholders with the respect
- your **database name** (a database with hat name must be created beforehand!)
- the name of the target **input file** (likely ends on `.sql`)

For example:

```
psql mydb < mydb.sql
```

To create the database `DB_NAME` beforehand, you can use the [`template0`](https://www.postgresql.org/docs/9.5/manage-ag-templatedbs.html) (which creates a plain user database that doesn't contain any site-local additions):

```sql
Expand Down

0 comments on commit 0f2491a

Please sign in to comment.