I would like to make a bash script which will add user to my custom table in MySQL database. I have username as script argument stored in variable $USERNAME and I need to execute this query into MySQL server:
INSERT INTO `user` VALUES ('$USERNAME', password)
I know how to execute a sql file but that won't help me when I have username in variable. The only way I can think of is to execute php file with GET values and execute sql command in php. And I'm not eve sure that with php will work as I think.
Is there any better way?
Thanks
EDIT:
After your helpfull answers I went with this command which works:
mysql -ppassword --default-character-set=utf8 -e "INSERT INTO table VALUES (\"$USERNAME#\",\"password\")" database
You could try using a heredoc:
#!/bin/bash
USERNAME="example"
mysql <<MYSQL
INSERT INTO user VALUES ('$USERNAME', password);
MYSQL
The command would be:
mysql -pyourpasswordwithoutspaces -e "Your insert query goes here with semicolon;"
I'm not sure if the semicolons are necessary or not, but it always good to provide them.
Related
How can I execute multiple SQL queries in the bash script?
I read these two posts from previous years:
A better way to execute multiple MySQL commands using shell script
How to execute a MySQL command from a shell script?
They brought some clarification, but there is still something I do not understand.
I have multiple queries for deleting information about subject with defined subject_id.
Unfortunately I need to run all of them since the table is not in the "cascade" mode.
Is there a way, to create a bash script in which I can use the "user given" variable (by that I mean for example [ read -p 'Subject ID' SUBJECT_ID ]) that will be used inside as the subject_id in each of the queries?
Do I still have to adjust everything to this:
mysql -h "server-name" -u root "password" "database-name" < "filename.sql"
or is there a way to just run this script with connection to db from .cnf file inside it?
There are two questions above. One is how to get a bash variable into your SQL script. I would do this:
read -p 'Subject ID' SUBJECT_ID
mysql -e "SET #subject = '${SUBJECT_ID}'; source filename.sql;"
Bash will expand ${SUBJECT_ID} into the string before it uses it as an argument to the mysql -e command. So the MySQL variable is assigned the string value of SUBJECT_ID.
This will be tricky if SUBJECT_ID may contain literal single-quote characters! So I suggest using Bash syntax for string replacement to make each single-quote in that into two single-quotes:
mysql -e "SET #subject = '${SUBJECT_ID//'/''}'; source filename.sql;"
Note you must put a semicolon at the end after the filename.
The second question is about specifying the host, user, and password. I would recommend putting these into an options file:
[client]
host=server-name
user=root
password=xyzzy
Then when you invoke the mysql client:
mysql --defaults-extra-file myoptions.cnf -e '...'
This is a good idea to avoid putting your plaintext password on the command-line.
Read https://dev.mysql.com/doc/refman/8.0/en/option-files.html for more details on option files.
Is it possible to use shell variable in a mysql query via command line?
What I want to do is :
$ var='test'
$ mysql -e 'INSERT INTO table (text) VALUE ($var);' database
It didn't seems to work, even when I use ${var}
Any idea of how I can do something like that?
I have to stay in the shell, as I am running this through a jenkins 'execute shell'
So, with the help of the comment, I finally manage to do it.
The link explanations of quotes in mysql explain everything.
The solution :
$ var='test'
$ mysql -e "INSERT INTO table (text) VALUE ('$var');" database
I am creating a database in a shell script using
mysql -u$username -p$password -e " create database testdb"
I want to store the result of this statement to make decisions in shell script based on its success or failure.
Even during insert statements i do not get any return value
how can is store the result after executing create database / insert statements in shell script?
use the following command substitution syntax:
var=$(command-name-here arg1 arg2...)
So for your command you can do something like this,
output=$(mysql -u$username -p$password -e " create database testdb")
echo "$output"
I'm running MySQL from the command line and executing SQL stored in files. What I'm trying to do, is prompt the user to enter input so that I can include this in the SQL script? Is there a way to do this with MySQL?
Many thanks,
James
If you want to execute sql script from the file try this:
mysql -u USER -p -D DATABASE < input_filename.sql
I am trying to create a batch script that would connect to a mySQL database and issue a delete command:
#echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback
delete from competency_question_answer;
I will run this script providing the password as a command-line argument, but all this script does is, connects to the database, and the mysql> prompt will be shown. After I exit from mysql, the rest of the batch commands get to execute (and fail, no surprise).
How can I pass the SQL commands from the batch script to the mysql console? Is this even possible?
You need to use command line tools. I don't know if there exists any for MySQL but for SQL there is SQLCMD and for Oracle there is OSQL.
What you can also do is something like this.
mysql -uuser -ppass < foo.sql
Where foo.sql is the commands you want to execute.
You may need to connect multiple times:
#echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback -e delete from competency_question_answer;
Alternatively, you should be able to put all your commands in a separate file such as input.sql and use:
mysql -hlocalhost -urdfdev -p%1 rdf_feedback <input.sql
echo "delete from competency_question_answer;" | mysql -hlocalhost -ur... etc.
Putting multiple sets of commands into .sql batch files works best, and you can execute multiples of these in the .bat file.