# Connecting to MySQL on localhost/127.0.0.1

**URL:** <https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857>\
**Category:** Databases\
**Created:** [March 18, 2018, 9:04pm UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857 "2018-03-18T21:04:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arlogb](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@arlogb](https://discuss.circleci.com/u/arlogb)\
**Post date:** [March 18, 2018, 9:04pm UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857/1 "2018-03-18T21:04:06Z")

</div>

Hi there,

It seems that whatever I do, I can’t get either Python’s mysqlalchemy or mysql client to connect to a server.

I’m trying to use either a base Debian image or the mysql:5.7 image and I’m met with this error:

```
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2003, "Can't connect to MySQL server on '127.0.0.1' (111)") (Background on this error at: http://sqlalche.me/e/e3q8)

/root/.local/share/virtualenvs/api-2w_u5qGM/lib/python3.6/site-packages/MySQLdb/connections.py:204: OperationalError

```

I’ve also tried a custom Debian image with mysql installed.

I’d love any help with this as I’ve been trying for a few hours now and just can’t get it to work.

```
version: 2
jobs:
  build:
    docker:
      - image: circleci:python
        environment:
          - MYSQL_PASSWORD='test_pw'
          - MYSQL_DATABASE='test_db'
          - MYSQL_ROOT_HOST=%
          - DB_HOST=127.0.0.1
          - DB_PORT=3306
      - image: mysql:5.7
        environment:
          - MYSQL_PASSWORD=test_pw
          - MYSQL_DATABASE=loans
          - MYSQL_ROOT_HOST=%
          - DB_HOST=127.0.0.1
          - DB_PORT=3306
    steps:
      - checkout
      - run:
          name: Update pipenv
          command: |
          sudo pip3 install --upgrade pipenv
      - run:
          name: Install dependencies and run tests
          command: |
            mysql -u root -h 127.0.0.1 -ptest_pw
            cd api && pipenv --three && pipenv install
            cp config.template.yml test_config.yml
            pipenv run pytest
      - run:
          name: Setup front end
          command: |
            cd frontend
            yarn install
            yarn build
      - store_artifacts:
          path: test-reports
          destination: test-reports
```

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [March 18, 2018, 10:40pm UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857/2 "2018-03-18T22:40:38Z")

</div>

The first thing I would do is to get the Docker logs for the MySQL image to see if it started up (or the MySQL logs themselves). Also, see if waiting before the tests helps - I would guess the Python and MySQL containers are instantiated at the same time, and so a race condition can occur.

---

<div class="post-metadata">

**Author:** ![arlogb](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@arlogb](https://discuss.circleci.com/u/arlogb)\
**Post date:** [March 19, 2018, 9:40am UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857/3 "2018-03-19T09:40:27Z")

</div>

Hi @halfer,

What’s the best way of getting the logs? Currently the only way I can seem to do this is have the build run on circleCI web interface (as opposed to the local CLI). This is irritating as I have to keep pushing broken builds.

For what it’s worth, I’m having no problem with the same docker containers locally (but outside circleCI).

Thanks.

---

<div class="post-metadata">

**Author:** ![arlogb](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@arlogb](https://discuss.circleci.com/u/arlogb)\
**Post date:** [March 19, 2018, 3:36pm UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857/4 "2018-03-19T15:36:26Z")

</div>

Hi again,

I’ve solved this by moving to postgres. Never figured out what was wrong but making this move just worked ‘out of the box’.

Thanks.

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [March 19, 2018, 6:51pm UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857/5 "2018-03-19T18:51:20Z")

</div>

> [@arlogb](#):
>
> What’s the best way of getting the logs?

I don’t exactly know, it’d need some research. The output in the Circle web app might be Docker log output (and is likely to be valuable) but other Docker logs may be possible. In any case, I’d perhaps start off with MySQL logs to see if the listener is starting up, and what in-container IP it is binding to.

> [@arlogb](#):
>
> I’ve solved this by moving to postgres.

Great! 😺

---

<div class="post-metadata">

**Author:** ![FelicianoTech](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/felicianotech/32/21348_2.png) [@FelicianoTech](https://discuss.circleci.com/u/FelicianoTech)\
**Post date:** [March 20, 2018, 3:05pm UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857/6 "2018-03-20T15:05:13Z")

</div>

The first step is to fix the syntax of the configuration file. Those environment variables aren’t being listed correctly. They are suppose to be a map not a list. You can review the format here: [https://circleci.com/docs/2.0/configuration-reference/#docker](https://circleci.com/docs/2.0/configuration-reference/#docker)

After that, please provided an updated config here and we can continue to look at it and hopefully get you up and running. 🙂

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [March 20, 2018, 4:21pm UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857/7 "2018-03-20T16:21:15Z")

</div>

Ah, I completely forgot, I have a [working MySQL configuration here](https://bitbucket.org/halferbits/circleci-help/src/44c2591cae1f149ac7557b5325f5b1f071d32559/.circleci/config.yml?at=master&fileviewer=file-view-default). It wasn’t easy to get working, but they were not Circle problems. The major one is MariaDB is not started by default, but there were others too.

I think the person I helped had given up with running MySQL in a separate container, but that ought to work, and it’d probably be easier to set up also. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [June 18, 2018, 4:21pm UTC](https://discuss.circleci.com/t/connecting-to-mysql-on-localhost-127-0-0-1/20857/8 "2018-06-18T16:21:25Z")

</div>

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