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
Related
I am trying to execute a mysql command inside a bash script but every time I try to execute it fails for some reason. I have tried several ways and none seemed to work(e.g: <<QUERY...;QUERY)
My select is the following but I get an error:
#!/bin/bash
mysql -utesting -pMypass -hlocalhost -D test DB -e "
SELECT value FROM h6_options
where module=cloud
AND `option`=prefix;"
I get the following error.
ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='prefix'' at line 5
Any help is appreciated.
You are using a multi-line SQL statement. this means you have two options:
you can push everything into one single line.
try reformatting your script to use EOF tags instead.
any option I didn't think of that the smart people here can consider...
here's an example:
mysql -u USER -pPASSWORD <<EOF
SQL_QUERY 1
SQL_QUERY 2
SQL_QUERY N
EOF
so for you, I would do this:
mysql -utesting -pMypass -hlocalhost -D test DB <<EOF
SELECT value FROM h6_options
where module=cloud
AND `option`='prefix';
EOF
Note: I don't like using the word EOF....for me, I like the word SQL or QUERY. because EOF means end of file. The code is identical, so you can use the word of choice:
mysql -utesting -pMypass -hlocalhost -D test DB <<QUERY
SELECT `value` FROM `h6_options`
where `module`='cloud'
AND `option`='prefix';
QUERY
Source: https://www.shellhacks.com/mysql-run-query-bash-script-linux-command-line/
Turns out the issue was the backticks. I had to escape them in order to not evaluate the line.
<<QUERY
SELECT value FROM h6_options
WHERE \`option\`="prefix"
AND module="cloud"
QUERY
I am looking for a command to drop a database if exists from mysql. I want to execute the command in terminal and not using mysql query. Using here answer I found that I can drop database from terminal using
mysqladmin -h[hostname/localhost] -u[username] -p[password] drop [database]
How do I do the same thing using if-exists condition?
There is no option for mysqladmin to add "IF EXISTS" to its drop database. It is hard-coded to accept a name of a database only, not any other syntax.
Here is line of code that formats the DROP DATABASE statement in the mysqladmin client:
sprintf(name_buff, "drop database `%.*s`", FN_REFLEN, db);
But you may not need IF EXISTS. Just try to drop the database. If it doesn't exist, the client will print an error, but the effect is the same.
Here's a demo I just ran in my shell:
$ mysqladmin -f drop test2
mysqladmin: DROP DATABASE test2 failed;
error: 'Can't drop database 'test2'; database doesn't exist'
The client will exit with status of 1. If you don't like that (like if you're running in a shell script with set -e in effect), you can suppress the exit status:
mysqladmin -f drop test2 || true
If you don't like to see the error message, you can redirect it:
mysqladmin -f drop test2 2>/dev/null || true
Did you try to use 'e' argument, like :
mysql -u root -p somedb -e "select * from mytable" ?
This should be so simple, but I cannot get it to work.
I am automating a release procedure on our server using bash (sh):
#!/bin/bash
oldDatabase=${1:-}
newDatabase=${2:-}
dbPassword=${3:-}
dbuser=${4:-}
if [ ! -z $oldDatabase ] ; then
mysql -u "$dbuser" "-p$dbPassword" -e "CREATE DATABASE IF NOT EXISTS $newDatabase"
mysqldump -u "$dbuser" "-p$dbPassword" $oldDatabase | mysql -u "$dbuser" "-p$dbPassword" $newDatabase;
else
echo "DB file: '$oldDatabase' not found/given!"
fi
This works great for database names like test_something, blabla, etc...
However, the current names of the database have a lot of stripes in it: rel-3-3-0.dev. I cannot get this to work, I keep getting a SQL error on the command. (ERROR 1064 in MySQL)
I guess the stripes in the query are not accepted by the command line MySQL variant, so I added backticks:
mysql -u "$dbuser" "-p$dbPassword" -e "CREATE DATABASE IF NOT EXISTS `$newDatabase`"
But this does not seem to work in either? (ERROR 1064 in MySQL)
Any way to get this working?
Never mind, it seems to work like this:
mysql -u "$dbuser" "-p$dbPassword" -e "CREATE DATABASE IF NOT EXISTS \`$newDatabase\`"
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.
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();