sivakumar V
sivakumar V

Reputation: 135

unable to connect the mysql database using shell script

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

Answers (2)

J&#252;rgen Steinblock
J&#252;rgen Steinblock

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.

  1. You can either use mysql ... -e SELECT * FROM TABLE or mysql ... << EOF (without -e). You should not mix them.
  2. Don't forget to pass the databasename as a parameter (or with use databasename;) in the sql
  3. Don't forget to add a ; after every sql command, if you have multiple statements

Method 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

NiiL
NiiL

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

Related Questions