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.
Related
So I need to read in a bunch of ID numbers from a file and do a mysql query on each of them. I started off with this:
#!/bin/bash
file="eidlist.txt"
while IFS= read -r line
do
mysql --host <redacted> --user <redacted> --password=<redacted> -N -e "use netops;select m_mailname from footprints where m_empno=$line;"
done <"$file"
This works and produces the expected output. Now I need to do the same thing but with a different list of IDs, querying a different field. Since the field I'm now querying on contains alphanumeric values (whereas the previous one was entirely numberic), I need to surround the value in quotes in the mysql query string, which I escape with \" like so:
#!/bin/bash
file="pidlist0.txt"
while IFS= read -r line
do
mysql --host <redacted> --user <redacted> --password=<redacted> -N -e "use netops;select m_mailname from footprints where m_stuid=\"$line\";"
done <"$file"
This doesn't work - the script produces no output. What am I doing wrong?
When I test the mysql command at the command line (with the $line variable pre-populated to one of the values from the file), it works, but when run from inside the script it produces no output. What's going on?
Just use m_stuid='$line';" ? Or change the double to single.
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.
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 %?
I have a sql file which contains over a million insert statements. The official tool for MySQL administration is not able to open it because of its size. Is it possible to insert records using a BASH script?
I tried this so far but it doesn't work.
while read line
do
mysql -u root --password=root << eof
use dip;
$line;
eof
done < $1
mysql -u root --password=root <mysqlfile.sql
Try this:
while read line
do
mysql -u root --password=root -c "$line"
done < $1
Notes:
If the sql contains double quotes ("), yo'll have to escape them
If the SQL statements go over multiple lines, you'll have to figure that out
The advantage of this method is each line gets its own transaction, whereas if you fire the whole file in, it could blow the logs being such a large change set
I'm trying to execute this:
$ mysql --user=XXX --password=XXX --batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
split -l 50 -a 5 - "result."
but bash complains about ) 'unexpected' character (i have this character and few other 'weird ones' in my mysql password). I tried to take my password in quotes --password="myweirdpasshere" but then mysql won't login me at all (probably password is incorrect?)
Some characters such as $, `, ", and sometimes \ retain their special meaning inside double quotes. If your password contains any of those characters, use single quotes instead. (Unless your password also contains single quotes, in which case you might just drop the quotes altogether and put a \ before each special character.)
You should be able to put the password in the $HOME/.my.cnf file, avoiding this issue as well as increasing the security.
Another option to using $HOME/.my.cnf is embedding the configuration file into the script. A comment on the documentation gives examples on this. For your case it should be:
$ mysql --user=XXX --defaults-file <(printf '[client]\npassword=XXX\n') \
--batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
split -l 50 -a 5 - "result."
If you don't give the password on the command line, it will bring up an interactive prompt. That might solve your error. Just use -p instead of --password=XXX.
Of course, if you require unattended access to the script, that's not a very useful answer. It would be more helpful if you could create a minimal example and tell us the exact error that Bash gives you.