How to run 'mysql' file?

docker
mysql
config

#1

In CircleCI 1.0, we used to have this code:

services:
  - mysql

database:
    override:
      - mysql -u ubuntu circle_test < scripts/db/mysql_setup.sql

Now we are migrating to 2.0 and mysql is a docker instead of a service:

version: 2
jobs:
  build:
    docker:
      - image: circleci/openjdk:8-jdk
      - image: redis:3.2.11
      - image: donilan/mysql-utf8mb4

We need to prepare our database, how can I execute this mysql -u ubuntu circle_test < scripts/db/mysql_setup.sql when mysql is dockerized?


#2

The same way. You’ll need to define the host as 127.0.0.1 to use TCP instead of a Unix socket.

You will also need to install the mysql-client package or the command won’t be available.


#3

Rohara, thank you very much for the answer. But I’m not sure I completely understand. What host do you mean? Is it mysql -h localhost -P 3306 --protocol=tcp?

Shall the mysql-client form a part of Dockerfile OR shall it be a selfcontained Docker?

I’m going to try this:

version: 2
jobs:
  build:
    docker:
      ...
      - image: arey/mysql-client

    steps:
      - run: mysql -u ubuntu circle_test < scripts/db/mysql_setup.sql

#4

Sorry for not being more clear.

mysql  -h 127.0.0.1 -u ubuntu circle_test < scripts/db/mysql_setup.sql

Just include this at the top of your job in config.yml

- run: sudo apt install mysql-client

Or if you do want to build an image (it’s never a bad idea in my opinion), here is your Dockerfile:

FROM circleci/openjdk:8-jdk
RUN sudo bash -c "apt-get update && apt-get install -y mysql-client"

You can not access binaries in other containers like in your example. We presently only support running commands in the primary container.

Also, I have heard some reports of that Redis version being a bit sluggish. I have had great success with redis:2.8.19 if you do see any performance issues.


#5

Haha, no problem. I was trying to connect it to dockerized version of mysql-client. I ended up initiating the db from a gradle task. :slight_smile:

Thank you anyways. I will rewrite it.

UPDATE

I’ve tried your solution. But it didn’t work in the end… I could never connect to db. I tried with a docker locally and it was working but not on CircleCi. I tested with 127.0.0.1 and 172.17.0.1: mysql -u root < ...sql.


#6

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.