mysqldump in PowerShell corrupts non-Latin characters when exporting database (SOLVED)
October 23, 2022
mysqldump is a MySQL utility for creating database and table backups. Unlike phpMyAdmin, which, although it offers a web interface, is a slower tool due to the limitations of intermediates such as PHP and Apache, mysqldump is a much more efficient tool without limitations for backing up very large data.
But on Windows, mysqldump has some nuances. Due to the peculiarities of PowerShell for working with encodings, all non-Latin characters can be corrupted in exported databases. This issue is not seen in CMD, but recent versions of Windows use PowerShell by default, so the issue in question affects all users who run mysqldump to back up databases in Windows.
The following command, executed in PowerShell 7:
.\mysqldump.exe -u root --all-databases > all-databases_ps7.sql
Creates a UTF-8 encoded all-databases_ps7.sql file into which all MySQL databases will be exported using mysqldump (for backup purposes). But in these databases all non-Latin characters will be irretrievably corrupted!
That is, instead of Cyrillic, it will be something like this:
'╨Р╤А╨▒╨╕╤В╤А╨░╨╢╨╜╤Л╨╣ ╨┐╤А╨╛╤Ж╨╡╤Б╤Б: ╤Г╤З╨╡╨▒╨╜╨╕╨║ / ╨Ъ.╨Ь. ╨Р╤А╤Б╨╗╨░╨╜╨╛╨▓, ╨Ф.╨е. ╨Т╨░╨╗╨╡╨╡╨▓, ╨а.╨Э. ╨У╨╕╨╝╨░╨╖╨
To avoid this problem, use mysqldump with the --result-file option. The following command will save the database in the correct encoding:
.\mysqldump.exe -u root --all-databases --result-file=all-databases.sql
You can also use the following two-command construct to fix the encoding problem:
[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("UTF-8") .\mysqldump.exe -u root --all-databases > all-databases.sql
Related articles:
- Output encoding issues in PowerShell and third-party utilities running in PowerShell (SOLVED) (100%)
- Error “The '<' operator is reserved for future use.” (SOLVED) (71%)
- What is the difference between utf8_general_ci, utf8_unicode_ci, utf8mb4_general_ci, utf8mb4_unicode_ci collations. Which collation, character set and encoding to choose for MySQL database (59.7%)
- How to remove the header from the output table in PowerShell and leave only the data (56.7%)
- How to clear the terminal window in PowerShell. Analog of clear (56.7%)
- How to connect your phone to the Internet using another phone's Bluetooth (RANDOM - 50%)