Thursday, March 12, 2009

Using mysql in Batch Mode script

Using mysql in Batch Mode
In the previous sections, you used mysql interactively to enter queries and view the results. You can also run mysql in batch mode. To do this, put the commands you want to run in a file, then tell mysql to read its input from the file:
shell> mysql <> mysql -e "source batch-file"
If you need to specify connection parameters on the command line, the command might look like this:
shell> mysql -h host -u user -p <> mysql <> mysql <> mysql.out
You can distribute your script to other people so that they can also run the commands.
Some situations do not allow for interactive use, for example, when you run a query from a cron job. In this case, you must use batch mode.
The default output format is different (more concise) when you run mysql in batch mode than when you use it interactively. For example, the output of SELECT DISTINCT species FROM pet looks like this when mysql is run interactively:
+---------+
species
+---------+
bird
cat
dog
hamster
snake
+---------+
In batch mode, the output looks like this instead:
species
bird
cat
dog
hamster
snake
If you want to get the interactive output format in batch mode, use mysql -t. To echo to the output the commands that are executed, use mysql -vvv.
You can also use scripts from the mysql prompt by using the source command or \. command:
mysql> source filename;
mysql> \. filename
See Section 4.5.1.4, “Executing SQL Statements from a Text File”, for more information.
Previous / Next / Up / Table of Contents
User Comments
Posted by Yurii Zborovs'kyi on March 6 2003 12:57am [Delete] [Edit]

How to measure total batch running time for several SQLs:

# at start of your script file
SET @start=UNIX_TIMESTAMP();

# great job
...
...
...

# at bottom of your script file
SET
@s=@seconds:=UNIX_TIMESTAMP()-@start,
@d=TRUNCATE(@s/86400,0), @s=MOD(@s,86400),
@h=TRUNCATE(@s/3600,0), @s=MOD(@s,3600),
@m=TRUNCATE(@s/60,0), @s=MOD(@s,60),
@day=IF(@d>0,CONCAT(@d,' day'),''),
@hour=IF(@d+@h>0,CONCAT(IF(@d>0,LPAD(@h,2,'0'),@h),' hour'),''),
@min=IF(@d+@h+@m>0,CONCAT(IF(@d+@h>0,LPAD(@m,2,'0'),@m),' min.'),''),
@sec=CONCAT(IF(@d+@h+@m>0,LPAD(@s,2,'0'),@s),' sec.');

SELECT
CONCAT(@seconds,' sec.') AS seconds,
CONCAT_WS(' ',@day,@hour,@min,@sec) AS elapsed;

# enjoy :)

p.s. Tested & works
p.p.s. No fractions of seconds :(

jz


Posted by Musba - on May 19 2003 11:41pm [Delete] [Edit]

Example of a Korn Shell Script

#!/bin/ksh
mysql --user= --password= -h < /etc/security/mysqlpassword
chmod 200 /etc/security/mysqlpassword

Then in your script:

echo "update tablex set x=1 where a=2;" mysql mydb --user=batchdb --password=`cat /etc/security/mysqlpassword`

This assumes you have created a user called "batchdb" with that password and the correct access rights to the database called "mydb".

tc


Posted by Senthil Nathan on October 29 2004 11:57pm [Delete] [Edit]

When using mysql in batch mode you can use pipes to write an interactive but pre-scripted shell script. Be aware that you need to use the "-n" command line option to flush the buffer otherwise your read will hang. Here is a code sample in ksh:

-------------------------------------------------
#!/bin/ksh
mysql -u username -ppassword -D dbname -ss -n -q &
print -p -- "select count(*) from some_table;"
read -p get_row_count1
print -p -- "select count(*) from some_other_table;"
read -p get_row_count2
print -p exit ;
#
echo $get_row_count1
echo $get_row_count2
#
exit
-------------------------------------------------
(The -q option is optional)

Note: If you dislike using "-n" then make sure all your read statements are after the exit.



Posted by Jamie Jones on November 22 2004 3:33am [Delete] [Edit]

N.B. On most Unix systems, by placing the password in the command line with --password (even the above method for using a password file) you are making the password visible to local users, who can see the command string with a "ps" or "w" command.

Whilst some systems can be set to block this, and others would let you wrap the command in something that would overwrite what users could see as your command, the best way to do any automations like this is to create a specific unix user for the job (or use a user that is already secure) and place the password in the .my.cnf file for that user - making sure the permissions are set so that only the owner can read it


Posted by Frances D on April 29 2006 11:33pm [Delete] [Edit]

For newbies like me: This exact command allowed me to run a script from outside MySQL (using the DOS command line in Win98):

C:\WINDOWS\Desktop>c:\mysql\bin\mysql -u root -p < "c:\mysql\scripts\20060416_ShowInnodbstatusscript.txt" more Note: My text file had two commands: Use db_name; Show InnoDB Status; FYI: I had been plagued by a foreign key error, but was unable figure out how to see the result of my Show Innodb Status command due to my 50-line DOS screen limitation. To get around this problem I a) created the simple script shown above b) created a foreign key error while logged into my MySQL user account, then c) opened an additional DOS window to execute the command shown above. Running the command outside of MySQL essentially creates a way to view command results one page at a time for folks administering MySQL at the command line (using Windows). Posted by WL Seow on November 2 2006 3:36pm [Delete] [Edit] Alternatively, by default, mysql.exe assumes that the script is inside the same folder. cd C:\Program Files\MySQL\MySQL Server 5.0\bin mysql -u root -p <> mysql <> chmod a+x test.sql

