How to use tee command of MySQL in shellscript? - mysql

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.

Related

How to copy data from one table to other in same database?

In MySQL how to copy data from one table to another in the same database table?
I know insert into select, but it is taking forever to do this, especially on a live database, we can't take a risk.
Some conditions are there:
1. table1 is a source table and table1_archives is a destination table.
2. table1_archives already have data so we can only append.
My attempt:
time mysqldump --log-error=$logfile --complete-insert --insert-ignore
--no-create-info --skip-triggers --user=$dbuser --host=$host $dbname table1
--where="created < now()-interval 10 month" > $filename
But it has the name of table1, so I can't insert it into table1_archives.
Any guidance will be appreciated.
Thanks in advance.
In your output file, you need to change the table name table1 to table1_archives. Unfortunately mysqldump does not have any way to do this. You will have to do this on the fly using sed, which will rename everything in output file from table1 to table1_archives.
Since your columns can also contain the content like table1, its better to search and replace by enclosing them in backticks.
You can also use gzip to compress the output file.
Here is the command that worked for me
mysqldump -u USER -h HOST -p --skip-add-drop-table --no-create-info --skip-triggers --compact DB table1 |\
sed -e 's/`table1`/`table1_archives`/' | gzip > filename.sql.gz
"but it is taking forever to do this"
There is a small trick to avoid this and then insert into will work faster:
Insert into table1 select * from table2
Trick:
step-1: drop all indices from table2
step-2: execute query
step-3: create indices again

Copy mysql database from mysql command line

How do I copy database1 to database2 from the mysql command line?
I know that mysqldump is one option, or I can do
drop table if exists table2;
create table table2 like table1;
insert into table2 select * from table1;
But, i won't want to do this manually for each table name. Is this possible?
The key here is "from the mysql command line"
mysql> ...
First create the duplicate database:
CREATE DATABASE database2;
Make sure the user and permissions are all in place and:
mysqldump -u admin -p database1| mysql -u backup -pPassword database2;
You can also refer to the following link for executing this on mysql shell.
http://dev.mysql.com/doc/refman/5.5/en/mysqldump-copying-to-other-server.html
In a stored procedure, loop over the results of
SELECT table_name FROM information_schema.tables WHERE table_schema = 'sourceDB';
At each iteration, prepare and execute a dynamic SQL statement:
-- for each #tableName in the query above
CREATE TABLE targetDB.#tableName LIKE sourceDB.#tableName;
INSERT INTO targetDB.#tableName SELECT * FROM sourceDB.#tableName;
Sorry, the MySQL syntax for stored procedure being a serious pain in the neck, I am too lazy to write the full code right now.
Resources:
CREATE PROCEDURE
PREPARE and EXECUTE
CURSORS
Mysqldump can be used from mysql command line also.
Using: system (\!): Execute a system shell command.
Query:
system mysqldump -psecret -uroot -hlocalhost test > test.sql
system mysql -psecret -uroot -hlocalhost < test.sql

Store the results of a shell script in MySQL

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);"

Getting MYSQL DB info via bash

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;'

mysql command line thinks I am using a different table. ERROR 1146 (42S02) at line 1

I have a database called maxppgco_drp1 with a table called users. I want to update the users table.
mysql --user=maxppgco --password=myPass -D 'maxppgco_drp1' -e "SELECT * FROM maxppgco_drp1.`users` WHERE 1";
the error I get is:
ERROR 1146 (42S02) at line 1: Table 'maxppgco_drp1.maxppgco' doesn't exist
How do I properly select the table. The query works in PHPmyAdmin.
EDIT::::Solution::::
mysql --user=maxppgco --password=myPass -e "USE maxppgco maxppgco_drp1; SELECT * WHERE 1";
Need to choose database in sql -e statement with USE statement.. not in the command line with -D
Try,mysql --user=maxppgco --password=myPass -D 'maxppgco_drp1' -e "use maxppgco_drp1;SELECT * FROM maxppgco_drp1 WHERE maxppgco_drp1.'users' = 1";
I think the problem is that your table is not called maxppgco.users but maxppgco. Users is probably a column that is part of your WHERE cause.
In one statement mysql --user=maxppgco --password=myPass -D maxppgco_drp1 -e "SELECT * FROM maxppgco_drp1 WHERE maxppgco_drp1.users = 1";.
Or perhaps breaking up the statement works.
Login with your username / password (mysql --user=maxppgco
--password=myPass) or (mysql -u maxppgco -p).
Type in your password "myPass".
Connect to you database maxppgco_drp1 (connect
maxppgco_drp1).
Execute your SQL statement (SELECT * FROM
maxppgco_drp1 WHERE maxppgco_drp1.'users' = 1).