0

I'm currently trying to get some dummydata for my project. So I setup a docker file and I am trying to load in some data from a csv file. I'm not getting any error's just an empty database.

Docker:

version: "3.3"

services:
  maria-db:
    image: 'mariadb:latest'
    ports:
      - '3307:3306'
    environment:
      MYSQL_DATABASE: 'researchprojectdb'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'user'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'admin'
    volumes:
      - ./scripts/researchprojectdb.sql:/docker-entrypoint-initdb.d/1.sql
      - ./data:/var/lib/mysql-files

SQL Script:

CREATE TABLE ROLES (
    role_id int PRIMARY KEY,
    role varchar(20)
);

CREATE TABLE USERS (
    user_id int PRIMARY KEY,
    firstname varchar(83),
    lastname varchar(83),
    email varchar(83),
    password varchar(83)
    role_id int,
    foreign key (role_id) references roles(role_id)
);

INSERT INTO ROLES VALUES(1, 'student');
INSERT INTO USERS VALUES(1,'Jan', 'Met de pet', 'jan@depet.com', 'depet', 1);
-- LOAD DATA LOCAL INFILE '/var/lib/mysql-files/rolesdata.csv' into TABLE ROLES FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 0 ROWS;
-- LOAD DATA LOCAL INFILE '/var/lib/mysql-files/usersdata.csv' into TABLE USERS FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 0 ROWS;

and then I use 2 .csv files with the following data:

Roles:

1   student
2   administrator
3   company

Userdata

1   Jan heas    jan@metdepet.be depet   1
Jens Panis
  • 99
  • 8
  • 1
    What have you tried to debug the problem? – Nico Haase Mar 19 '21 at 11:24
  • Does this answer your question? [How to execute docker-entrypoint-initdb.d/init.sql files AFTER database is created?](https://stackoverflow.com/questions/64139963/how-to-execute-docker-entrypoint-initdb-d-init-sql-files-after-database-is-creat) – Nico Haase Mar 19 '21 at 11:24
  • @NicoHaase In other words, what you want is impossible. You need to either create the database and table1 in init.sql and tell Django not to attempt creating them if they already exists or somehow add your INSERT.. to the list of migrations to be run. I don't really understand what that means or how to solve that? – Jens Panis Mar 19 '21 at 11:26
  • 1
    https://stackoverflow.com/questions/38504257/mysql-scripts-in-docker-entrypoint-initdb-are-not-executed/52715521 is the better duplicate: all stuff in `docker-entrypoint-initdb.d` is run **once** on first start or when you manually remove the data directory – Nico Haase Mar 19 '21 at 11:32
  • Are he `LOAD DATA LOCAL INFILE` lines commented out on accident in the example script? If that's your actual script and you expect them to be executed, you better uncomment the lines. – markusjm Mar 19 '21 at 12:54

0 Answers0