How to make a Bash insert script for MySQL - mysql

I've been trying for two days to make a simple script which feeds an argument (a query) to mysql -e for it to execute. But I can't use mysql -e inside the script for some reason, here are my testings :
I can run the following line in my bash with success :
mysql -u abrouze -p simubase -e "insert into Processing values(1,2,3,'coca cola')"
It might be important to note that I need to pass full strings with whitespaces in the values.
Now, here is my script :
#!/bin/bash
req="\"$1\""
echo $req
mysql -u abrouze -p simubase -e $req
Escaping quotes here so $req is really surrounded by quotes, and it doesn't work.
Trace :
brouze#lasb-ida:~$ ./myrage.sh "insert into Processing values(1,2,3,'huge bear')"
"insert into Processing values(1,2,3,'huge bear')"
mysql Ver 14.14 Distrib 5.5.35, for debian-linux-gnu (x86_64) using readline 6.2
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Following this, the help wall-of-text.
It seems absolutely trivial to me, I absolutely don't know how it would not work...

You can use a heredoc for this, have your script be:
#!/bin/bash
mysql -u abrouze -p simubase << EOF
$1
EOF
Then call your script as before:
./myrage.sh "insert into Processing values(1,2,3,'huge bear');"

Related

shell script issue may be because of quotes

I am working on a shell script and I have to execute a MySQL query from one server to a remote DB Server. I have written this script. Script was working fine until I added
where socialr_host = "http://$NEW/"
I think this is an issue of "" inside ''. Please help me with this, I don't have much knowledge to shell scripting
SERVER_USER="root"
SERVER_HOST="192.168.0.13"
MYSQL_HOST="localhost"
MYSQL_PASS="pass"
MYSQL_USER="root"
CORES_DATABASE="/root/Desktop/cores.db"
CORES_FILESYSTEM="/root/Desktop/cores.disk"
DIFF_FILE="/root/Desktop/diff.txt"
CORES_PATH="/raid/solr/cores"
NEW="db6055.da2"
ssh $SERVER_USER#$SERVER_HOST " mysql -u $MYSQL_USER -p$MYSQL_PASS -e 'select coloumn_name from table_name where socialr_host = "http://$NEW/";' database" > $CORES_DATABASE
You need to escape double quotes within double quotes:
ssh $SERVER_USER#$SERVER_HOST " mysql -u $MYSQL_USER -p$MYSQL_PASS -e 'select coloumn_name from table_name where socialr_host = \"http://$NEW/\";' database" > $CORES_DATABASE
However, there are several other issues with your code, and I would avoid using Bash for something as complicated as this. If you insist on using Bash, please read up on at least quoting and SSH quoting.

Bash-MySQL / try to insert string to table

I try to insert string to mysql in bash, so I do the next:
message="<a href = http://www."
message="$message ${d}"
message="$message .com"
mysql -u root -pmypass -Bse 'INSERT INTO atTable VALUES (null, "'$message'")'
When I do it, I get the next massage:
mysql Ver 14.14 Distrib 5.1.69, for debian-linux-gnu (i486) using readline 6.1
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
-A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to get
table and field completion. This gives a quicker start of
mysql and disables rehashing on reconnect.
-B, --batch Don't use history file. Disable interactive behavior.
(Enables --silent.)
--character-sets-dir=name
Directory for character set files.
and other commands. What I do wrong?
Please have a try with this one:
message="<a href = http://www."
message="$message ${d}"
message="$message .com"
mysql -u root -pmypass -Bse "INSERT INTO atTable VALUES (null, '$message')";
At least it worked for me, when I tested it with this:
message="<a href = http://www."
message="$message hello"
message="$message .com"
mysql -u root -pwhatever -Bse "SELECT '$message'";
Try this:
mysql -u root -pmypass -Bse "INSERT INTO atTable VALUES (null, '$message')"
The problem was the spaces in $message were ending the -e option.
Instead of piecing together the message variable like you did, this is easier to read:
message="<a href = http://www. $d .com"
This is equivalent to the example in the original post, though the text itself doesn't look meaningful.
You can pass your query to mysql like this:
mysql -u root -pmypass -Bse "INSERT INTO atTable VALUES (null, '$message')"
If message contains single quotes, you need to escape them, you can do like this:
message=$(echo "$message" | sed -e "s/'/\\\\'/")
Instead of putting your root password on the command line, I recommend to put that information in the .my.cnf file of your home directory, for example:
[client]
database=yourdbname
user=root
password=yourpass
However, before entering the real password, protect the file first like this:
touch .my.cnf
chmod 600 .my.cnf

