Reputation: 2534
I have this simple code:
cmd = ["mysqldump", "-u Dmitriy", "-pMyPass", "dmitrii"]
p = subprocess.Popen(cmd, stdout = file)
if I try to execute it - I get an error:
mysqldump: Got error: 1045: Access denied for user ' Dmitriy'@'localhost' (using password: YES
when trying to connect.
if I copy-paste this (mysqldump -u Dmitriy -pMyPass dmitrii
)to command line - everything works.
Any suggestions why this could be happening?
Upvotes: 1
Views: 392
Reputation: 4006
On my machine I can fix it by removing the space between -u and the username.
cmd = ["mysqldump", "-uDmitriy", "-pMyPass", "dmitrii"]
Edit: just wanted to add some reasoning
Each of the arguments in your list is escaped. So your first command was more like:
mysqldump '-u Dmitriy' '-pMyPass' 'dmitrii'
If you try that from the command line you'll see that it fails with the same error whereas this works
mysqldump '-uDmitriy' '-pMyPass' 'dmitrii'
I guess it's to do with the argument parser that's used in mysql. The two argument form ('-u' followed by 'Dmitriy') will strip the whitespace (which makes sense). If you give it the single form ('-u Dmitriy') you're saying to it - there's a whitespace on the front of the username.
Upvotes: 2