I have several hosts from which i want to do the same query. So imagine, i have on each server the database db and a table test like :
mysql> desc test;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | PRI | | |
| data | varchar(255) | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
Obviously, each table test has different data but they all have the same type.
Let's say i have 2 host : h1 and h2 which host these tables.
Now i need to write a script which do the same query on each host and get the results on standard output. first idea was :
shell> (mysql -h h1 -u myusername -p mypwd -d db -e "select * from test";
mysql -h h2 -u myusername -p mypwd -d db -e "select * from test";)
> out.txt
I want to do it faster, so what i did was :
shell> (mysql -h h1 -u myusername -p mypwd -d db -e "select * from test" &
mysql -h h2 -u myusername -p mypwd -d db -e "select * from test" &)
> out.txt
The problem is that i have some collision in my output file like
458 Karma police
459 876 Paint it black Everything in its right place
460 street spirit
You have two rows on line 2.
My idea was that mysql buffer the result, so the buffer could end in the middle of a row. In this example, the buffer would stop at 459. But i can't figure out how to solve the problem.
Any ideas ?
Use a loop
for host in h1 h2 ; do
mysql -h $host -u myusername -p mypwd -d db -e "select * from test" >> out.txt
done
Using >> appends to the file. Hosts will be accessed in order, not simultaneously, and so all results from h1 will be entered first, then all results from h2. If this is insufficient then there's another option but it's more complex.
Related
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 use a query command from bash which returns a number. However, it is printed as a table.
$ mysql -u muser -p$PASS mm -e "SELECT.....;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------------------+
| COUNT(DISTINCT ula.userid) |
+----------------------------+
| 29 |
+----------------------------+
I just want to get 29 and append that to a file with >> file.txt. How can I do that in mysql?
Use skip-column-names and batch-mode with -N and -B respectively:
mysql -u muser -p$PASS mm -NBe "SELECT.....;" >> file.txt
I am looking for a way to pull the information that is returned from Show Master Status so that I can assign the File and Position values to a variable.
I was able to set slave_relay_log_info and slave_work_info to tables but that does not show the local Master information I need.
SHOW MASTER STATUS;
I am not sure what table holds the Show Master Status data.
In Linux bash script you can try below command through mysql client to store the File and Position in variables
For File:
mysql -u username -p password -h IP -P Port -e "show master status" | grep "File"| cut -d ":" -f2
For Position:
mysql -u username -p password -h IP -P Port -e "show master status" | grep "Position"| cut -d ":" -f2
For MySQL 8.0 the log file and log position are in the log_status table of the performance_schema schema:
mysql> SELECT * FROM log_status ;
+--------------------------------------+------------------------------------------------------------------------------------------+------------------+-----------------------------------------------------------+
| SERVER_UUID | LOCAL | REPLICATION | STORAGE_ENGINES |
+--------------------------------------+------------------------------------------------------------------------------------------+------------------+-----------------------------------------------------------+
| 506c04ec-815c-11ed-a962-0800272d3b77 | {"gtid_executed": "", "binary_log_file": "mysql-bin.000022", "binary_log_position": 157} | {"channels": []} | {"InnoDB": {"LSN": 36558461, "LSN_checkpoint": 36558461}} |
+--------------------------------------+------------------------------------------------------------------------------------------+------------------+-----------------------------------------------------------+
The values can be extracted directly like this:
mysql> SELECT JSON_EXTRACT(`LOCAL`, '$.binary_log_file') AS file, JSON_EXTRACT(`LOCAL`, '$.binary_log_position') AS position FROM log_status ;
+--------------------+----------+
| file | position |
+--------------------+----------+
| "mysql-bin.000022" | 157 |
+--------------------+----------+
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 create a file and store in it the results from my query. I have a batch file that contains the single query,
USE database1;
SELECT * FROM table WHERE name = "abc" INTO OUTFILE output.txt;
QUIT
Executing this batch file using,
mysql -u root -p -t -vvv < select.sql
However, the result is not table formatted and fields' names are missing from the top.
100 abc Brown 32
101 abc Flair 25
102 abc McDonald 45
.
.
.
If I remove the INTO OUTFILE statement and print the results on terminal, then is working OK.
+----+------+---------+-----+
| id | name | surname | age |
+----+------+---------+-----+
| 100| abc | Brown | 32|
| 101| abc | Flair | 25|
| 102| abc | McDonald| 45|
+----+------+---------+-----+
How can I achieve the above in a txt file?
UPDATE
Special thanks to GreyBeardedGeek. Here is the solution for this question with help of GreyBeardedGeek.
Batch file:
USE database1;
SELECT * FROM table WHERE name = "abc";
QUIT
and mysql client:
mysql -u root -p -t -vvv < select.sql > output.txt
This should do the trick:
mysql -u root -p -t -vvv < select.sql | sed '1 d' > output.txt
You can also do following:
mysql -uroot -p DatabaseName -t -e "select * from table" > file.txt
This doesn't need to make an SQL file and then filter out the first line.
Besides using mysql client program options (mysql --help) one can configure defaults within .my.cnf file. Example (MacOs, Linux) to enable --table option:
edit/create a file:
vim ~/.my.cnf
add/update:
[client]
table
next time you call mysql command line program it will use options from .my.cnf file producing output in table format, e.g. the same as one has called:
mysql ... --table ...
Please note that command line options override options from the .my.cnf files.
References:
call mysql --help for list of options
https://dev.mysql.com/doc/refman/8.0/en/option-files.html