How to structure a cron job and script to execute sql command - mysql

I have a MySQL database accessible through CPANEL. I want to execute a SQL command to DELETE from dbtable where eventdate = 'YYYY-MM-DD'. This is my cron job.
curl -L --max-redirs 1000 -v "https://ottawaoc.ca/test/files/delete_dates.sh" 1>/dev/null
and here is the shell script
#!/bin/bash
mysql --user = "ottawaoc_test" --password = "test ps" --database = "ottawaoc_test" --execute ="DELETE FROM `h8be5_eventregistration` WHERE `eventdate` = '2020-09-27'"
(I do insert the correct password.)
I get output mailed to me and it seems to get the shell script but nothing happens within the database.
Could someone help to give me the correct commands and/or tell me how I can get errors from MySQL.

I used to run mysql crons by putting this in the shell:
#!/bin/bash
echo "mysql statement;" | mysql -B -hHOST -uUSER -pPASS DBNAME

Related

How to verify if all the queries inside a test.sql file are executed successfully through shell script?

I have a q.sql file that has queries like
SET SQL_SAFE_UPDATES = 0;
UPDATE student SET gender = 'f' WHERE gender = 'm';
.
.
UPDATE student SET rollno = '03' WHERE rollno = '003';
This .sql file is executed through a shellscript:
mysql -uuser -ppass DB < q.sql
The command is executed even when one of the queries in q.sql file has failed. Now I want to verify if all the queries are updated successfully.
I tried to echo $? but it always prints 0, i.e command successful, even if the one of the queries in q.sql has failed.
mysql -uuser -ppass DB < q.sql
echo $?
If query fails I want it to print "failed" or stop the further execution of the shellscript.
If you use bash, you can use the set -e in your script and execute each line of your mysql script using -e option.
#!/bin/bash
set -e
while read line; do
mysql -uuser -ppass DB -e "$line"
done < q.sql
For information set --help shows:
-e Exit immediately if a command exits with a non-zero status.
and the man mysql page:
--execute=statement, -e statement
Execute the statement and quit. The default output format is like that produced with --batch. See Section 4.2.3.1, "Using Options on the Command Line", for some examples. With this option, mysql does not use the history file.
You can catch the output in a file for further processing:
mysql -uuser -ppass DB < q.sql > mysql.out
If you have a query that produces a lot of output, you can run the output through a pager rather than watching it scroll off the top of your screen:
mysql -uuser -ppass DB < q.sql | more
If you want to get the interactive output format in batch mode, use mysql -t. To echo to the output the statements that are executed, use mysql -v.
https://dev.mysql.com/doc/refman/8.0/en/batch-mode.html

Using batch variable in sqlcmd query

I'm trying to create a batch to execute a few sql script and some queries. I use sqlcmd in my batch like this :
#echo off
sqlcmd -S server -U user -P password -Q query
As I will execute different queries on the same server, I'd like to replace the server statement with a variable. So I could write something like this :
#echo off
SET server=192.X.X.X
sqlcmd -S server -U user -P password -Q first query
sqlcmd -S server -U user -P password -Q second query
I found this question on SO but I'm still unable to understand how that works :
SQLCMD using batch variable in query
Does anyone have an idea ?
Thanks a lot!
Expanding on my comment a little:
#Echo off
Set "server=192.X.X.X"
Set "user=myname"
Set "password=pa55w0rd"
sqlcmd -S %server% -U %user% -P %password% -Q first query
sqlcmd -S %server% -U %user% -P %password% -Q second query
You may wish to enclose your variables with relevant quoting as necessary.
From Microsoft: "Cmd.exe provides the batch parameter expansion variables %0 through %9. When you use batch parameters in a batch file, %0 is replaced by the batch file name, and %1 through %9 are replaced by the corresponding arguments that you type at the command line."
So if the number of queries you wish to execute is known just pass them in through parameters like this:
Test.bat:
#echo off
SET server=192.X.X.X
sqlcmd -S server -U user -P password -Q %1%
sqlcmd -S server -U user -P password -Q %2%
Then call Test.bat like this:
Test.bat "Select * from Test1" "Select * From Test2"
You can pass up to 9 queries this way.

Run mysql commands in bash script without logging in or adding -u root to every command

