0

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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

tried. but still am getting the error as "ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)"
What happens if you just use mysql -u root -p without giving a pass? MySQL will prompt you to enter the password. Does this work?
@sivakumar That means your password is incorrect. Try another user. If you have forgotton and need to reset the password, maybe this helps: dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html
Now I can able to connect the my sql database. after connecting the database am getting the below message: mysql Ver 14.14 Distrib 5.5.30, for Linux (x86_64) using readline 5.1 Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Usage: mysql [OPTIONS] [database] -?, --help Display this help and exit. -I, --help Synonym for -?..like this am getting. Please suggest on this.
@sivakumarV - Ok after you can connect you have to fix your code. I updated my answer.
|
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.