Indonesia Website Awards
Indonesia Website Awards

Hello, how to import database in MySQL Docker ?. Yes, that’s the question I encountered some time ago when I wanted to migrate a database from a MySQL Server (non-docker) that I have to a MySQL Server that I installed in Docker. Since I’m new to Docker, I was confused about the fastest way to migrate. Then after I found out and I tried, there are 3 ways to import databases to MySQL in docker. Immediately, here are the ways:

#First method

This first method is directly using the docker command, namely:

docker exec -ti containerName mysql -u root -p < database.sql

This method is the most common way to run query queries that are small in number or small in size.

 

Also read : How to Fix “Warning: Remote Host Identification Has Changed”

 

#Second method

This second way, by using mysql-client (if installed) on your server:

mysql -u root -p -h IP_docker -P port_docker < database.sql 

You must include your docker ip and docker port, if I’m not mistaken by default the docker ip is 172.17.0.1 and for the MySQL port 3306. so more or less in I like this:

mysql -u root -p -h 172.17.0.1 -P 3306 < database.sql 

 

#Third method

The third and last way is by entering into the docker container for MySQL then calling the * .sql file that you want to import on the condition that you have a Shared Folder with docker.

# Enter the MySQL container
docker exec -ti containerName /bin/bash

# After that cd to the shared directory, here I put it at home
cd /home

# login to MySQL
mysql -u user -ppassword

# select database
use mydatabase;

# run query / import with the command source
source myquery.sql;

for this 3rd way, between the docker container and our local directory, must have a shared folder, for example for my docker mysql container, I add --volume ~/docker-data/mysql/data:/home . So when I want to use the 3rd method, I will put the * .sql file in ~ / docker-data / mysql / data. This method is what I use when migrating a database from a (non-docker) database server to a docker database server.

Yep, that’s 3 method I can share about How to import databases in MySQL Docker ?. Approximately I apologize, I hope this article can be useful, Thank you ^^,

 

REFRENCES :

How to import database in MySQL in Docker?

 


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *