start mysql service if it is down programatically - mysql

I have a ubuntu server and i want to monitor it and start mysql service if it goes down, what can i do programatically to implement this.
I see this solution and set it via cron to execute every minute.
#!/bin/bash
if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]
then
/usr/sbin/service mysql start
fi
but would setting a cron to execute every minute load my server.
Any help in this direction will be appreciated. Thanks.

For the most part, running a short script like that every minute won't stress your server. So programmatically, assuming you tested it and all, I don't see much wrong with your approach.
I'd be more concerned as to why your service randomly shuts down though, have you looked for clues in logs? Depending on the severity of the cause, perhaps implementing a monitoring tool like nagios or icinga might be useful.

Related

How would I create a snapshot of a MySQL instance in WSL?

We use MySQL for our app. During dev and testing, we have to constantly reload the MySQL database, sometimes with very large amounts of data. In Linux, I would create an LVM snapshot to do quick rollbacks during testing to reduce turn-around time from 10-15 minutes to 30 seconds.
A number of our devs use Windows Subsystem for Linux (WSL) for their dev environment. It works great and allows them to mix the use of their Windows and Linux tools. Alas, unless things have changed recently, we don't have the full capability of LVM snapshots under WSL.
Are there any scripts we can use to create a MySQL snapshot and quickly restore to that rollback point? I'm aware we can manually review snapshots with mysqlbinlog, but that's rather tedious. I'd love a way to:
run a script start-point.sh
do our testing
profit
run a script rollback.sh
I'm very familiar with filesystem snapshots and so haven't done much with MySQL snapshots to date. The googling results are.. kind of a mess and I'm hoping there is an obvious "oh yeah, just use script ABC at this github link" if possible.
Thanks all.
P.S. If there is a better way of doing this in WSL I am all ears!

How to detect what is running on MySQL every hour?

Munin showing huge spike on MySQL queries every hour but I am unable to detect what is causing this. I am running version 5.6.30.
Tried to enable slow running queries but can't find it there.
Also logged all queries and tried to see what is running on that particular time. I cannot find it.
Checked cronjobs but there wasn't anything related
Disabled almost everything on LFD & CSF
The event scheduler status is set to OFF
Is there any other way to find what is running every hour?
Munin graph showing sql queries:
You can use a shell script and put the instruccion "processlist" and send the output to a log file.
Put the delay in seconds that you want for run again the instucction.
while [true];
mysql -h localhost -u root -ppasswd < process
delay xtime
done
And the file processlist you put the instrucction "show full processlist"
I hope this help you
Regards
Some crawlers were mining data from my website. I wasn't able to detect because requests sent from a million different IPs.
Added captcha to website as human control and spikes gone.

google compute engine tool gcloud is exceptionally slow

I tried downloading and using the gcloud bash tool to manage my accounts, however everything I do with the tool is exceptionally slow. It will take MINUTES to reply to a command that is typed.
Is there perhaps a firewall I need to open up on my router or something else to get this to work fast like it's supposed to? For example, the "Installing..." lines in this video https://youtu.be/4y4-xn4Vi04?t=1m21s -- you'll notice they are all complete in the tutorial within a few seconds. This takes over 10 minutes to complete on my machine.
I'm on a newer Macbook Air, and all other internet/etc works really fast. I'm on a decent speed internet connection from AT&T Uverse (30mbps/3mbps). All other browsing is fast and just fine, the only thing in the world I have a problem with is this gcloud tool.
I just ran into a similar issue myself, though not as bad as minute long response times. What helped me was turning off the usage reporting.
Looking at some timings:
$ gcloud config set disable_usage_reporting False
$ time gcloud compute -h | tail -1
real 0m7.058s
user 0m0.464s
sys 0m0.088s
A whopping 7 secs to access the help!
Fortunately, this improves greatly after disabling reporting:
$ gcloud config set disable_usage_reporting True
$ time gcloud compute -h | tail -1
real 0m0.541s
user 0m0.459s
sys 0m0.080s
Much better!
I traced this back to packet filters. When I deleted this rule #5 in my AT&T Uverse modem/router (Motorola NVG589), everything works ok. This was a default setting in the modem which caused all sorts of issues.
Mark Shust's answer hinted to me that something might be going on with IPv6. Turning this support off in my router brought the gcloud times back to a couple of seconds from over 2 minutes for a gcloud compute machine-types list.
Note: I've also had to turn off IPv6 support in apt for a similar reason so this is likely a problem with my router or ISP, not the google cloud sdk.

Is expect code going to through error if i reboot remote machine?

i am not sure what will happen so i am asking this question and also because i didn't tested this.i have a function send command which sends commands to remote machine and it works fine for normal commands but what if it sends command for reboot like below.
sendcommand reload
expect -re "$prompt"
send -- "exit"
expect eof
i mean after reload how would the rest of the script going to excecute or it will thorugh some error or it will work fine? please guide.
It depends on exactly how you ask for the reboot to be done. Rebooting may be done by asking the system to restart, and the time to process that might allow you to exit. Or it might not; there's a race condition. You certainly need to drop the network connection though; when the OS comes back, it won't recognize it and you'll get a forced connection reset (if not before).
Or you could ask it to reboot a couple of seconds in the future (I forget the exact syntax for this) to give yourself time to disconnect. Some individual research and experimentation is likely to be needed; VMs are good for this as they restart much more rapidly…

Tomcat Freezes just one application

I have an Ubuntu LAMJ server running Tomcat6.
One of my JSP applications freezes every couple of days and I am having trouble figuring out why. I have to reboot tomcat to get that one app going again, as it won't cone back on its own. I am getting nothing in my own log4j logs for that app, and can't see anything in Catalina.out either.
This applications shares a javax.sql.DataSource resource with another, via a context element in the server.xml file. I don't think this is the cause of the problem, but I may as well mention it.
Could anyone point me in the right direction to find the cause of this intermittent issue?
thanks in advance,
Christy
Get a Thread dump of the running server
There are two options
Use VisualVM
in your %java_home%/bin folder there will be a file called jvisualvm. Run this and connect to your tomcat server. Click the Threads tab and then "Thread Dump"
Manually from the Command Line
open up a command line and find the process id for your tomcat
ps -ef | grep java
Once you identify the process ID for the running tomcat instance,
kill -3 <pid>
replace the process Id here. This will send your thread dump to the stdout for your tomcat. Most likely catalina.out file.
edit - As per Mark's comments below:
It is normal to take 3 thread dumps ~10s apart and compare them. It
makes it much easier to see which threads are 'stuck' and which ones
are moving
Once you have the thread dump you can analyse it for stuck threads. It may not be stuck threads as the problem, but at least you can see what is going on inside the server to analyze the problem further.