5,548,129 members and growing! (18,414 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

How To Automatically restart MySQL on Windows

By Emmanuel Kartmann

This article provides a small executable (36K) which configures MySQL (or another Windows Service) to automatically restart whenever it fails/crashes.
SQL, VC7, C++Windows, Win2K, WinXP, Win2003, SQL Server, MySQL, VS.NET2002, Visual Studio, Architect, DBA, Dev

Posted: 2 May 2005
Updated: 2 May 2005
Views: 24,117
Bookmarked: 16 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 3.88 Rating: 4.59 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 14.3%
3
1 vote, 14.3%
4
5 votes, 71.4%
5

Introduction

This article presents a not-so-known feature of Windows 2000 and above: the ability to automatically restart a Windows Service when it crashes. We use this feature to improve the reliability of many services, including MySQL which (alas) sometimes crashes unexpectedly.

Background

A Windows Service is a specially designed application that can be started automatically at system boot. Windows Services can execute even when no user is logged on to the system. Windows services are usually implemented in C++, but can also be written in any of the .NET languages (C#, VB.NET).

Since Windows 2000, Windows Services support corrective actions - actions that are to be taken in case of a failure (a Service is considered failed when it terminates without reporting a status of SERVICE_STOPPED to the Service Control Manager).

Whenever a service fails, the Service Control Manager detects it and logs an error in System Event Log:

The XXX service terminated unexpectedly. It has done this YYY time(s).

If the service has an associated corrective action, it logs the following message and executes the corrective action:

The following corrective action will be taken in ZZZ milliseconds: ACTION

Corrective actions can be:

  • No action. (SC_ACTION_NONE; the default)
  • Restart the service. (SC_ACTION_RESTART)
  • Reboot the system. (SC_ACTION_REBOOT)
  • Run any command.(SC_ACTION_RUN_COMMAND)

This sample application, written in Visual C++ .NET, uses the Win32 function ChangeServiceConfig2 to configure the MySQL Service to automatically restart in case of a failure. It can be called (or integrated) as part of a setup/installation procedure of MySQL on Windows.

Using the application

You can run the application from the command line prompt (CMD.EXE):

C:\> MySQLServiceSetCorrectiveActions.exe
SUCCESS: Configuration for Service "MySQL" changed!

You can use the application to configure another service, e.g. W3SVC (IIS):

C:\> MySQLServiceSetCorrectiveActions.exe W3SVC
SUCCESS: Configuration for Service "W3SVC" changed!

You can use the application to call your own recovery program (even a batch) in case of a failure:

C:\> MySQLServiceSetCorrectiveActions.exe MySQL "CMD.EXE /C C:\mybatch.bat"
SUCCESS: Configuration for Service "MySQL" changed!

And you can also clear the corrective action by passing an empty string as the second parameter:

C:\> MySQLServiceSetCorrectiveActions.exe MySQL ""
SUCCESS: Configuration for Service "MySQL" changed!

Points of interest

There is no such thing as bug-free code - Windows Services are no exception. Since most Services provide vital functionality to an application, they must be up and running 24/7. This lack of auto-restart feature in Services was a severe deficiency in Windows (compared to UNIX where inetd - among others - has been there for ages to automatically start/restart daemon processes). Now it's over - but is not so popular or well-known. Let's make tools integrate the right system calls into all services - why not directly into all installation programs for services?

History

  • V1.0: May 3rd, 2005.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Emmanuel Kartmann


Fell into computer software at the age of 11, founder of 3 startups, and now manager of an independent software vendor (ISV) labelled proSDK (www.prosdk.com)... And still a freeware writer and technical article author!

Occupation: Web Developer
Location: France France

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralWhy not just let Windows handle this?memberJeremy Falcon8:23 17 Apr '08  
GeneralAutomatic database connection when SQL Server restarted in VCmember1:39 8 Mar '07  
QuestionProblems with STARTing a servicememberHashamay12:14 21 Aug '06  
GeneralGreatmemberGUYFERD_223:06 4 Feb '06  
GeneralRe: GreatmemberEmmanuel Kartmann0:43 5 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 May 2005
Editor: Rinish Biju
Copyright 2005 by Emmanuel Kartmann
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project