Please help me out to execute a MySQL Stored procedure in command line, where the procedure contains conditional statements..
$ mysql --user=user_name --password=your_password db_name
mysql> call stored_procedure_name();
or
$ mysql --user=user_name --password=your_password db_name < script.sql
where script.sql contains your sql statement:
call stored_procedure_name();
Or:
mysql --user=your_username --execute="call stored_procedure_name()" db_name
The same as:
mysql ... -e "call stored_procedure_name()" ...
Or if you don't want to create a .sql file:
$ mysql -u your_username --password=your_password db_name <<!!
call stored_procedure_name();
!!
If you have parameters,
call stored_procedure_name(intValue, doubleValue, 'dateValue');
If your stored procedure doesnot take parameters,
call stored_procedure_name();
Related
I need to run a monthly bash script via cron that is related to our company's billing system. This is done with two stored procedures. When I run them via the MySQL console and workbench, they work fine.
I've looked at this article and this is basically the way I do it.
I call via cron, a shell script that looks like this:
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase < /path/to/billing_periods.sql
My text file that has the commands in it looks like this:
call sp_start_billing_period();
call sp_bill_clients();
What happens is that the first query runs, but the second one on the second line, doesn't.
I can make a stored procedure that wraps these two - but I just was hoping to learn why this was happening... Perhaps a mistake I made or a limit in the way you do this..
I also considered doing this (two calls to the MySQL shell):
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_start_billing_period();"
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_bill_clients();"
You could try separating each statement with a semicolon.
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_start_billing_period();call sp_bill_clients();"
If you have your statements in a file you can do:
while read LINE; do mysql -u<username> -p<password> mydatabase -e"$LINE";echo "-----------";done < statements.sql
I think you are only allowed to execute a single statement in your input .sql file, see the mysql documentation (manpage) for -e statement.
· --execute=statement, -e statement
Execute the statement and quit. The default output format is like that produced with --batch.
The -e is implicit. At least when I do different mysql queries I put them in their own script like you already suggested.
Am facing problem to connect the MySQL DB from shell script. Please find the below snippet i have written for connecting the MySQL data base. please suggest on this.
My shell Script:
#!bin/bash
Query="select * from Main"
MySQL -u root -p '!!root!!' -e kpi << EOF
$Query;
EOF
Please check the above code and suggest me how to connect the DB.
I think it should be
-pThePassword
So you should delete the space between -p and the pass. Also you should not use an apostrophe (except it is part of the pass itself. Use a backslash to escape special characters.
Second: *nix systems are case sensitive, please try mysql instead of MySQL
Update
You could also try to type your password into a file and read it with your script
mysql -u root -p`cat /tmp/pass` -e "SHOW DATABASES"
The file /tmp/pass should contain your password without any newline char at the end.
Update 2
Your Script is wrong.
You can either use mysql ... -e SELECT * FROM TABLE or mysql ... << EOF (without -e). You should not mix them.
Don't forget to pass the databasename as a parameter (or with use databasename;) in the sql
Don't forget to add a ; after every sql command, if you have multiple statements
Method One:
mysql -u root -ppassword databasename -e "SELECT * FROM main"
Method Two:
mysql -u root -ppassword databasename << EOF
SELECT * FROM main
EOF
Method Three:
mysql -u root -ppassword << EOF
USE databasename;
SELECT * FROM main;
EOF
mysql --user=root --password=xxxxxx -e "source dbscript.sql"
This should work for Windows and Linux.
If the password content contains a ! (Exclamation mark) you should add a \ (backslash) in front of it.
I am using MySQL: 5.1.69
I have created stored procedures and called them many times. In this case, I appear to have hit a snag related to escape characters in the schema name.
our MySQL database / schema is named like this: www.company.com
Due to this, we must always escape the database name like this: `www.company.com`.table_name
I have created a procedure from within MySQLWorkbench
CREATE PROCEDURE usp_backfill_data
(p_var1 INT(11)
,p_var2 DATETIME)
BEGIN
/*do lots of work here*/
END
And from within MySQL Workbench I am able to call the procedure using:
use `www.company.com`;
CALL usp_backfill_data(5000, '2012-01-01');
Under these conditions the procedure works exactly as expected.
However, when I try to call the procedure from the command line:
%> mysql -uuser -ppassword -Dwww.company.com -e "CALL usp_backfill_data(5000, '2012-01-01');"
or when I try
%> mysql -uuser -ppassword -e "CALL \`www.company.com\`.usp_backfill_data(5000, '2012-01-01');"
or when I log into the mysql command line and use:
mysql> use `www.company.com`;
mysql> CALL usp_backfill_data(5000, '2012-01-01');
I always get the following error:
ERROR 1305 (42000) at line 1: PROCEDURE www.company.com.usp_backfill_data does not exist
I am hoping that there is something super obvious that I'm overlooking here.
Thank you very much for your time
To escape database name in command line use double quotes
$ mysql -uuser -ppassword -D"www.company.com" -e "CALL usp_backfill_data(5000, '2012-01-01');"
^ ^
Other two methods
$ mysql -uuser -ppassword -e "CALL \`www.company.com\`.usp_backfill_data(5000, '2012-01-01');"
and
$ mysql -uuser -ppassword
mysql> USE `www.company.com`;
Database changed
mysql> CALL usp_backfill_data(5000, '2012-01-01');
work for me just fine
Maybe I'm not seeing something here but WHY would this command line work perfectly fine and provide a result set when run on a LINUX command line but when executed from a WINDOWS command line it fails dismally and returns nothing?
mysql -hHOSTNAME -uroot -p --xml -e 'SELECT * FROM db.table' > c:\temp\output.xml
What am I missing here?
Windows command line does not recognize ' as a quote character, so your statement parameter becomes SELECT (truncated at the 1st space).
You must use " instead, as Heena Hussain suggested.
Can you please try this...
C:\>mysql -u <userid> -p<password> -e "SHOW VARIABLES LIKE '%version%'" –-xml
and this...
mysql -u db_user -p db_name --xml -e "SELECT * FROM table_name" > table_name.xml
I use the pylons and sqlalchemy. I constantly update the schema files and delete and recreate the database so that new schema can be made.
Every time I do this by opening the MySql Query Browser and login and delete the database/schema.
How do I delete the MySQL db/schema thorough linux shell commands in Ubuntu Linux?
Try the following command:
mysqladmin -h[hostname/localhost] -u[username] -p[password] drop [database]
In general, you can pass any query to mysql from shell with -e option.
mysql -u username -p -D dbname -e "DROP DATABASE dbname"
If you are tired of typing your password, create a (chmod 600) file ~/.my.cnf, and put in it:
[client]
user = "you"
password = "your-password"
For the sake of conversation:
echo 'DROP DATABASE foo;' | mysql
Another suitable way:
$ mysql -u you -p
<enter password>
>>> DROP DATABASE foo;
No need for mysqladmin:
just use mysql command line
mysql -u [username] -p[password] -e 'drop database db-name;'
This will send the drop command although I wouldn't pass the password this way as it'll be exposed to other users of the system via ps aux
MySQL has discontinued drop database command from mysql client shell. Need to use mysqladmin to drop a database.
You can remove database directly as:
$ mysqladmin -h [host] -u [user] -p drop [database_name]
[Enter Password]
Do you really want to drop the 'hairfree' database [y/N]: y
[root#host]# mysqladmin -u root -p drop [DB]
Enter password:******