如何通过shell脚本运行mysql命令?

如何通过shell脚本运行mysql命令?
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';

我想创建一个 shell 脚本来执行这些命令并返回结果。

我如何通过 shell 脚本运行该命令?

答案1

还有另一个答案...

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

此外,如果您要在脚本中使用密码,则应该对其进行保护,以便您不想知道密码的人无法读取它。

答案2

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

或者,同样地,像这样:

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

编辑:修复引用。

相关内容