Docker Mysql Slow

Assume that you have two docker services/containers. One must be ready to accept connections from the other one. As you have just noticed, I didn't say 'one must be running' because there is different between being 'ready to accept connections' and 'running'. The depends_on key of docker compose doesn't always solve the problem because it just checks if the container is running. This is often the case if a service depends on MySQL, RabbitMQ, Elasticsearch like services. They take time to accept connections. To solve such issue, we can use an additional script. This script would periodically ping these services until they are ready to accept connections before actually running the main service.


Docker Mysql Slow Motion

In this example we have a Go and MySQL service. Go depends on MySQL. MySQL is slow in accepting connections. We will use our script in Go to force it to wait for MySQL to accept connections first. Once MySQL says it is ready then we will bring up the Go service.


2.Start a MySQL Container in Docker. The next step is to run a container in Docker with the MySQL image. To do this, execute the next command: docker run -name=mysql1 -p 3306:3306 -e MYSQLROOTPASSWORD=123456 -d mysql/mysql-server:8.0. Let’s break down this command to understand it better: run - will run a new command in a new Docker container.

  • Sometimes these problems are, in fact, due to slow queries. In this blog, we'll deal with slow queries and how to identify these. Checking Your Slow Query Logs. MySQL has the capability to filter and log slow queries. There are various ways you can investigate these, but the most common and efficient way is to use the slow query logs.
  • I'm using Docker for Windows with the WSL 2 backend as it works on W10 Home. The way I develop is I run the VS Code extension for Remote Containers and that mounts the workspace/folder to the container, so that it's isolated from other projects. Everything works but compiling JS projects is very very slow.
  • Re: SOLVED Very slow Docker MySQL container What storage driver are you using (see 'docker info')? If you are using devicemapper, which is the default unless you have an AUFS enabled kernel or a btrfs partition, I'd suggest creating a btrfs partition for /var/lib/docker and trying again.

Structure


Motion

Files


main.go



Docker Mysql Slow Start

Dockerfile


init.sh


You can use exactly the same script for other services or just add other services here.



docker-compose.yaml


Test


Failed to connect in 10 seconds


Docker mysql slow query log

I actually changed the 1 second to 0.001 (1ms) in the script in order to simulate termination here.



Successfully connected in 3 seconds


Related

Join the DigitalOcean Community

Join 1M+ other developers and:

  • Get help and share knowledge in Q&A
  • Subscribe to topics of interest
  • Get courses & tools that help you grow as a developer or small business owner
CPU usage problem by MySQL In a WordPress site Question
Create a Droplet from a snapshot using the snapshot's name instead of its id Question
Docker

Question

Hi,
I’m trying to run a simple project with nginx, php and mysql on a droplet with 2 vCPU and 2 GB Ram using the Ubuntu-Docker-Image. The project runs on a really cheap host so far and collects about 1.5GB of data in a database so far and I’m trying to migrate this to a docker environment on Digital Ocean. Basically the main (InnoDB) tables are blogs (about 200 entries) and blog_posts (about 200000 entries).

My problem is, that a simple query is awful slow! Its basically like:

So, nothing special. All used fields are indexed. On my local MacBook with the same docker-compose.yml the query takes about 400ms. On the droplet it takes more than 9 seconds!

So far i tried:

  • use mysql 8
  • use mysql 5.7
  • use mysql 5.6
  • use mariadb
  • run Optimize-Table-Query
  • additional indexed the PrimaryKey fields with a regular INDEX
  • modified the fstab with barrier=0 for that volume
  • changed the provider, Digital Ocean is the third now
  • tried a larger configuration (more RAM)
  • changed the innodb-default-row-format to Compact instead of dynamic

Nothing made this query noticeable faster.
When monitoring the server during those queries neither the CPU nor the RAM or the disk I/O seem to reach the limit.
When profiling the query the most time is used during “Send Data”
The best configuration so far is mariadb because it caches the query, but without caching or with changing/adding more parameter it is as slow as the other configurations.

I’m running all conatiner with a single docker-compose.yml and each component as a single service using:

  • nginx (nginx:alpine)
  • php-fpm (docker/php-fpm)
  • mysql (mysql:5.7 / mysql:5.6 / mariadb)

The MySql Data is mounted as volume

So I’m really run out of Ideas! I’m struggling with this for 2 weeks now and really hope that someone can help me to find the bottleneck.

Related

Join the DigitalOcean Community

Join 1M+ other developers and:

  • Get help and share knowledge in Q&A
  • Subscribe to topics of interest
  • Get courses & tools that help you grow as a developer or small business owner
CPU usage problem by MySQL In a WordPress site Question
Create a Droplet from a snapshot using the snapshot's name instead of its id Question
Docker mysql slow download

Docker Mysql Slow Query Log

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Docker Mysql Slow Download

×