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"
Related
Command for executing the MySQL stored procedures using command prompt also required to reduce the size of output history on a console window.
Update:
There is already a similar question with excellent answers on the CALL'ing part.
To run stored procedures from the command line, use the -e option of the mysql client. Example, to call a SP "foo", from bash, you'd do:
bash> mysql -e "call foo()" dbname
If you do not want the command to be stored in the history file, add a space before the mysql command, thusly:
bash> mysql -e "call foo()" dbname
^
|_ space
Want to Execute a MYSQL Stored Procedure in shell script
Example : Employee_config ('ClientId','Data') is Procedure name in my sql
When i try to give in the shell script as below
CALL Employee_config ('ClientId','Data') we are getting CALL: command not found
EXECUTE Employee_config ('ClientId','Data') we are getting EXECUTE: command not found
So would be great if someone can have an update on the same on how to call MySQL stored procedure from shell script
You can try to call it using the mysql command:
mysql -u root –ppassword -e 'call test_procedure();' Databasename
you can follow that with a > out.txt to capture the output into a file.
You can try the below way...
#!/usr/bin/bash
#Script to run automated sql queries
#Declaring mysql DB connection
MASTER_DB_USER='root'
MASTER_DB_PASSWD='root'
MASTER_DB_PORT='3160'
MASTER_DB_HOST='192.168.0.0'
MASTER_DB_NAME='MyDB'
startDate='2018-03-09'
endDate='2018-03-09'
#Prepare sql query
SQL_Query1='select * from mytable limit 1'
SQL_Query2='call carServicedQry'
SQL_Query3='call carServicedQry2("2018-03-09","2018-03-09")'
#mysql command to connect to database
MYSQL -u$MASTER_DB_USER -p$MASTER_DB_PASSWD -D$MASTER_DB_NAME <<EOF
$SQL_Query2
EOF
echo "End of script"
and execute the script like ./myscript.sh
I want to do something like this:
cat files_list | cut -d' ' -f1,3 | mysql -uroot -pxxxx -e "insert into table(var1, var2) values($f1,$f2)"
How can i achieve this thing?
You can use the following bash script:
while read -a record ; do
mysql -uroot -pxxxx -e "insert into table(var1, var2) values(${record[0]}, ${record[2]})"
done < files_list
However, this is simple but performs not very well.
If the task is performance critical, I would build just a single mysql query, which inserts all rows at once, or even better: use LOAD_DATA_INFILE
Also note, if the task is security critical, meaning the input data comes from an untrusted source, I wouldn't use the command line mysql client at all. Using a programming language which supports prepared statements for mysql - like PHP - would be the way to go.
Using a programming language would had another important advantage - you wouldn't need to pass the password via commandline which is insecure.
i'm trying to create a batch script to do some actions in my mysql database and some other actions with linux command, I'm trying to fetch result from my query to mysql in my script to put the result in an associative array, I've found just how to do it in simple array for one column, here is my query:
result=`mysql -h $DATABASE_HOST --user=$DATABASE_USER --password=$DATABASE_PASSWORD --skip-column-names -s -e "select id,type from $DATABASE_NAME.Media where status=0 "
I'm not an expert in batch script, sorry if it is a noob question !
result=$(mysql ...)
declare -A ary
while read id type; do
ary[$id]=$type
done <<< "$result"
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.