Search code examples
mysqlbashnetcathealth-monitoring

Healthcheck causing 'Got an error reading communication packets' messages


I'm using the following healthcheck to see if the 3306 port of MySQL server is open.

bash -c 'cat < /dev/null > /dev/tcp/localhost/3306' || exit 1

It's working fine, but some time ago MySQL started to print warnings after each check:

[Note] Got an error reading communication packets

Is there a way to check if the port is open without triggering the above message? The healthcheck is executed every few seonds causing the messages to spam the log output.


Solution

  • I was getting the same log messages when I used fsockopen to open a socket to port 3306 and then disconnect it without sending any data.

    Since MySQL expects to receive some data packets after the connection is opened, if none are provided, it emits "[Note] Got an error reading communication packets" to the error log.

    Instead of opening and closing a TCP socket, use mysqladmin ping with an anonymous user to check if MySQL is alive. It has the further advantage that it checks if the process listening on the port is actually MySQL server.

    $ mysqladmin ping -h running_host -u anonymous --password=pass &>/dev/null ; echo $?
    0
    
    $ mysqladmin ping -h not_running_host -u anonymous --password=pass &>/dev/null ; echo $?
    1
    

    From the documentation, "The return status from mysqladmin is 0 if the server is running, 1 if it is not. This is 0 even in case of an error such as Access denied, because this means that the server is running but refused the connection, which is different from the server not running. "

    While it is not necessary to specify a username and password, doing so may result in "[Note] Access denied for user 'user'@'host' (using password: NO)" messages in the error log. As a workaround, create an anonymous user in MySQL with no grants on any database.

    The above command discards all output and just echos the exit code instead.