I am having a simple shell script which will grep a sequence from the log and displays the result on terminal when i run the script. How can i store this result into a MySQL table directly in the script itself?
For Example:
#!/bin/bash
echo "Enter the two numbers to be added:"
read n1
read n2
answer=$(($n1+$n2))
echo $answer
In the above example, how can i store n1, n2 and answer values in a database table.
Create an insert query, and pipe it to stdin of mysql.
echo "insert into myTable values ($n1, $n2, $answer);" | mysql -u .. -h ..
First you need to have a table to hold the value of n1, n2, answer
suppose you have "mytable" in database "test" with 3 columns for n1 n2 and answer, then just insert like this:
echo "insert into test.mytable values ($n1, $n2, $n1+$n2);" | mysql -h hostname -u username -ppassword
or
mysql -h hostname -u username -ppassword -e "insert into test.mytable values ($n1, $n2, $n1+$n2);"
Related
I'm having a file mytest.sql file and it contains following statements:
use test;
tee output123.log;
select database() as 'Database';
select now() as 'Start Time';
set autocommit=0;
insert into table1 values(3,'k');
insert into table1 values(4,'kk');
insert into table1 values(5,'kkk');
commit;
select * from table1;
notee;
$mysql -h localhost -u root -p < mytest.sql
output of log file:
When i login to MySQL and execute the statements I'm getting the output in the below format :
output of log file :
I need the same above output when the statements are executed from shell script. how to do it .
What you're looking for is the --table option to mysql. https://dev.mysql.com/doc/mysql-shell/8.0/en/mysqlsh.html#option_mysqlsh_table notes "batch mode" specifically; mysql is probably inferring that --table should be on if the input device is a terminal and off otherwise, a common mechanism for such programs.
I'm trying to write a command in terminal which will truncate all tables in database except don't truncate those tables which are specified. This are my commands
// create tmp database
mysql -h localhost -u root -proot -e "create database testDb;"
// create tmp user and grant all PRIVILEGES for testDb
mysql -h localhost -u root -proot -e "GRANT ALL PRIVILEGES ON testDb.* TO tmpUser#localhost IDENTIFIED BY 'tmpPass'; FLUSH PRIVILEGES;"
// truncate all tables except table1 and table2
mysql -u tmpUser -ptmpPass -e "SET FOREIGN_KEY_CHECKS=0; SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME, '; ') FROM information_schema.tables WHERE table_schema = 'testDb' AND table_name NOT IN ('table1', 'table2');"
And the out put in terminal is
+-----------------------------------------------------------------------+
| CONCAT('TRUNCATE TABLE ', TABLE_NAME, '; ') |
+-----------------------------------------------------------------------+
| TRUNCATE TABLE tableX; |
| TRUNCATE TABLE tableY; |
| TRUNCATE TABLE tableZ; |
+-----------------------------------------------------------------------+
which is OK, because based on log all tables got TRUNCATED except table1 and table2, but the problem is that when I check in database if tables are truncated, data is still present. So if I check tableX, tableY or tableZ it still has records in it.
So the question is: Is there something wrong with my TRUNCATE TABLES command? Can I somehow check if there is some mysql process which is not finished maybe. How to debug this?
I'm working on Vagrant machine (just telling if this could be a case in any way).
If you need any additional information's please let me know and i will provide. Thank you!
I solved this problem with some sort shell script... It goes like this
EXCLUDETABLES=('table1' 'table2')
ALLTABLES=$(mysql -u root -ppass -Nse "SHOW TABLES testDb")
for TABLE in $ALLTABLES
do
DOTRUNCATE="yes"
for EX in "${EXCLUDETABLES[#]}"
do
if [[ "$EX" == "$TABLE" ]]; then
DOTRUNCATE="no"
break
fi
done
if [[ "$DOTRUNCATE" == "yes" ]]; then
mysql -u root -ppass -e "TRUNCATE TABLE testDb.$TABLE;"
fi
done
I have written a shell script as shown below
#!/bin/bash
result=$(msql -h localhost -uroot -proot -e database "SELECT * FROM USERS");
resultdetails=$(msql -h localhost -uroot -proot -e database "SELECT * FROM DETAILS");
I need to Insert both the result set from result and resultdetails to a new table "TABLE USERDETAILS".
I have tried lot and searched google for hour but couldn't able to get a solution so any help thanks in advance.
try the insert ... select ... syntax. Below is a example
msql -h localhost -uroot -proot -e database "INSERT into userdetail SELECT u1, u2, u3, d1, d2, d3 FROM USERS u INNER JOIN DETAILS d ON (u.id=d.user_id)"
I'm using some bash code which I got off another post on here
#!/bin/sh
users=$(mysql --user=user --password=password --database=$db -h _IP ADDRESS) -s -- execute="select$ users from db limit 1;"|cut -f1)
echo "$users"
The database is DB and the table is users basically I want to be able to get a user count from the table but when I run the script I get
ERROR 1049 (42000): Unknown database 'execute=select users from db limit 1;'
any idea what i'm doing wrong or a better way of doing it? if I do
select * form users;
on the mysql server itself it returns 12 rows in set (0.00 sec) 12 being the number of users, so I just want my script to query the user table on database DB and return the number of rows ie 12.
users_count=$(
mysql --user user_name --password=password -h x.x.x.x <<EOF | tail -n 1
select count(1) from mysql.user;
EOF
)
it seems that the order of your params is wrong...
MYSQL is telling you that 'execute=select users from db limit 1;' is in the position of the database parameter.
Try something like this:
mysql --user user_name --password=password -e 'select users from db_schema.table limit 1;'
Simply try this:
mysql -u username --password=password -h hostname database_name -e 'select count(1) from table limit 1;'
I want to use this to get data from row out of mysql database into text files (one entry under another, 50 entries per file):
$ mysql --user=XXX --password=XXX --batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
split -l 50 -a 5 - "result."
but I also don't want to copy duplicate entries to these files. Will this code remove duplicates or do i need to add something to it to don't copy duplicate entries?
Modifying the SQL to use the DISTINCT directive
e.g.
SELECT DISTINCT userid,displayname FROM Users
will ensure that only unique combinations of userid and displayname are selected.
However this will not prevent userids that have identical displaynames.
SELECT DISTINCT userid,displayname FROM Users
.. or outside the database
mysql --user=XXX --password=XXX --batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
sort -u | \
split -l 50 -a 5 - "result."