I'm writing a bash script to do some db stuff. New to MySQL. I'm on Mac and have MySQL installed via homebrew.
Am using username "root" right now and there isn't a pw set. I included the pw syntax below just to help others out that may have a pw.
My goal is to have mysql commands be as "clean" as possible in my bash script
Not a hige deal, but would like to do this if possible.
Example
# If I can do it without logging in (*ideal)
mysql CREATE DATABASE dbname;
# Or by logging in with - mysql -u root -pPassword
CREATE DATABASE dbname;
# Instead of
mysql -u root -pPassword -e"CREATE DATABASE dbname";
Tried to simplify it. I have a handful of things I gotta do, so would rather keep my code cleaner if possible. I tried logging in with the bash script, but the script stopped once logged into MySQL and didn't run any commands.
Another option I was considering (but don't really like) would be just to keep username and pw string in a var and call it for every commmand like so
# Set the login string variable
login_details="-u root -p password -e"
# example command
mysql $login_details"CREATE DATABASE dbname";
So any ideas?
Write a new bash script file and run this file after putting all your commands into it. Don't forget to give right username and password in your bash script.
For bash script:
#!/bin/bash
mysql -u root -pSeCrEt << EOF
use mysql;
show tables;
EOF
If you want to run single mysql command.
mysql -u [user] -p[pass] -e "[mysql commands]"
Example:
mysql -h 192.168.1.10 -u root -pSeCrEt -e "show databases"
To execute multiple mysql commands:
mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
Note: -B is for batch, print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in nontabular output format and escaping of special characters. -s is silent mode. Produce less output. -e is to execute the statement and quit

need to "build" a mysql command and exec it in a shell script

I'm new to writing shell scripts.
I am attempting to create a database using a shell script. Here's the script:
#!/bin/bash
#create a new db
a="mysql -uuser -ppassword -e'create database $1;'"
exec $a
The command exec mysql -uuser -ppassword -e'create database databaseName;' works in a shell, but when I sh the script, I get the mysql help open...
I think the problem is in the quotes, the simple quote prevent the variable expansion.
You can simply do like this in your script:
#!/bin/bash
#create a new db
mysql -u user -p password -e "create database $1;"
Or you can try to place all your mysql commands in a file, let's say "dbname.sql".
And do this:
#!/bin/bash
#create a new db
mysql -u user -p password "$1" < "$1.sql"
if you like use exec to run commands this can be a possible solution
#!/bin/bash
#create a new db
programm="mysql"
parameter[0]="-ppassword"
parameter[1]="-uuser"
parameter[2]="-ecreate database $1;"
exec "$programm" "${parameter[#]}"
exec parameter are
exec [-a NAME] [-cl] [COMMAND] [ARG...] [REDIRECTION...]
command is $programm and the array parameter is the argument list.
Sounds like you need to use the 'cat' command and a pipe instead of using 'exec' .
cat /path/to/my/file | mysql -h localhost -u root -padmin

bash script to access multiple mysql databases

I all,
I have a series of MYSQL databases with different users and passwords, nevertheless the DB structure is the same for all databases.
I can't create a user with the same username and password to all of them and I need to quickly perform operations on all of them.
I was thinking about a bash script to run via cron.
Any suggestion? I was thinking to something like this but it is not working :(
#!/bin/bash
uconn=(
'mysql -u user_db1 --password=pass_db1 db1 '
'mysql -u user_db2 --password=pass_db2 db2 '
)
for f in "${uconn[#]}"
do
exec ${f}
echo `mysql show tables`
echo `mysql exit`
done
exit
Why not use the documented way?
do
${f} <<EOF
show tables
\\q
EOF
done
Just pasting the full code taking into consideration #Ansgar Wiechers:
#!/bin/bash
uconn=(
'mysql -u user_db1 --password=pass_db1 db1'
'mysql -u user_db2 --password=pass_db2 db2'
)
for f in "${uconn[#]}"
do
${f} <<EOF
show tables
\\q
EOF
done
exit
To execute the code from the local machine to the remote one this works for me:
ssh ssh_user#mydomain.com 'bash -s' < /local/path/to/multiple_db_connections.sh
where the content of multiple_db_connections.sh is the code above