`<` character in exec task is breaking my Phing build script - mysql

I'm trying to setup an mysql import script in Phing, but the "<" is causing XML errors. Any ideas?
<exec command="mysql -u${mysql.username} -p${mysql.password} -h ${mysql.server} ${mysql.database} < ${sql.file}" />
I'm looking at making dbDeploy doing this, but it'd be great if there was an easy way for this to work (single line command versus the multi-line setup of dbDeploy)

Escape the character by replacing it with <

I'm not familiar with Phing, but it may not be executing the command in a shell, so the redirect operator won't work (it may be treating the operator as an argument to mysql itself).
Instead of < ${sql.file}, try -e 'source ${sql.file}', so your whole line is:
<exec command="mysql -u${mysql.username} -p${mysql.password} -h ${mysql.server} ${mysql.database} -e 'source ${sql.file}'" />
See the MySQL batch mode documentation.

Related

How to import/load/run mysql file using golang?

I’m trying to run/load sql file into mysql database using this golang statement but this is not working:
exec.Command("mysql", "-u", "{username}", "-p{db password}", "{db name}", "<", file abs path )
But when i use following command in windows command prompt it’s working perfect.
mysql -u {username} -p{db password} {db name} < {file abs path}
So what is the problem?
As others have answered, you can't use the < redirection operator because exec doesn't use the shell.
But you don't have to redirect input to read an SQL file. You can pass arguments to the MySQL client to use its source command.
exec.Command("mysql", "-u", "{username}", "-p{db password}", "{db name}",
"-e", "source {file abs path}" )
The source command is a builtin of the MySQL client. See https://dev.mysql.com/doc/refman/5.7/en/mysql-commands.html
Go's exec.Command runs the first argument as a program with the rest of the arguments as parameters. The '<' is interpreted as a literal argument.
e.g. exec.Command("cat", "<", "abc") is the following command in bash: cat \< abc.
To do what you want you have got two options.
Run (ba)sh and the command as argument: exec.Command("bash", "-c", "mysql ... < full/path")
Pipe the content of the file in manually. See https://stackoverflow.com/a/36383984/8751302 for details.
The problem with the bash version is that is not portable between different operating systems. It won't work on Windows.
Go's os.exec package does not use the shell and does not support redirection:
Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells.
You can call the shell explicitly to pass arguments to it:
cmd := exec.Command("/bin/sh", yourBashCommand)
Depending on what you're doing, it may be helpful to write a short bash script and call it from Go.

SQLMap realy slow on local network

i'am currently trying to use SQLMap on an apparently easy injection on a local web server :
SELECT * from table WHERE `col` LIKE 'VULN_HERE';
I'am using the following command :
sqlmap -u http://localhost/?i=1 --dbms mysql --level 5 --risk 3 -p i --dbs -v 2 --technique 'T'
When running this command, sqlmap identify the injection correctly but is blocking at :
[14:36:43] [INFO] checking if the injection point on GET parameter 'i' is a false positive
What is wrong ?
I think your URL shall be quoted :
sqlmap -u "http://localhost/?i=1" ....
Hi check your syntax and have a look:
SQLmap
You need your URL to be within quotes always or the command prompt will take i=1 outside the URL and as a different parameter.
Hope it'll solve your issues.

SQLCMD scripting error

i am using SQLCMD to run a .sql file which is 270 MB. The script file (.sql) was generated using Red Gate SQL Data Compare synchronization wizard. I cannot run it from SSMS because of insufficient memory. I log into the server and go to command prompt and it opens up command prompt
C:\Users\USER1>
then i type in
> C:\Users\USER1>SQLCMD -U sa -P PWD -d DATA_FEAT -i F:\SYNC\DATA-DATA_FEAT-20140709.sql -o F:\SYNC\DATA-DATA_FEAT-20140709result.txt
but i get
Sqlcmd: Error: Scripting error.
i am able to use Red gate to synchronize it without error. Red gate runs the same .sql file
Any Help
Thanks
I ran into this with a big script doing a lot of inserts. The solution was over in this other answer: Insert a GO periodically in the file so that everything wasn't being built up in one massive transaction. That answer even got the info from...a RedGate forum thread.
Since I'm using Linux and my file was one-statement-per-line, it was quite easy for me to use sed as outlined in this answer to add GO every few lines, e.g.:
$ sed ': loop; n; n; n; n; n; n; n; n; n; a GO
n; b loop' < bigfile.sql > bigfile2.sql
That inserts a GO every 10 lines (the number of times n appears in the sed script), which is probably overkill.
I had the same problem with an SQL file with 2.2M inserts.
Adding GO every few lines as suggested above didn't solve it for me I ended up having to split the file into smaller ones of xxx lines and running them seperately using the bash split command as follows: -
split -l xxx largefile.txt newfile.txt
I also have a nicer syntax for the sed solution listed above
sed '50~50i\GO' largefile.txt > newfile.txt
This will insert GO every 50 lines in a file and output the result to a new file.
Hope it helps someone.

Bad For Loop Variable in bash with sql server

I've been using a program of mine written in bash to interact with a mysql database, I switched to microsoft sql server and now I have a very strange issue. The code below worked with mysql. With microsoft sql server I can see that it successfully pulls the count. My "echo $id" shows a value of 23 like it should but the issue is bash spits out " Syntax error: Bad for loop variable". I'm confused at why it's doing that 23 is an integer value. Please Help.
id="`tsql -S Server\\SqlServerName -U Databas_Name -P Password -o q <<EOF
use numbers
go
SELECT COUNT(*) FROM lotsa_numbers
go
quit
EOF`"
echo $id
for (( c=0; c=>$id-1; c++ ))
do
echo $c
done
Issue was likely leading or trailing whitespace. Number of ways to deal with this, a simple one is using bash splitting by not quoting a variable (might cause a problem in some cases, but not if we're trying to get an integer)
id=$(echo $id)

What is the equivalent of the spool command in MySQL?

I know you use the spool command when you are trying to write a report to a file in Oracle SQLplus.
What is the equivalent command in MySQL?
This is my code:
set termout off
spool ${DB_ADMIN_HOME}/data/Datareport.log # ${DB_ADMIN_HOME}/Scripts.Datavalidation/Datareportscript.sql
spool off
exit
How can I write it in MySQL?
In MySQL you need to use the commands tee & notee:
tee data.txt;
//SQL sentences...
notee;
teedata.txt == spooldata.txt
notee == spool off
For the Oracle SQLPlus spool command, there is no equivalent in the mysql command line client.
To get output from the mysql command line client saved to a file, you can have the operating system redirect the output to a file, rather than to the display.
In Unix, you use the > symbol on the command line. (It seems a bit redundant here to give an example of how to redirect output.)
date > /tmp/foo.txt
That > symbol is basically telling the shell to take what is written to the STDOUT handle and redirect that to the named file (overwriting the file if it exists) if you have privileges.
Q: is set pagesize and set linesize used in mysql when you are trying to generate a report?
A: No. Those are specific to Oracle SQLPlus. I don't know of any equivalent functionality in the mysql command line client. The mysql command line client has some powerful features when its run in interactive mode (e.g. pager and tee), but in non-interactive mode, it's an inadequate replacement for SQLPlus.
If I get what you are asking:
mysql dbname < ${DB_ADMIN_HOME}/Scripts.Datavalidation/Datareportscript.sql \
> ${DB_ADMIN_HOME}/data/Datareport.log
Use redirection.