Reputation: 135
Am facing problem to connect the MySQL DB from shell script. Please find the below snippet i have written for connecting the MySQL data base. please suggest on this.
My shell Script:
#!bin/bash
Query="select * from Main"
MySQL -u root -p '!!root!!' -e kpi << EOF
$Query;
EOF
Please check the above code and suggest me how to connect the DB.
Upvotes: 0
Views: 3689
Reputation: 31743
I think it should be
-pThePassword
So you should delete the space between -p
and the pass. Also you should not use an apostrophe (except it is part of the pass itself. Use a backslash to escape special characters.
Second: *nix systems are case sensitive, please try mysql
instead of MySQL
Update
You could also try to type your password into a file and read it with your script
mysql -u root -p`cat /tmp/pass` -e "SHOW DATABASES"
The file /tmp/pass
should contain your password without any newline char at the end.
Update 2
Your Script is wrong.
mysql ... -e SELECT * FROM TABLE
or mysql ... << EOF
(without -e). You should not mix them.use databasename;
) in the sql;
after every sql command, if you have multiple statementsMethod One:
mysql -u root -ppassword databasename -e "SELECT * FROM main"
Method Two:
mysql -u root -ppassword databasename << EOF
SELECT * FROM main
EOF
Method Three:
mysql -u root -ppassword << EOF
USE databasename;
SELECT * FROM main;
EOF
Upvotes: 1
Reputation: 2827
mysql --user=root --password=xxxxxx -e "source dbscript.sql"
This should work for Windows and Linux.
If the password content contains a !
(Exclamation mark) you should add a \
(backslash) in front of it.
Upvotes: 0