简体   繁体   中英

How to restore Data from a MySQL dump file, from host to Docker container via. Windows

I've read through a lot of alternative solutions but I haven't found anything specific to my problem. I'm using Windows PowerShell, and I need to restore/populate my DB Docker container that I created using the following code below.

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -e MYSQL_DATABASE=DB -e MYSQL_USER=PASSWORD -e MYSQL_PASSWORD=PASSWORD -d mysql:8

Then to populate my DB (Restoring data from an sql dump file) I enter in the following code below to point my.sql file to the DB container I created.

ps I have to place in dbl quotes due to windows syntax. I also can't place in the following character '<' before entering in my path because it's improper syntax for Docker on Windows)

docker exec -i some-mysql sh -c "exec mysql -uroot -p "$root"" "C:/filePathHere/all-databases.sql"

After I hit enter I receive the fallowing error below

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Even after creating a new DB connection and adding in a new user/password, which I use root for both User/Pwd. I still get denied access.

Anyone know what I may be doing wrong? It's important I populate my DB container this way and avoid manually restoring the dump file.

I've found the solution to my question, and first the '<' operator is not supported in PowerShell v1 and as of v5, it's still not... You can either use regular cmmd line or go to the provided link for the work around with Windows Powershell The '<' operator is reserved for future use

And so thanks to this blog I found online, I've found the syntax file path solution to my problem (I provided the code from the link below) . Windows: Back up & restore MySQL data from Docker container

And if anyone is getting a similar error

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

A solution that worked for me was to add the fallowing cmmd mysql -u root to your Restore cmmd. Found this solution via this link here Access denied for user 'root'@'localhost'

BACK-UP

C:\docker\directory> docker ps

C:\docker\directory> docker exec container-id /usr/bin/mysqldump -u username database-name > dump.sql

RESTORE

C:\docker\directory> docker ps

C:\docker\directory> docker exec -i container-id /usr/bin/mysql -u username database-name < dump.sql

If you want to use docker-compose command

    # To BACK BACKUP
C:\docker\directory> docker-compose exec -T container-name /usr/bin/mysqldump -u username database-name > dump.sql

    # To ROLLBACK 
C:\docker\directory> docker-compose exec -T container-name /usr/bin/mysql -u username database-name < dump.sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM