6

Monit is configured to watch MySQL on localhost at port 3306.

check process mysqld with pidfile /var/lib/mysql/li175-241.pid
  start program = "/etc/init.d/mysql start"
  stop program = "/etc/init.d/mysql stop"
  if failed port 3306 protocol mysql then restart
  if 5 restarts within 5 cycles then timeout

My application, which is configured to connect to MySQL via localhost:3306, is running just fine and can access the database. I can even use MySQL Query Browser to connect to the database remotely via port 3306. The port is totally open and possible to connect to. Therefore, I'm pretty darn certain that it's running.

However, running monit -v reveals that Monit cannot detect MySQL on that port.

'mysqld' failed, cannot open a connection to INET[localhost:3306] via TCP

This happens consistently, until Monit decides not to track MySQL anymore, as configured.

How can I begin to troubleshoot this issue?


sudo netstat -lnp | grep mysql returns the following:

tcp        0      0 173.230.135.241:3306    0.0.0.0:*               LISTEN      14357/mysqld    
unix  2      [ ACC ]     STREAM     LISTENING     265228   14357/mysqld        /var/run/mysqld/mysqld.sock
5
  • Can you please paste the output of sudo netstat -lnp | grep mysql?
    – Khaled
    Jan 9, 2011 at 8:00
  • @Khaled: edited in :)
    – Matchu
    Jan 10, 2011 at 2:57
  • Just out of curiousity, is the name of the pid file correct? After all, monit is looking for the pid file, not an actual connection to MySQL. Jan 10, 2011 at 3:35
  • @John: I believe it checks both. The PID check passes, and the connection test fails.
    – Matchu
    Jan 11, 2011 at 1:52
  • 1
    Posting this for others who have this issue. I had to open the port in my firewall to allow this as well: '-A INPUT -s 127.0.0.1/32 -p tcp -m tcp --dport 3306 -j ACCEPT`, then reload firewall rules, restart monit, and it worked.
    – user179657
    Jun 29, 2013 at 2:02

4 Answers 4

4

'mysqld' failed, cannot open a connection to INET[localhost:3306] via TCP

This error shows that monit is trying to connect to port 3306 on localhost, which is the IP address 127.0.0.1

tcp 0 0 173.230.135.241:3306 0.0.0.0:* LISTEN 14357/mysqld

This netstat output shows that mysqld is listening on the IP address mentioned. It is not listening on localhost.

You either need to make mysqld listen on localhost as well, or you need to tell monit to check the specific IP address, rather than defaulting to localhost.

4
  • 1
    Interesting. What's the difference between 0.0.0.0 and localhost? My web app can connect via localhost, so I'm probably missing something.
    – Matchu
    Jan 11, 2011 at 1:53
  • In the meantime, I've hardcoded the IP address, so that should do for now. Thanks!
    – Matchu
    Jan 11, 2011 at 1:58
  • @Matchu: the output from netstat says that mysqld is only listening on the 173.230.135.241 IP address, unless you didn't paste some of the output. It also says that it's listening for incoming connections from 0.0.0.0 - which is the global address. Jan 12, 2011 at 3:43
  • @Matchu your webapp probably isn't connecting to localhost (127.0.0.1), but is either using the 172.etc IP address, or connecting via a UNIX socket (which netstat also says is open). I'm pretty sure a lot of APIs will immediately prefer a socket if "localhost" is specified, which somewhat masks this issue. Jan 12, 2011 at 3:44
1

This works for me - using the mysql socket rather than its port (on a debian machine):

check process mysql with pidfile /var/run/mysqld/mysqld.pid
   group database
   start program = "/etc/init.d/mysql start"
   stop program = "/etc/init.d/mysql stop"
   #if failed host 192.168.1.222 port 3306 protocol mysql then restart
   if failed unix "/var/run/mysqld/mysqld.sock" protocol mysql then restart
   depends on mysql_bin
   depends on mysql_rc

 check file mysql_bin with path /usr/bin/mysql
   group database
   if failed checksum then unmonitor
   if failed permission 755 then unmonitor
   if failed uid root then unmonitor
   if failed gid root then unmonitor

 check file mysql_rc with path /etc/init.d/mysql
   group database
   if failed checksum then unmonitor
   if failed permission 755 then unmonitor
   if failed uid root then unmonitor
   if failed gid root then unmonitor
0

Is your application running on the same server as mysql? Is Monit running on that same server?

Mysql may be blocking external connections.

5
  • All on the same server :/
    – Matchu
    Jan 8, 2011 at 21:35
  • You may also need to create a monit user for mysql. Have you done that?
    – Publiccert
    Jan 8, 2011 at 21:38
  • I couldn't find anywhere that indicated that Monit would be actually logging into the MySQL database rather than just confirming that it was running. Where would I specify the username and password in Monit's config?
    – Matchu
    Jan 8, 2011 at 22:08
  • "allow admin:monit" added anywhere in your monit config should do the track, once you add the username.
    – Publiccert
    Jan 8, 2011 at 22:20
  • I'm fairly confident that the allow command is only permitted in areas specifying who is able to access the HTTP interface.
    – Matchu
    Jan 8, 2011 at 22:29
0

I just found this question because i had the same problem. Running Monit 5.12.2 the trick is to uppercase the protocol. This is what i found working for me:

check process mysqld with pidfile /var/run/mysqld/mysqld.pid
    group database
    start program "/root/scripts/restart.mysql.sh" with timeout 900 seconds
    stop program = "/etc/init.d/mysql stop"
    if failed port 3306 protocol MYSQL then restart
    if failed unixsocket /var/run/mysqld/mysqld.sock protocol MYSQL then restart
    if 5 restarts within 5 cycles then timeout
    depends on mysql_bin
    depends on mysql_rc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.