MySQL/Windows/Docker - How to import a dump file to MySQL container
Tags: docker, mysql, windows, powershell
Published: 2023-02-02 19:18:37

**< environment >**
OS : Windows
DB : MySQL
virtualization software ： Docker
shell ： PowerShell / git bash / command prompt

I tried import to MySQL container on Windows using dump file, it was a really tough job.

## How to export dump file using PowerShell command (execute from outside of container)
When you get MySQL dump file from outside of container, the command is like this.

I have a MySQL container named "mysql".
```
docker-compose exec mysql mysqldump --user=root --password=password myapp01 > dump_file_20221119.sql
```

To execute, replace "--user" and "--password" in your environment.
And the scheme is "myapp01".

After executing this command, dump_file_20221119.sql will be created.
There is no problem so far.

## How to import dump file

### PowerShell (execute from outside of container) [failed]
I tried the most basic import command.
```
docker-compose exec mysql mysql --host=localhost --user=root --password=password myapp01 < dump_file_20221119.sql
```

The result is:
```
PS C:\kaki\work> docker-compose exec mysql mysql --host=localhost --user=root --password=password myapp01 < dump_file_20221119.sql
発生場所 行:1 文字:90
+ ... ql --host=localhost --user=root --password=password myapp01 < dump_fi ...
+                                                                 ~
演算子 '<' は、今後の使用のために予約されています。
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported
```

You can't use the character '`<`' in PowerShell.

I looked up how do I make the character escape, but there is no way to do that.


**(Reference)**
[about Special Characters - PowerShell | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.3)

I tried "<" and "<", it didn't work.

### PowerShell (execute from outside of container - other command) [failed]
When you execute import command, you can use '-e' option instead of the character '`<`'.

