I have a problem with writing in the variable from table in MySQL database.
DB="--user=$OSD_USERNAME \
--password=$OSD_PASSWORD \
--database=$OSD_DB -N";
mysql --user="$user" \
--password="$password" \
--database="$database" \
--execute="DROP DATABASE $user; CREATE DATABASE $database;"
id_customer = $(echo "SELECT id FROM customers WHERE customers.customer='John'"| mysql $DB)
My problem is that id_customer is still empty.
When I connect to mysql databases and put there this:
SELECT id FROM customers WHERE customers.customer='John'
then it give me table like this
+----+
| id |
+----+
| 1 |
+----+
Only empty, no errors? Try remove spaces between variable name and $
Related
I have the below script for deleting duplicate entries from particular table. I have scheduled this job through cron job daily once. But delete is not happening from cron job. If I run the same query from the MySQL console, it works fine. Can anyone help me what is wrong with crontab?
#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%d%b%Y"`
MYSQL_USER='**********'
MYSQL_PASS='*********'
MYSQL_DB='*********'
MYSQL_HOST='********'
START_TIME=`date`
echo "Deletion of duplicate records started for the table CPM_QUESTIONS at ${START_TIME}"
SQL="delete from CPM_QUESTIONS WHERE ID IN (select * from duplicate_questions_id);"
echo $SQL | /usr/bin/mysql --user=$MYSQL_USER --host=$MYSQL_HOST --password=$MYSQL_PASS $MYSQL_DB
END_TIME=`date`
echo "Deletion of duplicate records completed for the table CPM_QUESTIONS at at ${END_TIME}"
Subquery table ddl as below:
mysql> desc duplicate_questions_id;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| id | int | NO | | 0 | |
+-------+------+------+-----+---------+-------+
1 row in set (0.00 sec)
You probably want to quote your $SQL variable when passing it to mysql as well as any other variable you use. If you do not do this, bash will glob the * character in your SQL query and pass the list of files in the current directory to mysql, rather than a literal asterisk.
Your full script can thus look like this:
#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY="`date +"%d%b%Y"`"
MYSQL_USER='**********'
MYSQL_PASS='*********'
MYSQL_DB='*********'
MYSQL_HOST='********'
START_TIME="`date`"
echo "Deletion of duplicate records started for the table CPM_QUESTIONS at ${START_TIME}"
SQL="delete from CPM_QUESTIONS WHERE ID IN (select * from duplicate_questions_id);"
echo "$SQL" | /usr/bin/mysql --user="$MYSQL_USER" --host="$MYSQL_HOST" --password="$MYSQL_PASS" "$MYSQL_DB"
END_TIME="`date`"
echo "Deletion of duplicate records completed for the table CPM_QUESTIONS at at ${END_TIME}"
I am simply trying to understand why these 2 commands have different outputs on my screen:
$> mysql -ularavel -ppassword -e 'select id from queue.jobs;'
+-------+
| id |
+-------+
| 20945 |
| 20946 |
+-------+
$> watch "mysql -ularavel -ppassword -e 'select id from queue.jobs;'"
Every 2.0s: mysql -ularavel -ppassword -e 'select id from queue.jobs;'
id
20945
20946
Notice that the watch command does not draw the table borders. I simplified this example, but for multiple columns the table is distorted and difficult to read.
So, why? Is there a difference between the input/output of the watch command that is different from what's directly in the terminal?
Tried on OSX with iTerm2 and the default Terminal app
I am trying to create a bash shell script that runs an sql query and later on create a cronjob that runs it at an specific time.
I created my bash script see below
mysql -u $host -D $dbname -u $user -p$password -e $mySqlQuery
I have wrap -u -D -p -e all in variables. I have also change it to and executable file. When i run it. it gives out an output stating. Command not found. can anyone tell the mistake i made?
Below is the bash script
host="host"
user="user"
dbname="database"
password="password"
mySqlQuery = "SELECT *
FROM invoice i
JOIN item it
ON it.invoice_id = i.id
JOIN user u
ON i.user_id = u.id
JOIN gateway_response gr
ON gr.invoice_id = i.id
WHERE i.created_at >= '2019-03-01 00:00:00' and
i.created_at <= '2019-03-17 23:59:59' and i.status=9"
mysql -u $host -D $dbname -u $user -p$password -e $mySqlQuery
Below is the error i am receiving when i run it.
/home/chris2kus/givingDetectRun.sh: line 8: mySqlQuery: command not found /home/chris2kus/givingDetectRun.sh: line 20: mysql: command not found –
There must be no spaces around the = and the variable name mySqlQuery.
Also, I suggest you wrap your variables around double quotes, i.e., use "$host" instead of just $host.
You can write a file like this and chmod 755 filename.sh :
#!/bin/bash
host="localhost"
dbname="test"
user="root"
password="xxxxxxxxxx"
mySqlQuery="select *
from col;"
mysql -u $host -D $dbname -u $user -p$password -e "$mySqlQuery"
Sample
$chmod 755 testmysql.sh
$
$ ./testmysql.sh
+----+------+------+------+
| id | Col1 | Col2 | Col3 |
+----+------+------+------+
| 1 | 1 | 2 | 3 |
| 2 | 2 | 3 | 4 |
| 3 | 3 | 4 | 5 |
+----+------+------+------+
$
For starters, make sure your line 5 looks like
mySqlQuery="SELECT..."
(notice no spaces on either side of the assignment operator)
For seconds, try to re-format your entire MySQL query to fit into a single line.
(perhaps Heidi since it's a query, since that will keep you at least syntax-wise in the clear of errors)
For thirds, once you confirm that the bash runs as intended, add \n to tell the bash that you're continuing the command in the next row
Prototyping before optimization. Get it running before you get it flying.
I am trying to get count of column user_Id using count(user_Id) from mysql as follows:
count=$(mysql -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")
I am not getting what is wrong with it. I want it's numeric result. What could help me?
Using options -B -N in command mysql
--batch, -B - 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.
--skip-column-names, -N - Do not write column names in results.
count=$(mysql -uroot -proot csv_imports -B -N -e "select count(user_Id) from test_data where user_Id=\"12345\";»)
without options -B -N result is:
+----------------+
| count(user_id) |
+----------------+
| 4 |
+----------------+
with option -B result is:
count(user_id)
4
with option -B -N result is:
4
I am using this command:
mysql -u user -ppassword database -e "select distinct entityName,entitySource from AccessControl"
The output is like this:
+-----------------------+--------------+
| entityName | entitySource |
+-----------------------+--------------+
| low | Native |
| high | Native |
| All Groups | AD |
| Help Ser vices Group | AD |
| DEFAULT_USER_GROUP | Native |
| SYSTEM | Native |
| DEFAULT_BA_USER_GROUP | Native |
| soUsersGrp | Native |
+-----------------------+--------------+
My question is: how can I dynamically create an array of variables to store the values entityName and entitySource? What I need to use is use every value of entityName and entitySource to update another table.
Earlier I was trying to store the output in a file and access each line using awk, but that doesn't help because one line may contain multiple words.
Sure, this can be done. I'd like to second the idea that piping mysql to mysql in the shell is awkward, but I understand why it might need to be done (such as when piping mysql to psql or whatever).
mysql -qrsNB -u user -p password database \
-e "select distinct entityName,entitySource from AccessControl" | \
while read record; do
NAME="`echo $record|cut -d' ' -f 1`" # that's a tab delimiter
SOURCE="`echo $record|cut -d' ' -f 2`" # also a tab delimiter
# your command with $NAME and $SOURCE goes here ...
COMMAND="select trousers from namesforpants where entityName='${NAME}'" # ...
echo $COMMAND | mysql # flags ...
done
the -rs flags trim your output down so that you don't have to grok that table thing it gives you, -q asks that the result not be buffered, -B asks for batch mode, and -N asks to not have column names.
What you do with those variables is up to you; probably I would compose statements in that loop and feed those to your subsequent process rather than worry about interpolation and quotes as you have mentioned some of your data has spaces in it. Or you can write/append to a file and then feed that to your subsequent process.
As usual, the manual is your friend. I'll be your friend, too, but the manpage is where the answers are to this stuff. :-)
#!/usr/bin/env bash
mysql -u user -ppassword database -e "select distinct entityName,entitySource from AccessControl" | while read name source; do
echo "entityName: $name, entitySource: $source"
done
Please check it, I fixed it through exec.
[wcuser#localhost]$ temp=`exec mysql -h10.10.8.36 --port=3306 -uwcuser -pwcuser#123 paycentral -e "select endVersion from script_execution_detail where releaseNo='Release1.0' and versionPrefix='PS'"|tail -1`
[wcuser#localhost]$ echo $temp
19