Escaping stripes from MySQL execute command - mysql

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\`"

Related

drop database if exists using mysqladmin in linux

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" ?

Receiving 'ERROR 1046 (3D000) at line 1: No database selected' when executing linux bash script

I'm trying to automate the creation of a mysql user with a database and table using Linux bash, but receiving ERROR 1046.
I've looked at various tutorials online, including this website, and from what I can see, my script looks correct!
If i log in to mysql via the command line and run these commands manually, it works!
But if i run the exact same commands in a bash script, it fails. (name of user/db replaced with $1 variable)
Posting this as a last resort as all the posts i have found referring to this error say to use 'use databasename;' but i already do that in the script.
#Code to create mysql user with db and table in linux bash.
#to run: ./createmysqldb Testuser1 TestPass1
$1 = Testuser1
$2 = TestPass1
...
mysql -u$user -p$password -e "create user '$1'#'%' identified by '$2';"
mysql -u$user -p$password -e "create database $1_DB;"
mysql -u$user -p$password -e "use $1_DB;"
mysql -u$user -p$password -e "CREATE TABLE persons (id int, name varchar(20), surname varchar(20));"
mysql -u$user -p$password -e "GRANT ALL PRIVILEGES ON $1_DB.* TO '$1'#'%';"
mysql -u$user -p$password -e "flush privileges;"
...
Manually tested this code successfully, so i know it works, not sure if the variables and the use $1_DB; is being executed correctly, I expect a new user to be created, with a database containing a persons table.

Executing mutiple MySQL Queries in bash script

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.

unable to connect the mysql database using shell script

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.

How does MySQL take variables in bash?

Making a script to print out data from a MySQL db via bash, I met the following problem:
While I try to log in, it uses the password as the database to log in to.
Script is like this:
#!/bin/bash
echo $1
db=$1
pasx=$2
CMD="use $db; select * from job_log;"
mysql -u sqluser -p "${pasx}" -e "$CMD"
If I'm going to run the script with the command
User#server:/path/with/file$ sh sql.sh ok hobo
MySQL returns the following:
User#server:/path/with/file$ sh sql.sh ok hobo
ok
Enter password: ERROR 1049 (42000): Unknown database 'hobo'
I might have fully misunderstood something, but I can't put my finger on what it might be.
You need to remove the space after the -p parameter. See the mysql man page. You also need to specify the database in the command (remove it from the query)
mysql -u sqluser -p$pasx -e "$CMD" $db
Or maybe more clear:
mysql --user=sqluser --password=$pasx --execute="$CMD" $db
Try this:
mysql -u sqluser --password=${pasx} -e "$CMD" $db