Below is my sample code:

KEY=$(openssl rand 16)
echo $KEY > $KEY_FILE
source .mysqlCredentials.txt
MySqlCommand="mysql -h $IP -u $UserName -p$Password $DBName -e"
$MySqlCommand "INSERT INTO  some_table ( ID , ssl_key ) VALUES (\"$ID\", \"$KEY\") ;"

the error I get is :

+ mysql -h X.X.X.X -u root -ppassword database -e 'INSERT INTO  some_table ( ID , ssl_key ) VALUES ("2345", "ïëC;+¹"ë'\''J£èMÑ") ;'
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ë'J£èMÑ")' at line 1

Here in the generated key, I'm getting single quotes, backticks, double quotes, and spaces ... etc...

how can I overcome this error?


Why don't you try base64 encoding the rand string? Like so: openssl rand -base64 16 That is much easier to handle.

  • no... requirement is strictly to use AES – msp9011 May 13 at 6:05
  • Using a generating algorithm is one thing, storing the output value is another. The Base64 representation of a random number is just as random as the initial number. In practice as per the doc openssl rand generates random bytes, that can also be seen as an N-bit number and stored as such. Keeping the random bytes as text as you do is fraught with peril, the DB could alter them due to some language setting, or reject them because they aren't valid UTF-8. – xenoid May 13 at 6:39
  • Trying to store the variable binary and handling that correctly is a real PITA, especially in languages like Bash. I agree with @xenoid. The base64 thing could really help you here. – Edward May 13 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.