Import Data to MySQL on Docker
Sungwa Yu
  1. Pull and start a MySQL install
1
2
3
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
# Example:
# docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=000000 -d -p 3306:3306 mysql:5.7
  • some-mysql is the name you want to assign to your container.
  • my-secret-pw is the password to be set for the MySQL root user.
  • tag is the tag specifying the MySQL version you want.
  • -p host_port:container_port.

Now can also use client to connect the data base via port 3306

  1. Import local (host) .sql file to the container
1
docker cp /opt/a.sql my-mysql:/tmp/
  1. Log in to Docker MySQL
1
docker exec -it my-mysql mysql -uroot -p000000

Should see mysql>

  1. Create database
1
CREATE DATABASE my_database;
  1. Select database
1
USE my_database;
  1. Execute sql file
1
source /tmp/a.sql