# Can't connect to circleci/mysql:5.7 container on localhost or 127.0.0.1

**URL:** <https://discuss.circleci.com/t/cant-connect-to-circleci-mysql-5-7-container-on-localhost-or-127-0-0-1/16932>\
**Category:** Databases\
**Tags:** mysql\
**Created:** [September 28, 2017, 4:37pm UTC](https://discuss.circleci.com/t/cant-connect-to-circleci-mysql-5-7-container-on-localhost-or-127-0-0-1/16932 "2017-09-28T16:37:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![renaudguerin](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/renaudguerin/32/8605_2.png) [@renaudguerin](https://discuss.circleci.com/u/renaudguerin)\
**Post date:** [September 28, 2017, 4:37pm UTC](https://discuss.circleci.com/t/cant-connect-to-circleci-mysql-5-7-container-on-localhost-or-127-0-0-1/16932/1 "2017-09-28T16:37:28Z")

</div>

I’m using `circleci/mysql:5.7` as a secondary container, because it has the right environment variables set from the start.

The doc says all containers should be on the same network, but I can’t connect to it from my primary container (a private Debian-based image with “mysql-client” installed), and yes I’m forcing a TCP connection to avoid using the Unix socket.

```
mysql --protocol=tcp "SELECT 1"
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (99)
aExited with code 1

```

Same with MYSQL\_HOST set to 127.0.0.1 in the primary container environment :

```
mysql --protocol=tcp "SELECT 1"
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
aExited with code 1

```

Any ideas very welcome

---

<div class="post-metadata">

**Author:** ![renaudguerin](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/renaudguerin/32/8605_2.png) [@renaudguerin](https://discuss.circleci.com/u/renaudguerin)\
**Post date:** [September 28, 2017, 4:50pm UTC](https://discuss.circleci.com/t/cant-connect-to-circleci-mysql-5-7-container-on-localhost-or-127-0-0-1/16932/2 "2017-09-28T16:50:33Z")

</div>

Ok, my MySQL command line syntax is wrong for one thing, but that didn’t explain the connection failure.

It seems that the MySQL container just wasn’t ready to accept connections, after sleeping for a few seconds it all works.

How do you usually solve that ? A sleep command is ugly.  
Is it possible to pause the primary container until the others are ready ?

---

<div class="post-metadata">

**Author:** ![levlaz](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/levlaz/32/36978_2.png) [@levlaz](https://discuss.circleci.com/u/levlaz)\
**Post date:** [September 29, 2017, 5:54am UTC](https://discuss.circleci.com/t/cant-connect-to-circleci-mysql-5-7-container-on-localhost-or-127-0-0-1/16932/3 "2017-09-29T05:54:45Z")

</div>

You cannot pause a container until others are ready, but I’ve done something like this in the past (for PostgreSQL) which should work for any arbitrary command as well.

There is still a sleep, but it will take as long as it needs rather than being hard coded.

```auto
  until psql -U postgres -c '\l'; do
    >&2 echo "Postgres is starting up ... will try again momentarily"
    sleep 1
  done

```

---

<div class="post-metadata">

**Author:** ![renaudguerin](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/renaudguerin/32/8605_2.png) [@renaudguerin](https://discuss.circleci.com/u/renaudguerin)\
**Post date:** [September 29, 2017, 2:45pm UTC](https://discuss.circleci.com/t/cant-connect-to-circleci-mysql-5-7-container-on-localhost-or-127-0-0-1/16932/4 "2017-09-29T14:45:54Z")

</div>

Thanks, that’s what I had in mind, seems good enough.

---

<div class="post-metadata">

**Author:** ![levlaz](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/levlaz/32/36978_2.png) [@levlaz](https://discuss.circleci.com/u/levlaz)\
**Post date:** [September 29, 2017, 4:51pm UTC](https://discuss.circleci.com/t/cant-connect-to-circleci-mysql-5-7-container-on-localhost-or-127-0-0-1/16932/5 "2017-09-29T16:51:34Z")

</div>

For what it’s worth if you use docker compose in production you may run into this issue as well so I usually add a similar script to the entry point of my app.

---

<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:** [November 10, 2017, 8:51am UTC](https://discuss.circleci.com/t/cant-connect-to-circleci-mysql-5-7-container-on-localhost-or-127-0-0-1/16932/6 "2017-11-10T08:51:42Z")

</div>

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