MySQL shortcuts and icons
Having a SQL server available always comes in handy. There are many implementations, most of which are at least partly freely usable. Previously I've worked with an enterprise installation of Windows Server 2008 R2. For my needs, just a stripped down server will suffice.
At the time I installed MySQL server, MySQL was probably the most popular open source SQL application. Ever since I've stuck with MySQL. The choice doesn't really matter in terms of functionality, it's just what I've grown accustomed to.
With a fresh install of my pc, I had to reinstall MySQL server as well. I always chose for the MySQL Community Server installer, which includes all the MySQL products. This time I wanted a more simple installation, because I realised I didn't really ever use any of the included products.
With just the server installed, I also thought it would be neat to optimise my resource usage. By default the MySQL server starts when Windows starts up. This of course isn't a big resource hog, but I don't like services always starting up at boot when they aren't necessary (Looking at you, Adobe).
Previously I'd start the service in command prompt, log in to the server and start querying. This also felt cumbersome, so I made two shortcut buttons on my desktop, one to start MySQL server, the other to shut the service down again.
Since Windows 10 doesn't allow batch files to be executable by themselves, I stored the batch files in a folder, and made two desktop shortcuts to them.
batch; batchStart.bat
sc query mysql57 | find "RUNNING"
if "%ERRORLEVEL%"=="0"
START "MySQL shell" "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u root
) else (
sc start mysql57
START "MySQL shell" "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u root
)
batch; batchStop.bat
sc query mysql57 | find "RUNNING"
if "%ERRORLEVEL%"=="0" (
sc stop mysql57
) else (
exit
)
The first line uses the sc
command (service controller) to check whether the service is running. Running sc query mysql57
returns a status report on the service:
cmd
C:\>sc query mysql57
SERVICE_NAME: mysql57
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING (STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
This is then piped to the find command. When the word "RUNNING" is found anywhere in the returned text above, the errorlevel %ERRORLEVEL%
will be 0, no error found. From there, depending on whether the service is running one of several actions is executed.
- Open a command prompt window, logged into the MySQL shell
- Start MySQL server and open a command prompt window, logged into the MySQL shell
- Stop MySQL server
- Do nothing
When launching a command prompt window with the start batch file, it looks likes this:
I went through some iterations of starting commands in my script before it really executed how I wanted it to. The main obstacle was the title bar of the command prompt. With a command like START cmd.exe /k "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql" -u root –p
The command prompt window looks like this:
Where the title of the window is 1) very long and 2) although not visible in this example, contains the passed options -u root –p, which isn't desirable.
An improvement is START cmd.exe /k "cd C:\ & "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql"" -u root –p
, which changes the title slightly, but still contains the passed options.
Eventually I wondered why I had cmd.exe /k
in there at all (I still can't remember), and ended up with the command in my example.
The icons that are the shortcuts look rather dull and non-informative.
To improve on them, I searched for a MySQL icon with a transparent background, and two icons to represent start and stop.
I blended the images with a python script, so that the MySQL logo is superimposed on the buttons. It looks rather neat if you'd ask me!
The .convert("RGBA")
in the code below is necessary, because one of the images is a black and white .png image, not a 4 channel RGBA like the others.
python, imageBlend.py
from PIL import Image
startBackground = Image.open('Start-icon.png').convert("RGBA")
stopBackground = Image.open('Stop-icon.png').convert("RGBA")
foreground = Image.open('mysql-black.png').convert("RGBA")
Image.alpha_composite(startBackground,foreground).save('start.ico')
Image.alpha_composite(stopBackground,foreground).save('stop.ico')
Comments
Post a Comment