Unix : Can't use backquote in MySQL command

I wrote a MySQL command in bash (Ubuntu) :
[XXXX:~]$ mysql -h localhost -u XXXX -pXXXX -e "DROP DATABASE IF EXISTS `f-XXXX`;"
I need backquote in this command, cause database name is variable.
That command doesn't work and it sends f-XXXX command not found
I think my problem is related to backquotes. How can I do?
You need not use backtick for variable substitution here.
[XXXX:~]$ mysql -h localhost -u XXXX -pXXXX -e "DROP DATABASE IF EXISTS ${DB};""
Bash takes the content of the backtick and runs another bash process with that as a command.
This is a backtick. Backtick is not a quotation sign, it has a very special meaning. Everything you type between backticks is evaluated (executed) by the shell before the main command (like chown in your examples), and the output of that execution is used by that command, just as if you'd type that output at that place in the command line.
Use $(commands) instead.
mysql -h localhost -u XXXX -pXXXX -e "DROP DATABASE IF EXISTS $('f-XXXX');"

Bash scripting for MySQL in

good day all,
please i am new to bash scripting and i am having some challenges with my bash script for connecting to a mysql server to reteive information. this is my script:
#! /bin/bash
MYSQL_USER="root"
MYSQL_PASSWORD="bibson13"
MYSQL_DATABASE="fredhosting"
MYSQL="/usr/bin/mysql –u$MYSQL_USER –p$MYSQL_PASSWORD -D$MYSQL_DATABASE"
$MYSQL -ss -e "select name,email,username,password from free_users where id_user=( select max(id_user) from free_users )"
but when i try to run it i keep getting this:
/usr/bin/mysql Ver 14.14 Distrib 5.5.28, for debian-linux-gnu (i686) using readline 6.2
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: /usr/bin/mysql [OPTIONS] [database]
please i really don't know what am doing wrong. i would be most grateful if anyone could help me with some information on what to do, thank you.
The main problem is that you have some broken characters. Specifically, where you meant to type -u and -p (with hyphens), you instead have –u and –p (with en-dashes). This probably results from copying-and-pasting via a word-processor such as Microsoft Word (which is never a good idea). Once you change those en-dashes back to hyphens, you should be O.K.
You have 2 invalid characters into your MYSQL variable (E2 80) before each "-" sign.
Try to cleanup the line.

Grails MySQL import dump

I'm trying to write a function that imports the mysql dump into the database. My code looks like this:
def sout = new StringBuffer()
def serr = new StringBuffer()
String pathToDump = "C:\\Users\\Lojza\\AppData\\Local\\Temp\\protetika-dump-temp-13393134598714336471323836046295.sql"
def process = "mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 < \"${pathToDump}\" ".execute()
process.consumeProcessOutput(sout, serr)
process.waitForOrKill(10000)
println 'sout: ' + sout // => test text
println 'serr: ' + serr
I tried this code in Grails console, but it returns me this:
mysql Ver 14.14 Distrib 5.5.21, for Win64 (x86)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
...... (and many more lines)
I can't figure out what's wrong with the code above... When I try to execute the generated process line in cmd, it works and did the importing to database. The command I executed looks like this:
mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 < "C:\Users\Lojza\AppData\Local\Temp\protetika-dump-temp-13393134598714336471323836046295.sql"
Thanks for any help!
Regards,
Lojza
Redirection does not work the same way under Java as it does with the Windows command prompt. A way to get around this is to execute the source command with the -e option to read a file. So from the command line you could do
mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 –e “source C:\Users\Lojza\AppData\Local\Temp\protetika-dump-temp-13393134598714336471323836046295.sql"
Code to do this from groovy (untested),
def process = "mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 -e \"source "${pathToDump}\"\" ".execute()