How can I create multiple MySQL databases in the same Docker image?

I do have an app that has multi-tenant architecture and does create a new database via executing shell command calling exec() in PHP code. The thing is that because of such a call, I can’t use multiple docker images for multiple databases. I need a single docker image with mutliple databases. Is that possible?

version: 2
    - image: circleci/php:7.1-node-browsers
    - image: tkuchiki/delayed-mysql
        MYSQL_DATABASE: circle_test
      - checkout
      - run: sudo apt-get install mysql-client --allow-unauthenticated
      - run: mysql -u root -e "create database production_flow" --protocol=tcp

Do you mean multiple databases running in the one database server instance, or do you want several database servers? Either is possible.

The first - multiple databases in a single server instance

Righto. The secondary image will allow you to pass the database name and credentials to create for the first database. Then, in a run step, run a SQL script against the database using these credentials to run additional CREATE DATABASE commands.

If you get stuck, show us your config.yml here, in a code-formatting block.

1 Like

works like a charm. Added a line:
- run: mysql -h -u root -e "create database newdatabase"
Thanks @halfer

Oh no! So it didn’t work then? :joy: :rofl: :laughing:

1 Like

Actually, it did work when I tried to rebuild with SSH connection and sshed manually inside. When adding a run command it did not work saying connection refused.

I think you explained the problem here - Why can't I connect to MySQL server?, though I did not quite understand how that works :confused:

1 Like

I actually don’t understand why does it work, when I ssh to the server? I added my config.yml to the first post

In your build, you are not waiting for MySQL to accept connections, and I suspect that although the container is started, the database server itself needs more time. However, in SSH, MySQL always has time to get ready. To fix this, add a new run step to wait for the database to spin up - checking the TCP socket is usually enough.

See here:

1 Like

Now I have started setting up the tests for the second project. I did similar yml but swapping projects and added my key to “Checkout keys” and started getting problems that I cant even checkout the project :confused:

Start a new thread about that please, including all the config and details that are needed for someone to help you.

For reference, this question was cross-posted to Stack Overflow.

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