Search code examples
pythonmysqlwindowscmd

How can I read the error content outputs of mysql command executed from python


bash_fc = rf"mysql -u {source_user_name} -h {source_ipv4_addr} --password={source_db_password}"

When I use the following functions for the above command,

from subprocess import PIPE,run
def cons_r(cmd):
  response = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
  return response.stdout
response = os.popen(bash_fc)

mysql: [Warning] Using a password on the command line interface can be insecure. mysql: Unknown OS character set 'cp857'. mysql: Switching to the default character set 'utf8mb4'. ERROR 1045 (28000): Access denied for user 'root'@'pc.mshome.net' (using password: YES)

I can't read the output, is there a method you know of so I can read it?


Solution

  • This change fixed the problem

    from subprocess import PIPE, run, STDOUT
    def cons_r(cmd):
      response = run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True, shell=True)
      return response.stdout