1
mysql -u root -pmysql;

SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb"  FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema='database_name';

I want to create a shell script which will execute these command and return the result.

How may i run this commands through shell script ?

3 Answers 3

6

Yet another answer...

mysql -uroot -pmysql -B --skip-column-names -e'SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb"  FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema="database_name"'

Also, if you are going to have the password in the script, you should secure it so it isn't readable by people you don't wanting to know the password.

3
mysql -u root -pmysql << eof
SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb"  FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema='database_name';
eof
3

Or, equivalently, something like this:

echo 'SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb" FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema="database_name";' | mysql -u root -pmysql

EDIT: fixed quoting.

3
  • Getting Syntax error: Unterminated quoted string Mar 22, 2011 at 6:38
  • sorry, got quoting wrong. adjusted above. Mar 22, 2011 at 7:05
  • @PhilHollenback : Is it possible to save the result in a script variable while executing the above line?
    – Tom Taylor
    Mar 16, 2018 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .