Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access mysql database using shell script?

Is there a way I can access MySQL database using shell script? want to do some selects and some inserts on multiple tables?

It will be great if you can give some sample code as I am new to scripting.

like image 243
Bhushan Avatar asked Apr 27 '11 18:04

Bhushan


2 Answers

This link seems to have the information you want.

http://www.cyberciti.biz/faq/using-mysql-in-shell-scripts/

mysql -u user -p dbnane
like image 163
Lactose Avatar answered Oct 22 '22 23:10

Lactose


try this

#!/bin/bash
echo "show all tables"
mysql -uroot -p'password' dbname<<EOFMYSQL
show tables;
EOFMYSQL
echo "Count of all records"
mysql -uroot -p'password' dbname<<EOFMYSQL
select count(*) from tbname;
EOFMYSQL
like image 34
ishimwe Avatar answered Oct 22 '22 23:10

ishimwe