How can i store mysql databases in linux using shell scripting - mysql

How can i store mysql databases in linux using shell scripting
script:
mysql -uusername -hhostname -ppassword -e "show databases"

I think you want this: http://lists.mysql.com/mysql/96132
mysql> use mysql
Database changed
mysql> tee /tmp/mysqltee
Logging to file '/tmp/mysqltee'
mysql> show tables;
+-----------------+
| Tables_in_mysql |
+-----------------+
| columns_priv |
| db |
| host |
| tables_priv |
| user |
+-----------------+
5 rows in set (0.02 sec)
mysql> notee
Outfile disabled.
mysql>
If the file exist, the output will be appended to the existing file (/tmp/mysqltee).
As you can see, the output is also displayed on the screen. This may not be
what you want, especially if the output is big... You could use
mysql -e "select table_name from user_tables" database > output.txt
or
mysql database < script.sql > output.txt
from the os command line. (You may also need to use -u, -p and/or -h, use
the same as when you do a 'normal' start of the mysql client.)

it may help you
#!/bin/bash
results=($(mysql --user root -pwelcome -Bse "show databases;"))

The following code will retrieve all database names into a variable called dbnames. After that, it iterates will just echo a string with each name individually
#!/bin/bash
dbnames=`mysql --user=user --password=password -se "show databases;"`
for x in $dbnames;
do
echo "There is a database called $x"
done;

Related

Different output of the watch cli command for mysql select tables

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

How can I execute multiline sql statements in windows command line with mysql.exe?

On Linux shells we can execute sql commands both in one line as multiline:
mysql -u username -usecret -e "show databases;use mydatabase;show tables;"
mysql -u username -usecret -e "show databases;
use mydatabase;
show tables;"
But multiline inside the sql script seems not to work in Windows.
How can I execute multiline sql statements in windows? Is there some kind of <newLine> for mysql multilines?
# the following doesn't work in windows:
mysql.exe -u username -usecret -e "show databases;
use mydatabase;
show tables;"
When I use echo and pipe it to mysql.exe the ouput is not formatted as a table
echo show databases; ^
use mydatabase; ^
show tables; | mysql.exe -u username -usecret
it simply outputs
Database
information_schema
mydatabase
performance_schema
sys
...
and I would expect the following output:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydatabase |
| performance_schema |
| sys |
+--------------------+
...
Use --table or -t for output in table format:
echo show databases; ^
use mydatabase; ^
show tables; | mysql.exe -u username -usecret --table

Getting only the output of query without table shape

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

MySQL 5.6 Where is Show Master Status information stored

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 |
+--------------------+----------+

Shell script: Launch Mysql script using shell variable

I have a problem launching MySQL script from shell. I assign a value to my variable ${x}, using filename. So I have to launch a MySQL script using this variable. I would like to launch script without insert all MySQL code in shell (is too long) but using:
mysql -h localhost -uuser -ppsw DB < script.sql
My tentatives are:
mysql -h localhost -uuser -ppsw DB -e "set #x=${x}; source script.sql"
mysql -h localhost -uuser -ppsw DB -e "set #x=${x};"
mysql -h localhost -uuser -ppsw DB< script.sql
But not work for me. Could you help me?
I was surprised that your first solution didn’t work:
mysql -h localhost -uuser -ppsw DB -e "set #x=${x}; source script.sql"
Analysis
source is a MySQL command
set #x=${x}; is an SQL statement.
I thought that there may be an issue combining the two types as one statement as the MySQL --execute=statement, -e statement is supposed to execute the statement and quit.
The and quit part is why the redirected stdin is ignored when I tried my first idea:
mysql -h localhost -uuser -ppsw DB -e "set #x=${x};" < script.sql
Solution
After further experiments, I figured out that simply appending a semi-colon to the source command will prevent the syntax error.
I can’t say why this works as a semi-colon isn’t usually required to terminate the last SQL statement of a list but there you have it:
mysql -h localhost -uuser -ppsw DB -e "set #x=${x}; source script.sql;"
As Glenn Jackman pointed out, if the shell variable is a non-numeric string, the shell variable will have to be wrapped in single quotes so that when the MySQL variable is being assigned, MySQL will treat the right hand side (the shell variable) as a string literal instead of as an identifier for a column name:
mysql -h localhost -uuser -ppsw DB -e "set #x='$x'; source script.sql;"
This version will also work safely with numeric strings as can be seen in the examples below. I’ve also removed the curly braces around the shell variable since they’re not necessary.
Examples
Contents of t.sql:
select now();
select #variable as 'Contents of variable';
Use a numeric string as the shell variable:
$ number=3
$ mysql -e "set #variable=$number; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:06:45 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| 3 |
+----------------------+
Use a non-numeric string as the shell variable generates errors:
$ text=text
$ mysql -e "set #variable=$text; source t.sql;"
ERROR 1054 (42S22) at line 1: Unknown column 'text' in 'field list'
$ text="This is a string"
$ mysql -e "set #variable=$text; source t.sql;"
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a string' at line 1
Now wrap the shell variable in single quotes:
$ mysql -e "set #variable='$text'; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:08:04 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| This is a string |
+----------------------+
$ text=text
$ mysql -e "set #variable='$text'; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:10:53 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| text |
+----------------------+
$ mysql -e "set #variable='$number'; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:11:42 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| 3 |
+----------------------+
Using a non-existing shell variable will set the MySQL variable to an empty string:
$ mysql -e "set #variable='$nonexistent'; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:06:14 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| |
+----------------------+