PyMySql连接到MySql OperationalError:1045

时间:2016-11-21 09:45:23

标签: mysql pymysql

我正在尝试通过python中的pymysql包连接mysql:

>>> import pymysql
>>> conn = pymysql.connect(host='10.1.1.216',port=3306, user='linxz_genome', password='linxz123', database='linxz_genome')

工作正常。但是在烧瓶中它会引发一个错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 864, in drop_all
    self._execute_for_all_tables(app, bind, 'drop_all')
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 848, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), tables=tables)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 3447, in drop_all
    tables=tables)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1727, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
  File "/nfs2/biosoft/Python-2.7.10/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1720, in _optional_conn_ctx_manager
    with self.contextual_connect() as conn:
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1910, in contextual_connect
    self.pool.connect(),
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/pool.py", line 338, in connect
    return _ConnectionFairy._checkout(self)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/pool.py", line 645, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/pool.py", line 440, in checkout
    rec = pool._do_get()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/pool.py", line 964, in _do_get
    return self._create_connection()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/pool.py", line 285, in _create_connection
    return _ConnectionRecord(self)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/pool.py", line 411, in __init__
    self.connection = self.__connect()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/pool.py", line 539, in __connect
    connection = self.__pool._creator()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 96, in connect
    connection_invalidated=invalidated
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 90, in connect
    return dialect.connect(*cargs, **cparams)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 377, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/pymysql/__init__.py", line 90, in Connect
    return Connection(*args, **kwargs)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/pymysql/connections.py", line 688, in __init__
    self.connect()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/pymysql/connections.py", line 906, in connect
    self._request_authentication()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/pymysql/connections.py", line 1114, in _request_authentication
    auth_packet = self._read_packet()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/pymysql/connections.py", line 981, in _read_packet
    packet.check_error()
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/pymysql/connections.py", line 393, in check_error
    err.raise_mysql_exception(self._data)
  File "/nfs3/onegene/user/group1/linxingzhong/workspace/flask/env/lib/python2.7/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
OperationalError: (OperationalError) (1045, u"Access denied for user 'linxz_genome'@'10.1.1.223' (using password: YES)") None None

从错误消息中,我发现IP 10.1.1.215与python代码中的10.1.1.216不同。 config.py位于以下位置:

class ProductionConfig(Config):
    dbuser = 'linxz_genome'                                                     
    dbname = 'linxz_genome'
    dbhost = '10.1.1.216:3306'
    dbpass = 'linxz_glean20161026'
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://' + \ 
            dbuser + ':' + dbpass + '@' + dbhost + \ 
            '/' + dbname + '?charset=utf8mb4'
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True

我找到了一些关于此错误的threads并尝试了以下命令:

mysql> GRANT ALL on *.*  to 'linxz_genome'@'10.1.1.216' IDENTIFIED BY 'linxz123' WITH GRANT OPTION;
ERROR 1045 (28000): Access denied for user 'linxz_genome'@'%' (using password: YES)   

如何在flsak中修复此错误?

0 个答案:

没有答案