code: def add_master_user_mysql(database: str,
                            root_user: str,
                            root_password: str,
                            new_user: str,
                            new_password: str,
                            server: str = "localhost",
                            port: int = 3306,
                            charset: str = "utf8",
                            use_unicode: bool = True,
                            localhost_only: bool = True) -> None:
      
      con = mysql.connect(
          host=server,
          port=port,
          user=root_user,
          passwd=root_password,
          charset=charset,
          use_unicode=use_unicode
      )
      wherefrom = "localhost" if localhost_only else "%"
      sql = ("GRANT ALL PRIVILEGES ON {}.* TO '{}'@'{}' "
             "IDENTIFIED BY '{}'").format(
          database,
          new_user,
          wherefrom,
          new_password
      )
      cursor = con.cursor()
      debug_sql(sql)
      cursor.execute(sql)
      log.info("Added master user {} to database {}", new_user, database)
docs: Connects via PyMySQL/MySQLdb and creates a database superuser.