LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-09-2012, 10:15 AM   #1
drigby
LQ Newbie
 
Registered: Apr 2012
Location: Colorado
Distribution: CentOS
Posts: 11

Rep: Reputation: Disabled
Trying to write script that tests mysql login


Hi,

I'm familiar with PHP, but bash is pretty new to me.

I'm writing a bash script that logs into mysql. I need the script to test whether it's using the proper mysql user/pass, so that it can switch to another if necessary. The problem is everything I've tried throws an error, and ends the script.

What I've been doing so far looks like this (though I've tried many variations):
Code:
mysql -h localhost -u Username -pPassword <<EOF; exit; EOF
if [ "$?" != "0" ]; then
  # Code that tests alternate credentials
fi
I've tried using -f to keep the errors from stopping the script, but I think that "force" does not apply to the mysql command itself. I think "force" only applies to the commands executed in mysql.

I don't see any other options in the man page that seem to apply to what I'm doing. Could have misunderstood something though.

Does anyone have some code that would work in this situation?

Thanks in advance!
 
Old 04-09-2012, 10:25 AM   #2
JSkywalker
Member
 
Registered: Aug 2007
Distribution: openSUSE 12.1
Posts: 96

Rep: Reputation: 24
If you are on linux (i think it can be done on Windows too, but i did not test that) you can create a file called '.my.cnf' in your home-directory
Quote:
[client]
user=Username
password=Password
After is you can start mysql without the username/password. The '-h localhost' is not needed when you try to access mysql on your local system.
 
Old 04-09-2012, 10:34 AM   #3
drigby
LQ Newbie
 
Registered: Apr 2012
Location: Colorado
Distribution: CentOS
Posts: 11

Original Poster
Rep: Reputation: Disabled
I want to have root privileges to mysql, and in this case I won't know if any password has been set on mysql. I'm guessing that if I use your method, and the root password has previously been set, I'm not going to get into mysql with full privileges?

I could be wrong though.
 
Old 04-09-2012, 10:43 AM   #4
JSkywalker
Member
 
Registered: Aug 2007
Distribution: openSUSE 12.1
Posts: 96

Rep: Reputation: 24
no, if you want to have access, you should know the 'proper' password
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write a script with auto login ssh and then run commands? keber314 Linux - Server 13 02-16-2009 03:37 PM
Need to write a shell script which will check the GUI login and retuen the status cod bhsk_08 Linux - General 2 10-14-2008 01:08 PM
Is It Possible to write a script which login a remote server senthilvael Linux - Newbie 4 11-21-2007 06:23 PM
PHP login script using mysql issue ACDII Linux - Server 4 01-20-2007 04:58 AM
i am tryin to write a script that will send data to MySQL rhuser Programming 7 03-06-2003 03:17 PM


All times are GMT -5. The time now is 11:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration