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()
Related
I'm baffled as to where I'm going wrong with the following mysql command in my bash script to get a SELECT statement out in HTML format.
#!/bin/bash
dbhost="localhost"
dbname="unicentaopos"
dbuser="user"
dbpass="pass"
mysql -h $dbhost -u $dbuser -p $dbpass -H -e "SELECT NOW();" $dbname > /home/imperial/.scripts/test.txt
Everything is identical to my script, except for the credentials for obvious reasons. I have also tried hard coding the vars into the command - same results.
I cannot see anything wrong with what I've done, but the text file produced just contains mysql usage options:
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper
Copyright (c) 2000, 2019, 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 -?
etc etc etc etc.....
Can anyone tell me what's wrong?
is the dbhost variable accessed correctly?
#dbhost -> $dbhost
I am trying to backup a mysql database using mysqldump command, but the message I get is mysqldump: option '--tables' cannot take an argument.
Here you are:
root#myhost:~# mysqldump mydatabase --user myuser --password mypassword
Warning: Using unique option prefix table instead of tables is deprecated and will be removed in a future release. Please use the full name instead.
mysqldump: option '--tables' cannot take an argument
I have tryed several argument combinations, but I finally discovered that the result is the same if I just try to get the command version or event with no arguments at all:
root#myhost:~# mysqldump --version
Warning: Using unique option prefix table instead of tables is deprecated and will be removed in a future release. Please use the full name instead.
mysqldump: option '--tables' cannot take an argument
root#myhost:~# mysqldump
Warning: Using unique option prefix table instead of tables is deprecated and will be removed in a future release. Please use the full name instead.
mysqldump: option '--tables' cannot take an argument
As you can see in the following lines, it is mysql server 5.5 running on debian 7.
System version:
root#myhost:~# uname -a
Linux myhost 3.2.0-4-amd64 #1 SMP Debian 3.2.41-2+deb7u2 x86_64 GNU/Linux
mysql client version:
root#myhost:~# mysql --version
mysql Ver 14.14 Distrib 5.5.35, for debian-linux-gnu (x86_64) using readline 6.2
mysql server version:
root#myhost:~# mysql -h localhost --user=myuser --password=mypassword mydatabase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 75
Server version: 5.5.35-0+wheezy1-log (Debian)
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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
I have looked for this problem on the web, but I cannot see anyone reporting this precise issue. I am not an expert on mysql but I can say it is a very simple install. Should you need more information, please tell me.
Thanks in advance,
ivan
Following #DCoder indications I inspected /etc/mysql/my.cnf which, among others, contained
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
table = true
After removing table = true line from /etc/mysql/my.cnf, mysqldump command works as expected:
root#myhost:~# mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
My conclusion is that table=true option is not suitable for mysqldump command and must be removed from [client] in the options file. [client] section groups option settings applied to all client programs.
Should another command need that option set, it should be placed in another program section, neither in [mysqldump] nor in [client].
Try this
mysqldump --tab = dir_name options db_name tbl_name
--tab writes each dumped file as a tab-delimited text file in the "dir_name" directory.
db_name is the db containing the table to the exported.
tbl_name is the table to be exported.
"options" part may include options such as --host or --user.
e.g.
mysqldump --tab = /tmp office contact
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');"
the command:
ssh xxx.xxx.xxx.xxx \"mysql -u root -password \\\"grant all privileges on rcf_275d315.* to rfc_user#localhost identified by \'W27j453frxrff23\'\\\"\"
gives me an error:
bash: mysql -u root -p97yf2beiru3trf289 "grant all privileges on rcf_275d315.* to rfc_user#localhost identified by 'W27j453frxrff23'": command not found
when i copy the string returned by bash, end run locally it works. Also works, when pasted on the remote serve.
For some reason it doesn't work over ssh, and returns error:
mysql -u root -p97yf2beiru3trf289 "grant all privileges on rcf_275d315.* to rfc_user#localhost identified by 'W27j453frxrff23'": command not found
UPDATE:
i tried few variations, without success:
ssh xxx.xxx.xxx.xxx \"mysql -u root -pBOY8o7ubio87gubip7 \\\"grant all privileges on rfc_275d315.* to rfc_user identified by \'KUG34dY976fyvc768g\'\\\"\"
result:
bash: mysql -u root -pBOY8o7ubio87gubip7 "grant all privileges on rfc_275d315.* to rfc_user identified by 'KUG34dY976fyvc768g'": command not found
ssh xxx.xxx.xxx.xxx \"mysql -u root -pBOY8o7ubio87gubip7 \\"grant all privileges on rfc_275d315.* to rfc_user identified by \'KUG34dY976fyvc768g\'\\"\"
result:
bash: -c: line 0: unexpected EOF while looking for matching `"'
bash: -c: line 1: syntax error: unexpected end of file
ssh xxx.xxx.xxx.xxx mysql -u root -pBOY8o7ubio87gubip7 \\"grant all privileges on rfc_275d315.* to rfc_user identified by \'KUG34dY976fyvc768g\'\\"
result:
mysql Ver 14.14 Distrib 5.1.66, for redhat-linux-gnu (x86_64) using readline 5.1
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: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
ssh xxx.xxx.xxx.xxx mysql -u root -pBOY8o7ubio87gubip7 \\\"grant all privileges on rfc_275d315.* to rfc_user identified by \'KUG34dY976fyvc768g\'\\\"
result:
mysql Ver 14.14 Distrib 5.1.66, for redhat-linux-gnu (x86_64) using readline 5.1
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: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
still, don't where the problem is. Thanks for any help.
UPDATE:
when echo saved to file:
ssh xxx.xxx.xxx.xxx "echo mysql -u root -piugiu -e \\"grant all privileges on rfc_275d315.* to rfc_user identified by \'in76bn6bgb876n\'\\" > rfc/echo.txt"
i get this in file:
mysql -u root -piugiu -e "grant all privileges on rfc_275d315.* to rfc_user identified by 'in76bn6bgb876n'"
which is proper command, and when copied and pasted to command line on remote server it works, as it should.
when echo is removed:
ssh xxx.xxx.xxx.xxx "mysql -u root -piugiu -e \\"grant all privileges on rfc_275d315.* to rfc_user identified by \'in76bn6bgb876n\'\\" > rfc/echo.txt"
the text file contain info about usage of mysql:
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
ssh xxx.xxx.xxx.xxx 'mysql -u root -pBOY8o7ubio87gubip7 -e "grant all privileges on rfc_275d315.* to rfc_user#localhost identified by '\''KUG34dY976fyvc768g'\''" yourdatabase'
Should be working. You forgot the -e in the MySQL client parameters which allows you to pass a statement
EDIT 1:
fixed quoting
In bash you can differ two ways of quoting (just like in many other languages)
1. Weak quoting: double quotes
e.g. echo "$PATH"
In a weak-quoted string there is no interpretion of
spaces as word-separators
pathname expansion
process substitution
single-quotes to introduce strong-quoting
characters for pattern matching
Otherwise parameter expansion is done:
ls -l "*"
not be interpreted, will pass * literally and will cause an error unless you have a file called *
echo "Your PATH is: $PATH"
Will work as expected. $PATH is interpreted
2. Strong quoting: single quotes
Within single quotes you get no interpretation at all. All characters within single quotes are treated as text.
If you have to use the single quote within a single-quoted text, simply escaping isn't enough. You have to concat like this:
QUERY='SELECT * FROM myTable WHERE col1 = ' \' 'value1' \'
source: http://wiki.bash-hackers.org/syntax/quoting
You don't need to quote the command you send to ssh. Remove your outermost layer of quoting around your mysql command and it should work fine.
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.