Does crontab leave mysql connections open? - mysql

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.

Related

Is it possible to run a sql query when azure VM starts?

Every day after turning on azure VM I'm executing one SQL query.
ie,
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
and also I have to restart my ngnix service after running this query.
I'm planning to do this using a bash script and will schedule a run for the same bash script using a cronjob.
Is there any other way to do this?
Yes, you can try it with the bash script, that is the best option.
Apart from that, as mentioned by #Anand in comment section, you can try to make Automatically Running Stored Procedures at SQL Server Startup.
Refer this third party tutorial for the same.

What is a mysql disconnect command line (why is it useful)?

I am running a mysql database and I connect to it just fine. My question is: whenever I connect to the database (to add new input via php) do I also have to include a disconnect command line?
I ask because my bandwidth usage is growing faster than I expected so I am happy thinking that I am getting traffic, but perhaps it is growing because I connect and do not "disconnect"?
From the mysql docs
mysql is a simple SQL shell with input line editing capabilities. It
supports interactive and noninteractive use.
The fact is that the SQL shell should not be causing major load on your box. The standard practice is to just close the shell and kill the program.
Typing Control+C causes mysql to attempt to kill the current
statement. If this cannot be done, or Control+C is typed again before
the statement is killed, mysql exits
When you exit mysql command line tool the process will end and mysql will continue doing its thing. But the answer to your question is no SQL shell should not be slowing things.
From PHP its a good idea to close the connection when you are done using it. To check out what processes are running open up mysql cmd tool and try the following to see what is connected to your mysql instance.
SHOW PROCESSLIST
if showprocesslist isnt what you were looking for give this a shot:
mysql > show status like '%onn%';
Hopefully this will give you enough information to handle the traffic load.
devzone.zend.com :
"Open connections (and similar resources) are automatically destroyed at the end of script execution. However, you should still close or free all connections, result sets and statement handles as soon as they are no longer required. This will help return resources to PHP and MySQL faster."
My advice:
It is a good practise to close a connection after doing the queries you wanted.

How to execute mysql scripts in cloudfoundry

I have a .sql file (initial sql scripts). I have recently deployed application in cloudfoundry, So I want to run these scripts to make application work, Scripts will update more than 5 db tables.
Is there any other way to run the mysql scripts from the grails application on start up Or Is there any provision to run the scripts in the cloudfoundry.
you have several options here.
The first one (which I recommend), is to use something like http://liquibase.org/ (there is a Grails plugin for it: http://grails.org/plugin/liquibase). This tool will make sure that any script you give it will run prior to the app starting, without running the same script twice, etc. This is great to keep track of your database changes.
This works independently of CloudFoundry and would help anyone installing your app having an up to date schema
The second option would be to tunnel to the CloudFoundry database and run the script to the db. Have a look at http://docs.cloudfoundry.com/tools/vmc/caldecott.html or even easier with STS : http://blog.cloudfoundry.com/2012/07/31/cloud-foundry-integration-for-eclipse-now-supports-tunneling-to-services/
Yup, what ebottard said! :-) Although, personally I would opt for using the tunnel feature on VMC, but that said, I am a Ruby guy!
Be weary of the fact that there are timeouts against queries in MySQL if are bootstrapping your database with large datasets!

MySQL Instrumentation using mysql_proxy

I want a simple way to see what commands are being sent to MySQL. I have several MySQL projects that sometimes have a few messy layers of code. I want something like SQL Server Profiler without all of the bells and whistles. I just need to see the SQL traffic. Not analyze which queries are executed most often.
I found MySQL Proxy and can't get it to work in Windows. I downloaded their binaries, and tried their first example LUA script from the link. It loads fine, but when I try to connect to port 4040 using mysql, I get:
ERROR 1105 (HY000): #07000MySQL Proxy Lua script failed to load. Check the error log.
What error log? I didn't even give it the credentials to connect to my real SQL Server. What can I do to get this to work? I'm open to other options (hopefully not sniffing traffic, but maybe if someone can make it easy).
Enable query logging in my.ini. this will write all queries to a log file
add the line
log = [querylog_filename]
Then restart the mysql service
using a program such as tail for win http://tailforwin32.sourceforge.net/ will allow you to watch the queries as they run.
if you have a problematic query you can enable slow logging as well. which will log important details about queries that take a long time to run
DC

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

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.