# MySQL Slow Performance In Docker

**URL:** <https://forums.docker.com/t/mysql-slow-performance-in-docker/37179>\
**Category:** General\
**Created:** [August 26, 2017, 7:14pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179 "2017-08-26T19:14:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![weikai](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@weikai](https://forums.docker.com/u/weikai)\
**Post date:** [August 26, 2017, 7:14pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/1 "2017-08-26T19:14:05Z")

</div>

I’m running Docker on Ubuntu Xenial with the following command. For certain reason it’s 15 times slower than Virtual Box on the same machine with ext4. It’s about 5 times slower on XFS. I have tried different configuration but couldn’t figure out why its such slow. Is there anyway we can optimize MySQL performance in docker?

docker run -d --env=“MYSQL\_ROOT\_PASSWORD=drupal” --name=mysql -p 3306:3306 mysql

MySQL restore test in VirtualBox  
weikai@drupal:~$ time mysql vbox \< drupal3.sql  
real 2m12.081s  
user 0m4.868s  
sys 0m1.208s

MySQL restore test in Docker  
root@e3488a990fde:~# time mysql -u root -p dockerdb \< drupal3.sql  
Enter password:

real 11m47.783s  
user 0m4.784s  
sys 0m13.316s

---

<div class="post-metadata">

**Author:** ![terpz](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/terpz/32/36125_2.png) [@terpz](https://forums.docker.com/u/terpz)\
**Post date:** [August 27, 2017, 1:53pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/3 "2017-08-27T13:53:43Z")

</div>

You can try and “mount” a local folder, instead of using the container as storage.  
try adding: -v $HOME/datadir:/var/lib/mysql

---

<div class="post-metadata">

**Author:** ![weikai](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@weikai](https://forums.docker.com/u/weikai)\
**Post date:** [August 28, 2017, 4:23am UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/4 "2017-08-28T04:23:45Z")

</div>

It makes no differences. It’s about 15 times slower than the VirtualBox on the same machine since $HOME is on ext4 partition.

root@3ff6a442cbb2:~# time mysql dockerdb \< drupal3.sql

real 29m41.965s  
user 0m6.456s  
sys 0m0.472s

weikai@cloud:~$ mount|grep ‘^/dev’  
/dev/mapper/cloud–vg-root on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)  
/dev/sda1 on /boot type ext2 (rw,relatime,block\_validity,barrier,user\_xattr,acl)  
/dev/sde on /mnt/disk1 type ext4 (rw,relatime,data=ordered)  
/dev/mapper/data–vg-docker on /var/lib/docker type xfs (rw,relatime,attr2,inode64,noquota)  
/dev/mapper/data–vg-data on /srv type ext4 (rw,relatime,data=ordered)  
/dev/mapper/data–vg-data on /home type ext4 (rw,relatime,data=ordered)  
/dev/mapper/data–vg-vbox on /home/vbox type ext4 (rw,relatime,data=ordered)  
/dev/mapper/data–vg-vbox on /srv/home/vbox type ext4 (rw,relatime,data=ordered)  
/dev/mapper/data–vg-docker on /var/lib/docker/plugins type xfs (rw,relatime,attr2,inode64,noquota)  
/dev/mapper/data–vg-docker on /var/lib/docker/aufs type xfs (rw,relatime,attr2,inode64,noquota)

weikai@cloud:~$ docker info  
Containers: 3  
Running: 3  
Paused: 0  
Stopped: 0  
Images: 89  
Server Version: 17.06.1-ce  
Storage Driver: aufs  
Root Dir: /var/lib/docker/aufs  
Backing Filesystem: xfs  
Dirs: 106  
Dirperm1 Supported: true  
Logging Driver: json-file  
Cgroup Driver: cgroupfs  
Plugins:  
Volume: local  
Network: bridge host macvlan null overlay  
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog  
Swarm: inactive  
Runtimes: runc  
Default Runtime: runc  
Init Binary: docker-init  
containerd version: 6e23458c129b551d5c9871e5174f6b1b7f6d1170  
runc version: 810190ceaa507aa2727d7ae6f4790c76ec150bd2  
init version: 949e6fa  
Security Options:  
apparmor  
seccomp  
Profile: default  
Kernel Version: 4.4.0-92-generic  
Operating System: Ubuntu 16.04.3 LTS  
OSType: linux  
Architecture: x86\_64  
CPUs: 8  
Total Memory: 19.5GiB  
Name: cloud  
ID: Q4IZ:YS4B:QDBR:5W6J:W4EN:ZT2T:72RE:X3MT:SUWI:BZGD:L34Q:KGI7  
Docker Root Dir: /var/lib/docker  
Debug Mode (client): false  
Debug Mode (server): false  
Registry: [https://index.docker.io/v1/](https://index.docker.io/v1/)  
Experimental: false  
Insecure Registries:  
127.0.0.0/8  
Live Restore Enabled: false

WARNING: No swap limit support

---

<div class="post-metadata">

**Author:** ![redsandro](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@redsandro](https://forums.docker.com/u/redsandro)\
**Post date:** [November 15, 2017, 9:46am UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/5 "2017-11-15T09:46:38Z")

</div>

I’m experiencing the same issue. MySQL is very slow. I’m running `mysql:5.7` on Docker version `17.09.0-ce`, build `afdb6d4` with a local volume for the data directory, which is `ext4`. The volume driver is `local` and on my system the mounts show up as `nsfs`.

Importing a gigabyte of data from an external server takes half an hour on localhost, but close to 24 hours on this container.

```auto
Server Version: 17.09.0-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 06b9cb35161009dcb7123345749fef02f7cea8e0
runc version: 3f2f8b84a77f73d38244dd690525642a72156c64
init version: 949e6fa
Kernel Version: 4.4.0-93-generic
Operating System: Linux Mint 18
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.64GiB
Docker Root Dir: /home/local/docker
Debug Mode (client): false
Debug Mode (server): false
Experimental: false
Live Restore Enabled: false

WARNING: No swap limit support

```

---

<div class="post-metadata">

**Author:** ![trendevfr](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@trendevfr](https://forums.docker.com/u/trendevfr)\
**Post date:** [November 15, 2017, 8:31pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/6 "2017-11-15T20:31:47Z")

</div>

Same there ☹ I’ve tested with volumes as well and same behaviour. Perfs divided by 10.

---

<div class="post-metadata">

**Author:** ![weikai](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@weikai](https://forums.docker.com/u/weikai)\
**Post date:** [November 18, 2017, 3:02pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/7 "2017-11-18T15:02:12Z")

</div>

Not many people experience the issue. I’m wondering if we can find something in common. The following is my server information. The slow less problem is gone if I mount the volume on the small SSD. I’m wondering why it’s this slow on the magnetic drive (Internal drive Seagate 8TB BarraCuda Pro). VirtualBox instances running on the same machine using magnetic partition can run 10 - 20 times faster on the same MySQL operations.

OS: Ubuntu 16.04  
CPU: Intel® Xeon® CPU E31235 @ 3.20GHz  
Hard Drives: sda SSD lvm 128MB  
sdb magnetic lvm 8TB

---

<div class="post-metadata">

**Author:** ![redsandro](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@redsandro](https://forums.docker.com/u/redsandro)\
**Post date:** [December 8, 2017, 11:58am UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/8 "2017-12-08T11:58:47Z")

</div>

OS: Ubuntu 16.04.3 (Linux Mint 18.3)  
Kernel: 4.10.0-40-generic x86\_64  
Volume: Crucial m4 SSD 256GB  
CPU: Intel Core i7-3770 @ 3.40 GHz

---

<div class="post-metadata">

**Author:** ![redsandro](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@redsandro](https://forums.docker.com/u/redsandro)\
**Post date:** [December 14, 2017, 1:10pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/9 "2017-12-14T13:10:29Z")

</div>

Hey @Everyone experiencing this issue. I did an unexpected discovery.

In order to circumvent this problem, I connected my docker container to a fast MySQL setup running on the localhost, outside of docker.

However, I was surprised to see there was no speed improvement at all. Importing 1GB of data still took 24 hours.

This leads me to believe there is a serious and unexpected bottleneck in the docker system when using a mysql- **client** , and the culprit is not the mysql- **server**.

---

<div class="post-metadata">

**Author:** ![renomarx](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@renomarx](https://forums.docker.com/u/renomarx)\
**Post date:** [January 9, 2018, 9:00am UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/10 "2018-01-09T09:00:45Z")

</div>

Hi @redsandro, everyone,

I had the same problem, running mysql anormally slow in docker, and discovered that it had nothing to do with docker at all (i have pretty much the same configuration as you, but with ext4 partition); it was my file system extremly slow with mysql (and maybe with other packages but i didn’t notice).  
I resolved the problem following this tutorial:  
[http://phpforus.com/how-to-make-mysql-run-fast-with-ext4-on-ubuntu/](http://phpforus.com/how-to-make-mysql-run-fast-with-ext4-on-ubuntu/)

Not the best I presume, but after that, I had the exactly same performance as my colleagues who were on another system.

---

<div class="post-metadata">

**Author:** ![weikai](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@weikai](https://forums.docker.com/u/weikai)\
**Post date:** [January 9, 2018, 2:39pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/11 "2018-01-09T14:39:07Z")

</div>

The performance problem on my system is actually inside Docker. Mysql running on both Ubuntu physical Host and VirtualBox instances are 10X+ time faster than MySQL server inside Docker.

---

<div class="post-metadata">

**Author:** ![redsandro](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@redsandro](https://forums.docker.com/u/redsandro)\
**Post date:** [January 9, 2018, 3:25pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/12 "2018-01-09T15:25:29Z")

</div>

Thanks @renomarx for sharing this information. I’m glad you’ve found a way around this. I have to agree with @weikai though; this problem is more complicated than it looks.

In my case, I have moved the MySQL server from a Docker container to my _localhost_, which is also _ext4_, and it was still slow. But **hosting the application locally in stead of in docker** - while using that same local MySQL server - improves the speed more than drastically.

Unintuitively, It seems that this is not Docker-specific volume/mount/link/share/volume related, but Docker related nontheless.

---

<div class="post-metadata">

**Author:** ![weikai](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@weikai](https://forums.docker.com/u/weikai)\
**Post date:** [January 9, 2018, 7:36pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/13 "2018-01-09T19:36:49Z")

</div>

Docker performance issue on my server is resolved. It’s quicker than VirtualBox now. Thank you @renomarx for the article link. I mounted an ext4 partition at /var/lib/docker with barrier=0. It also explains why the problem only happens on magnetic disk not SSD on the same server because SSD doesn’t have barrier on by default.

---

<div class="post-metadata">

**Author:** ![alvarow](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/alvarow/32/13230_2.png) [@alvarow](https://forums.docker.com/u/alvarow)\
**Post date:** [January 22, 2018, 10:45pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/14 "2018-01-22T22:45:01Z")

</div>

I am still facing this problem ☹ Also happens on Docker for Mac… mysql imports take a very long time. If I run the client from a different machine it takes half as long, but still 20min to import aprox 300mb of files.

---

<div class="post-metadata">

**Author:** ![hotrush](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/hotrush/32/18728_2.png) [@hotrush](https://forums.docker.com/u/hotrush)\
**Post date:** [February 1, 2018, 7:59am UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/15 "2018-02-01T07:59:42Z")

</div>

Same issue with mysql:5.7 on Azure

---

<div class="post-metadata">

**Author:** ![dockrist](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@dockrist](https://forums.docker.com/u/dockrist)\
**Post date:** [February 1, 2018, 2:05pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/16 "2018-02-01T14:05:55Z")

</div>

Almost same here. I realized queries to the database were about 5 times slower compared to a non-docker MySQL installation.

After I ran **“optimize table”** on every table in the mysql docker, the query ran 22 times faster than before and 4-5 times faster compared to the non-docker MySQL installation.  
However, after a complete drop database and fresh import of all tables, the performance of the query was just as fast as after the optimization of the tables.

My conclusion was, that during the (development) usage of the mysql docker the performance might have become worse and worse. Maybe the index files are getting corrupted by stopping a stack with ctrl-c rather than docker-compose down.

I’ll keep an eye on it, to confirm or falsify the above.

---

<div class="post-metadata">

**Author:** ![correnz](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@correnz](https://forums.docker.com/u/correnz)\
**Post date:** [March 2, 2018, 1:31pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/17 "2018-03-02T13:31:25Z")

</div>

We have experienced a similar issue where MySQL is extremely slow. But not only in a container, also on a Ubuntu system directly if docker is installed.  
We were unable to solve this issue in many hours, so we have created a ticket on github:

> <https://github.com/docker/for-linux/issues/247>

---

<div class="post-metadata">

**Author:** ![kazanz](https://avatars.discourse-cdn.com/v4/letter/k/5e9695/32.png) [@kazanz](https://forums.docker.com/u/kazanz)\
**Post date:** [March 29, 2018, 5:44pm UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/18 "2018-03-29T17:44:20Z")

</div>

I too am experiencing the same issue with docker for mac. Mysql in a virtualbox with vagrant is exponentially faster

---

<div class="post-metadata">

**Author:** ![wahani](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/wahani/32/20053_2.png) [@wahani](https://forums.docker.com/u/wahani)\
**Post date:** [June 8, 2018, 7:44am UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/19 "2018-06-08T07:44:38Z")

</div>

Hi. I think I am having a similar problem:

- hosting the server with or without docker does not change performance.

- When the application runs outside of docker I have normal/expected performance. No matter on which machine in my network.

- When I run the application inside a container with `--network host` on the same machine as the mysql database + using 127.0.0.1 as host address: I have normal performance.

- When I am running the application inside docker on a different machine than the database performance goes down. Here I get a net\_read\_timeout while running a `LOAD DATA LOCAL INFILE`- however this seems to kick in when the file becomes ‘large’ (\> 1000 lines, 3 cols) and works otherwise. The read speed is so low that raising the timeout is not an option.

Did you find any solution for yourself?

---

<div class="post-metadata">

**Author:** ![redsandro](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@redsandro](https://forums.docker.com/u/redsandro)\
**Post date:** [June 8, 2018, 11:08am UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/20 "2018-06-08T11:08:13Z")

</div>

That sounds about right. Unfortunately we did not find a solution. We’re not running the main app on docker anymore because of this.

---

<div class="post-metadata">

**Author:** ![otarza](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/otarza/32/15484_2.png) [@otarza](https://forums.docker.com/u/otarza)\
**Post date:** [April 1, 2019, 9:43am UTC](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179/21 "2019-04-01T09:43:43Z")

</div>

Hello guys,

I had same issue and the instruction @renomarx linked helped to fix it. Mysql performance under Docker is dramatically improved.

Here is my setup:

- Macbook pro retina late 2013, 8GB RAM, i5, SSD with dual boot Ubuntu 18.10.

Thank you!

[Next page](https://forums.docker.com/t/mysql-slow-performance-in-docker/37179.md?page=2)
