MySQL 5.6.26
Trying to log queries to stdout doesn't work below,
$ mysqld --general_log=1 --general_log_file=/dev/stdout
To disable or enable the general query log or change the log file name at runtime, use the global general_log and general_log_file system variables. Set general_log to 0 (or OFF ) to disable the log or to 1 (or ON ) to enable it. Set general_log_file to specify the name of the log file.
The general query log is a log of every SQL query received from a client, as well as each client connect and disconnect. Since it's a record of every query received by the server, it can grow large quite quickly.
MySQL Server has several logs that can help you find out what activity is taking place. By default, no logs are enabled, except the error log on Windows. (The DDL log is always created when required, and has no user-configurable options; see Section 5.4.
You can't, not directly. The query logs only go to files, tables or nowhere. They don't seem to go to /dev/stdout
nor honor the -
convention. Don't worry, there's a much better way.
If all you want to do is see the query log as it happens, you can stream the log file using various Unix utilities. tail -f
is one option.
tail -f /path/to/query.log
I prefer opening the log with less
and using the F
command to keep reading as the file is added to. It's like tail -f
but you can also scroll around and search the file.
$ less /path/to/query.log
~
~
/opt/local/lib/mysql56/bin/mysqld, Version: 5.6.27-log (Source distribution). started with:
Tcp port: 0 Unix socket: (null)
Time Id Command Argument
151004 16:10:51 1 Connect schwern@localhost as on
1 Query select @@version_comment limit 1
151004 16:10:53 1 Query select 1
151004 16:10:54 1 Quit
Waiting for data... (interrupt to abort)
I am very surprised at how difficult it is to do this in MySQL vs. PostgreSQL.
Following below is the config that allowed me to send all MySQL logs to the Docker container's stdout.
Contents of docker-compose.yml
:
---
version: "3.9"
services:
your_db:
image: mysql:5.7-debian
command:
- /usr/local/bin/mysqld.sh
environment:
MYSQL_DATABASE: "$your_db"
MYSQL_PASSWORD: "$your_pass"
MYSQL_ROOT_PASSWORD: "$root_pass"
MYSQL_USER: "$your_user"
volumes:
- ./my.cnf:/etc/mysql/conf.d/my.cnf:ro
- ./mysqld.sh:/usr/local/bin/mysqld.sh:ro
networks:
- your_net
Contents of my.cnf
:
[mysqld]
# Log General
general_log = 1
general_log_file = /var/log/mysql_general.log
# Log Error
log_error = /var/log/mysql_error.log
# Log Slow
slow_query_log = 1
slow_query_log_file = /var/log/mysql_slow_query.log
long_query_time = 0 # 0 has the effect of logging all queries
log_queries_not_using_indexes = 1
Contents of mysqld.sh
:
#!/usr/bin/env bash
set -euo pipefail
##
# This script exists to work-around the fact that
# mysql does not support logging to stdout
#
# This will tail the file logs configured in ./my.cnf
##
LOG_PATHS=(
'/var/log/mysql_general.log'
'/var/log/mysql_error.log'
'/var/log/mysql_slow_query.log'
)
for LOG_PATH in "${LOG_PATHS[@]}"; do
# https://serverfault.com/a/599209
( umask 0 && truncate -s0 "$LOG_PATH" )
tail --pid $$ -n0 -F "$LOG_PATH" &
done
docker-entrypoint.sh mysqld
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With