Maybe I'm not seeing something here but WHY would this command line work perfectly fine and provide a result set when run on a LINUX command line but when executed from a WINDOWS command line it fails dismally and returns nothing?
mysql -hHOSTNAME -uroot -p --xml -e 'SELECT * FROM db.table' > c:\temp\output.xml
What am I missing here?
Windows command line does not recognize ' as a quote character, so your statement parameter becomes SELECT (truncated at the 1st space).
You must use " instead, as Heena Hussain suggested.
Can you please try this...
C:\>mysql -u <userid> -p<password> -e "SHOW VARIABLES LIKE '%version%'" –-xml
and this...
mysql -u db_user -p db_name --xml -e "SELECT * FROM table_name" > table_name.xml
Related
This is the code I wrote on command line:
mysql --user=root --password=root mydb --default-character-set=utf8 -e "set #arg1='[1002,2003,3304]';source run.sql"
However, it throws me this error:
ERROR at line 1: Unknown command '\U'
I have tested if I remove set statement like this and run:
mysql --user=root --password=root mydb --default-character-set=utf8 -e "source run.sql"
The run script file executes.I know I can open the SQL file and change the source code to void this, but this problem is really annoying.
Could anyone have any suggestions to solve this?
Use ; at the end:
... -e "set #arg1='[1002,2003,3304]';source run.sql;"
or change source by \.:
... -e "set #arg1='[1002,2003,3304]';\. run.sql"
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.
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.
I'm having an issue with a query, when I run it from the command line I have no problems, but from a script it expands the script in a strange way.
My query:
QUERY[1]="SELECT users.username AS \"User name\"FROM mytable WHERE creation_date > '${BEGIN}' AND creation_date < '${END}'"
The call from the script:
$(mysql -hmyhost -uuser -pmypass -Dmydb -B -e "${QUERY[${query}]}")
and I get a:
-bash: User: command not found
Any idea whats going wrong?
Thanks
$(mysql -hmyhost -uuser -pmypass -Dmydb -B -e "${QUERY[${query}]}")
I think you don't need to place it under command substitution:
mysql -hmyhost -uuser -pmypass -Dmydb -B -e "${QUERY[${query}]}"
It causes the output of mysql ... to be executed as well thus showing the error User: command not found.
if you just add a line on a script like this:
$(somecommand)
The output of the command 'somecommand' will be used by the shell as a command.
What you probably want to do is:
output=$(somecommand)
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');"