Populate database from .sql
file dump on local MySQL instance
.sql
file hasCREATE DATABASE
andUSE
statements with desired db name, eg
CREATE DATABASE IF NOT EXISTS `2024_07_03_alpaca_db`;
USE `2024_07_03_alpaca_db`;
-
Docker is installed and running, eg Docker desktop for Mac
-
Create local folder to persist data named
mysql_data
-
Create local docker network
docker network create dev-network
- Install and run container
mysql_alapcas
. Download image if not exists.
docker run --name mysql_alapcas --net dev-network -v /Users/anita/Documents/dev/mysql_data:/var/lib/mysql -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=123 mysql:8.2.0
- Start another container
mysql
to execute SQL commands to original container
docker run -it --network dev-network -e LANG=C.UTF-8 --rm mysql:8.2.0 mysql -hmysql_alapcas -uroot -p123
- Check database that executes SQL commands has correct charset for Norwegian chars as this overrides all other language settings
mysql> show variables like 'char%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.2/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)
mysql> exit;
- Populate database from
.sql
file dump
docker exec -i mysql_alapcas sh -c 'exec mysql -p123' < /Users/anita/Documents/dev/elastic-data-tool/data/2024_07_01_alpacas.sql
- To query database re-run command to start container
mysql
that executes SQL commands to original container
Helpful docker commands
- Show networks
docker network ls
- Remove non active networks
docker network prune
Ref: https://hub.docker.com/_/mysql
- MySQL is installed locally, eg using homebrew for Mac
- Start MySQL server
mysql.server start
- Connect to MySQL
mysql -u root -p
and enter password. MySQL prompt should now show. - At MySQL prompt create empty database eg
CREATE DATABASE alpaca_database;
and verify createdSHOW DATABASES;
- Exit MySQL
exit
- Create/update .env file to contain
MYSQL_DATABASE="YOUR DATABASE NAME GOES HERE"
- Create folder at root level named ./data
- Add to
.gitignore
- For .sql, JSON files that will NOT be committed to the repo - Store MySQL dump file there, eg
alpaca.sql
- Change directory to folder
cd data
- Populate database from MySQL dump
mysql -u root -p alpaca_database < alpaca.sql
- Connect to MySQL
mysql -u root -p
and verify createdUSE alpaca_database
andSHOW TABLES
- Create database in Azure account named eg
alpaca_database
- Go to the directory containing the .sql database file to import
- Run
mysql -h {my azure db server name}.mysql.database.azure.com -u {my azure server admin login name} -p alpaca_database < {my_database_file.sql}
- comment that out, or
- update the database name in the .sql file, then in the SQL -> JSON step remove the database name, eg
mysql -u root -p < alpaca.sql
show databases;
use <database name>;
show tables;
SHOW COLUMNS FROM <table name>;