Skip to content

Instantly share code, notes, and snippets.

@akirattii
Last active July 14, 2023 22:12
Show Gist options
  • Save akirattii/40b284332e887f2f3dcdc4bca08517a5 to your computer and use it in GitHub Desktop.
Save akirattii/40b284332e887f2f3dcdc4bca08517a5 to your computer and use it in GitHub Desktop.
memo: MySQL docker container settings for slow query log

Check running mysql container:

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
26137efa979f        mysql               "docker-entrypoint..."   8 months ago        Up 10 minutes       0.0.0.0:3306->3306/tcp   container-mysql

Check docker container mysql version:

$ sudo docker exec container-mysql mysqld --version`
mysqld  Ver 5.7.15 for Linux on x86_64 (MySQL Community Server (GPL))

Check current mysqld.cnf:

$ sudo docker exec container-mysql cat /etc/mysql/mysql.conf.d/mysqld.cnf

Copy mysqld.cnf to local:

$ sudo docker cp container-mysql:/etc/mysql/mysql.conf.d/mysqld.cnf ~/mysqld.cnf

Edit config:

$ sudo nano ~/mysqld.cnf`

[mysqld]
skip-host-cache
skip-name-resolve
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
#log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Slow query settings:
slow_query_log=1
slow_query_log_file=/var/log/mysql/slow.log
long_query_time=0.1

character-set-server=utf8mb4

[client]
default-character-set=utf8mb4

Copy new mysqld.cnf to the container:

$ sudo docker cp ~/mysqld.cnf container-mysql:/etc/mysql/mysql.conf.d/mysqld.cnf

Restart mysql container:

$ sudo docker restart container-mysql

Check slow query log:

$ sudo docker exec container-mysql cat /var/log/mysql/slow.log

@daominhsangvn
Copy link

Can we stream the mysql logs to container logs?

@pupseba
Copy link

pupseba commented Mar 10, 2022

How these logs would be rotated?

@phanthaihuan
Copy link

How these logs would be rotated?

I think it is better to install logrotate daemon inside the container to handle it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment