Following way, we can execute the MySQL Queries directly using the Linux command line using "-e or --execute" option
# mysql -u root -p -e "select @hostname,now()";
Enter password:
+----------------------+---------------------+
| @hostname | now() |
+----------------------+---------------------+
| NULL | 2023-10-21 14:25:44 |
+----------------------+---------------------+
# mysql -u root -p --execute "show databases;"
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
We can also execute multiple query using semicolon:
# mysql -u root -p -e "show databases;select user,host from mysql.user"
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+