**(Reference)**
[MySQL :: MySQL 5.6 Reference Manual :: 4.5.4 mysqldump — A Database Backup Program](https://dev.mysql.com/doc/refman/5.6/en/mysqldump.html)

> (example)
> shell> mysql -e "source /path-to-backup/backup-file.sql" db_name

So, I tried :
```
docker-compose exec mysql mysql --user=root --password=password -e "source dump_file_20221119.sql" myapp01
```

The result is:
```
ERROR at line 1: Failed to open file 'dump_file_20221119.sql'
```

I found out that the "-e" option only target files inside the container.

I tried store the dump file in the container and execute the command, it recognized the file.

Although, the error occurred.

```
ERROR: ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled and mysql is run in non-interactive mode. Set --binary-mode to 1 if ASCII '\0' is expected. Query: '��m'.
```

The cause is that PowerShell default character code is CP932(SJIS).

(Note)
The writer is Japanese.


**(Reference)**
[How to export dump file on Windows, import it to Ubuntu](https://qiita.com/hideshis/items/38f48c56a6353cfa8a67)

I thought if "binary-mode" was set "1", it would work.
But, it didn't.


**(Reference)**
[mysql Client Options](https://dev.mysql.com/doc/refman/5.6/en/mysql-command-options.html)


Execute command:
```
docker-compose exec mysql mysql --user=root --password=password -e "source dump_file_20221119.sql" myapp01 --binary-mode
```

The result:
```
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'source dump_file_20221119.sql' at line 1
```

When I google the word "binary-mode" option, I found "--binary-mode=1" and "binary-mode 0".
But, I did't understand quite well. (I tried them, but I got errors)

I'm not sure why there are so many different statement. Anyway, I refer the one from official website.

But, if I solve the problem, I have to set the file in the container. So, it is not an efficient approach.

When I export using MySQL Workbench appended command, I could successfully import without mentioned errors.

The command is:

**export using MySQL Workbench appended command**
```
& "C:\Program Files\MySQL\MySQL Workbench 8.0 CE\mysqldump.exe" --result-file=dump_file_20221119.sql myapp01 --user=root --password=password --host=127.0.0.1 --port=3306 --set-gtid-purged=OFF --skip-lock-tables --skip-add-locks --skip-column-statistics
```

The shell is Powershell.
When you execute Powershell command that contains space, the error occurred.
So, you have to do other approach.

You need to put the dump file in MySQL container.

### Execute PowerShell script [failed]
I thought it would work if I created PowerShell script file and executed.
So, I tried.

I created the file "dump-import.ps1".
The file contents is:

**dump-import.ps1**
```
Write-Host "dump importing..."

# docker-compose exec mysql bash
docker-compose exec mysql mysql --host=localhost --user=root --password=password myapp01 < dump_file_20221119.sql
```

Execute command:
```
PowerShell -ExecutionPolicy RemoteSigned .\dump-import.ps1
```

The result:
```
PS C:\kaki\work\tmp> PowerShell -ExecutionPolicy RemoteSigned .\dump-import.ps1
発生場所 C:\kaki\work\dump-import.ps1:4 文字:90
+ ... ql --host=localhost --user=root --password=password myapp01 < dump_fi ...
+                                                                 ~
演算子 '<' は、今後の使用のために予約されています。
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : RedirectionNotSupported
```

I got exactly same error.

It works the command "docker-compose exec mysql bash" correctly.


### use git bash [failed]
I gave up using PowerShell, so I experimented with git bash.

The result is:
```
$ docker-compose exec mysql mysql --host=localhost --user=root --password=password myapp01 < dump_file_20221119.sql
the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'
```

The error message says use "winpty".

Anyway, add "winpty" on the head of the command.

**(Reference)**
[the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty' · Issue #2888 · vercel/hyper](https://github.com/vercel/hyper/issues/2888)


So, re-execute.
```
$ winpty docker-compose exec mysql mysql --host=localhost --user=root --password=password myapp01 < dump_file_20221119.sql
stdin is not a tty
```

The error message says "stdin is not a tty".

I found the page after researching.

[docker-compose on wsl: stdin is not a tty · Issue #166 · rprichard/winpty](https://github.com/rprichard/winpty/issues/166)

> It's a long standing issue with Docker.
> The way it does terminal detection only works with cmd.exe and powershell.exe at the moment. Any third-party terminal breaks that detection and gives the message.

It seems impossible to add "winpty" on the head of docker command, it is necessary to use directly powershell or cmd. (If you use Windows)

As a side note, you can check out about "winpty" here.

[[Git Bash] winpty コマンドについて調べてみた([Git Bash] About winpty)](https://mseeeen.msen.jp/what-does-winpty-command-do/)

> "winpty" command is interface to interact between Windows console program and UNIX virtual terminal.


### use command prompt [failed]
So, I used cmd.
```
C:\kaki\work>docker-compose exec mysql mysql --host=localhost --user=root --password=password myapp01 < dump_file_20221119.sql
the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'
```

Next, I re-execute the command with "winpty" on the head.
```
C:\kaki\work\ryuki-prd>winpty docker-compose exec mysql mysql --host=localhost --user=root --password=password myapp01 < dump_file_20221119.sql
'winpty' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識されていません。
```

It didn't work.


### execute the command inside container [success(But, inefficient)]
I tried to execute import command after entering the container.

It is necessary to set share directory between host and container.
The example is here :

**docker-compose.yml**
```yml
  mysql:
    image: mysql:5.7

# (omission)

    volumes:
      - db-data:/var/lib/mysql
      - ./shared_db:/shared_db  # set share directory between host and container
```

Store exported dump file in "shared_db" directory, after login mysql container.

And execute import command in "shared_db" directory inside of the container.

Drawback : Need to rebuild container. annoying.

Also, it is not suitable at all to edit docker-compose.yml and rebuild container only to import dump file.


## use MySQL Workbench. [success]
After this long research, I recommend using MySQL Workbench to import dump file to MySQL container on Windows.

There is a annoying error about character encoding when I import the dump file with the command above.
So, it is better to use MySQL Workbench to export.

### MySQL Workbench：export

#### 1. Server -> Data Export
![sqlworkbench01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz4sbnjmqv7p0iyqxwmv.png)

#### 2．Start Export
Select the schema to export. ("myapp01" in the image.)

Select Export Self-Contained File.
(If you don't check the option, the dump files will be created for each tables, which create disaster in your file system.)

Set export file name.

After click "Start Export" button.

![sqlworkbench02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7vi4413ggqh0gsnn518k.png)

#### 3. Data export complete
![sqlworkbench03](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2yn7kdil2pg814sqibp.png)

### MySQL Workbench：import

#### 1．Delete existing schema
delete the schema to import "myapp01".

![sqlworkbench04](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpa7x7c079mks2tanri5.png)

#### 2. Create schema
Recreate deleted schema.(myapp01)

![sqlworkbench05](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzrkap3nej5e40lnfpo7.png)

Set the name, and click "Apply".

![sqlworkbench06](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kswncwsrib9s0gaxv0t.png)

You can leave Algorithm and Lock Type as default.
![sqlworkbench07](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kv4yjif56nk3id8uof64.png)

Scheme will be created.
![sqlworkbench08](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldse0t9ext2pyy7uj4y7.png)

#### 3. Server -> Data Import
![sqlworkbench09](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1m62jjqssr3zfmm100w.png)

#### 4．Start Import
Select "Import from Self-Contained File", and set export file.

Set target scheme.

Then, click "Start Import".

![sqlworkbench10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d0zsqmc8w49glat9fcl.png)

#### 5．Import Completed
![sqlworkbench11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a730z00gp8bufxq2bpb7.png)

## observation
I hope Powershell accepts the character "<".