Bash init.d script detect that mysqld has started and is running - mysql

I'm working on my dedicated server running CentOS. I found out that one of my applications which starts up via a script in /etc/init.d/ requires MySQL to be running, or else it throws an error, so essentially I currently have to start it by hand.
How can I detect, in a bash script (#!/bin/sh), whether the MySQL service has started yet? Is there some way to poll port 3306 until it is open to accept connections, and only then continue with the script? Or maybe set an order so that the script doesn't run until the mysqld script runs?

The init scripts allow you to set the order with the numbering scheme.
Example, in /etc/rc.d/rc5.d you might see:
S98first
S99second
S99second runs after S98first due to the naming.

Related

running scripts on ssh remotely

I have a python script and a file (both on ssh) to be inserted to a db (mysql) using the python script. My file has 53,000,000 rows to be submitted to the db. This submission takes a very long time. Is there anyway in which I can run the script so that even if I exit ssh the script continues running?
Thanks,
You can use the screen or tmux. Both commands allow you to have detached user session. You can reconnect at any time to this session.
You only need to start screen and run your scripts there. After that you can push ctrl+a and than d and your session will be detached.
To restore your session you need to run screen -a.
Why dont you have it handled in a jenkins pipline, setup a job in jenkins to run the script.

Does crontab leave mysql connections open?

When I run report jobs (get net sales data from mysql) in crontab (bashscript) , I establish a connection to mysql and get the required data.
Does this leave mysql connections open or all the connections are closed on job completion?
UPDATE - I use bash script to connect to mysql.
In basic scenarios the MySQL connections will be closed when the mysql client within the bash script finishes running.
If you are running the mysql command line Linux client within your bash script then bash would normally wait for the mysql process to exit before continuing to the end of the script.
There are ways to persist processes beyond the life of the bash script but you haven't mentioned using those in your question.
If you are using a mysql library that has a close function - most of the MySQL libraries have this in the api then you should use it. Although the default process behaviour will probably clean up open connections for you, it helps to get in the habit of closing resources that you are not going to require again within your code as this makes it more scalable and also informs other developers of your intended behaviour.

How to minimise downtime and user facing errors while restarting Mysql Server and Rails servers on Cloud66?

What are the best practises for restarting mysql and rails servers on Cloud66, to minimise the possibilities of user's seeing errors?
Background
Sometimes logging into a server via ssh, there is a message system restart required
Sometimes there are security upgrades that require a restart.
Current Approach
My current approach for mysql is to:
Put the app into maintenance mode
use bluepill commands to stop the mysql
restart server with "shutdown -r now" command
After server has booted up, confirm that bluepill mysql service is running
put the app back into non-maintenance mode
My current approach for rails servers is to:
use bluepill commands to stop the web service (this is due to the fact that I have multiple web servers so the app can keep on running using alternative web servers)
restart server with "shutdown -r now" command
After server has booted up, confirm that bluepill web service is running
How do you do it?
Your approach sounds very reasonable. In most cases, a restart is required when an update to the Linux kernel has been installed, which usually contains security updates and only come into effect after a reboot. So restarting is good practice, you just need to determine the best time for doing this.
To determine what's being updated (and as such the urgency of reboot), you could always run cat /var/run/reboot-required.pkgs. Apart from being notified of a required restart when you SSH into the server, you can also check for the existence of the reboot-require file by running ls /var/run/reboot-required.
To add to your approach, you may also want to stop any background tasks that will be connecting to your database when you restart it.

How To start multiple mySQL instances on boot

I currently have 3 mySQL instances on my Linux Centos 64 bit server. When the server boots up it only starts the first mySQL instance. At that point I have to "mysqld_multi stop" then "mysqld_multi start" to ensure all 3 are started. Is there anyway Linux can start all 3 up at run time so I don't have to do this every time I reboot the server.
you can use crontab #reboot option:
http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
After researching this a little bit, it looks like you don't have too many options and they aren't as elegant as I would like.
Creating a separate mysql server instance
Running Multiple MySQL 5.6 Instances on one server in CentOS 6/RHEL 6/Fedora
This link explains pretty well how to create another MySQL server instance that starts at boot time. You could definitely do things better than he describes, but this is a start. Essentially he copies the /etc/init.d/mysqld startup script and the /etc/my.cnf configuration file and has the new startup script reference the new configuration file.
Creating a unified startup script
You could also chkconfig mysqld off to not use mysql's built-in startup script and create your own that runs your mysqld_multi commands at boot time.
Let me know if you are looking for more information.

Restart mysql after ending process

I ran an sql via the mysql command prompt, but it was taking too long so I decided to stop it. I closed the window, but I could see in Task Manager that mysqld.exe was still using 50% of my CPU. I therefore ended that process via the task manager. But now I don't know how to reconnect to MySQL. If I try to start a mysql command prompt again, I'm told that it can't connnect. I also tried "net start MySQL" but it says that the service name is invalid. Last time, I ended up restarting my computer.
I see two options: If you are using MySQL as a service then you must have killed the service. You should go to Start Menu -> Run... and then execute services.msc. Look for MySQL service there and start it.
If you are not using MySQL as a service then you'll have to look for the executable and execute it again.
Note: If you got it working after a restart then I bet you're running it is as a service set to start automatically.