I’m trying to import a .sql dump from disk into MySQL via Python and subprocess. I.e. the equivalent to
mysql -u user -ppassword db < dump.sql
My Python code looks like this (but I have tried a ton of alternatives :)):
proc = subprocess.Popen(
("mysql -u %s -p%s database" % (MYSQL_USER, MYSQL_PASSWORD)).split(),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=False)
out, err = proc.communicate('source /tmp/dump.sql')
The application finishes successfully, but there are no rows imported to MySQL. I have also tried pipe the dump.sql like this:
proc = subprocess.Popen(
("mysql -u %s -p%s database < /tmp/dump.sql" % (MYSQL_USER, MYSQL_PASSWORD)).split(),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=False)
out, err = proc.communicate()
If important, when I set shell=True I get ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO))
Can anyone please point me in the right direction?
You are using Popen.communicate() wrong.