I tried to execute the following mysql command in one of my scripts:
mysql -e 'show global status like 'open_files''
But it doesn't seem to work, because of the single quotes around the string 'open_files'.
How can I issue a command like this, that contains a single quote?
Use double quote outside.
mysql -h127.0.0.1 -uxxxxx -pxxxxx -A databasename -e "show global status like 'open_files'"
or the reverse way:
mysql -h127.0.0.1 -uxxxxx -pxxxxx -A databasename -e 'show global status like "open_files"'
or you could escape the single quote.
And you are using LIKE, didn't you miss the wild char %?
Related
I am trying to execute an insert statement from linux shell where one of the columns has '$2a$10$zKjqmgld1gDYB/qkDuAS' in the value. When I see the inserted data the value is truncated and I get only 'aKjqmgld1gDYB/qkDuAS' as any digit followed by dollar is treated by linux as a parameter passed to the script.
This is how I am executing the script
mysql -u user --password=password -e "insert into users(id,name,password) values(1,'Some Name','\$2a\$10\$zKjqmgld1gDYB/qkDuAS')"
I have even tried escaping the $ like \$2a\$10\$zKjqmgld1gDYB/qkDuAS, but still it yields the same truncated data , however when I do echo '\$2a\$10\$zKjqmgld1gDYB/qkDuAS', I get the entire thing back.
Sameer
Single quotes don't nest in shell (do they anywhere else, anyway?). So, your string is effectively outside the quotes. Combine with double quotes and backslashes:
mysql -u user --password=password -e \
"insert into users(id,name,password) values(1,'Some Name','\$2a$10\$zKjqmgld1gDYB/qkDuAS')"
it was already in double quotes (sorry, for writing the wrong query). That didn't work either, however I found a workaround
echo "insert into users(id,name,password) values(1,'Some Name','\$2a\$10\$zKjqmgld1gDYB/qkDuAS')" >> temp.sql
mysql -u user --password=password < temp.sql
This finally worked.
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');"
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