and then run `test.sql' as a usual Unix script.
But it doesn`t work.

It is well known that so-called Sha-Bang (#!) symbol
is used in Unix world to specify where actual interpreter
lives. For example, #!/bin/bash

My objective is to create small and convenient wrapper
to run MySQL scripts in the same way as I run other scripts:
just typing `test.sql'.

Here is the program:

=[sql.c]===8<======================================================== /* * MySQL Sha-Bang wrapper. Public domain. * Alexander Simakov */ #include
#include
#include

int main(int argc, char **argv) {
char buf[512];
int i;

buf[0] = 0;

for (i = 1; i < i ="="" i ="=""> gcc -o sql sql.c

Copy program into the usual location
shell> cp sql /usr/bin/sql

Set execution flag on the script
shell> chmod 755 test.sql

Show script contents
shell> cat test.sql
#!/usr/bin/sql -t

use mysql;
select User,Host from user;

Enjoy!
shell> ./test.sql


+--------+-----------+ User Host +--------+-----------+ root localhost xander localhost +--------+-----------+P.S.
Note that in order to use batch mode efficiently you need
non-interactive authentification. The best way is per-user
configuration file: ~/.my.cnf

Create file and put a couple of strings:
[client]
password = yourpasswd

NB! Don`t forget to set proper permissions!
shell> chmod 400 ~/.my.cnf

Now mysql will use this password as you default password.

P.S.S.
You can also pass any mysql parmeters in the sha-bang line.
For example:
#!/usr/bin/sql -t
or
#!/usr/bin/sql -X
or even
#!/usr/bin/sql -u someuser -ppassword

That`s it!

---
There are 10 kinds of people: those who understand binary and those who don`t.


Posted by Richard Moor on April 30 2008 12:49am [Delete] [Edit]

re:Using mysql in Batch Mode - Windows XP - Vista:
If you need to specify connection parameters on the command line for using a text file use forward slashes
and print the word source, not the url of your file.
Example:mysql> source C:/inetpub/wwwroot/your folder/sql.txt; unfortunately the reference manual does
not allude to this.
rich: comrefhvac.com


Posted by David Goadby on August 6 2008 11:30am [Delete] [Edit]

Batches in Linux are ok but, running batches in Windows via a DOS box (cmd line) causes a lot of problems if other tasks are running.

I have a large file of SQL (1000 lines, 38Kb) that is generated nightly and run as a batch. For about 10 minutes the CPU utilisation is 100% and other programs slow to a crawl.

Sadly we have no nice for cmd.exe so cannot change it. We fixed the problem by using another program to execute the file as a series of SQL statements. As the program is a proper windows program (Delphi) then it yields from time to time and the CPU utilisation drops back. Ok, the SQL takes longer to run, but it run at 2am, so no one is waiting for it.

No comments: