0

I am trying to connect to mysql in unix from a python script. I provided the password to connect to mysql in the script itself but terminal still prompts for the password. This is what i have till now:

import os
from subprocess import Popen, PIPE

passwd = "user"
command = "mysql -u root -p"

proc = Popen(command.split(), stdin=PIPE)
proc.communicate(passwd+'\n')[1]

Can any one suggest what am i doing wrong here. Or is there a better way to do this.

2

2 Answers 2

1

You can try this:

command = "mysql -u root -p" + passwd
Sign up to request clarification or add additional context in comments.

3 Comments

@harshul gandhi hi, please mark this answer if you find it correct. Thanks
tried that, it prompts me to enter password on terminal
The command will look like: "mysql -u root -puser" for your case.
0

I tried your script in Ubuntu 14.04. It is very easy to start a MySQL in terminal using shell script. Here is the code..

#!/bin/bash       
user=('root')
pass=('XXX')
mysql -u $user -p$pass
echo 'success'

Simply run this code & you can start the MySQL at terminal...

6 Comments

What are the brackets all about around the user and pass values?
Really? That doesn't create a constant for me, in bash I use declare -r. Which version of bash is that in?
GNU bash, version 4.3.11
I misunderstand variable declaration. Brackets used for array declaration.
And that was introduced in Bash 4, prior to that the parentheses are ignored. By the way, it is good practice to put double quotes around variable expansions (`"$pass"') in case there is embedded whitespace.
|

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.