본문 바로가기
Development/Python

[Python] OperationalError: (1045, "Access denied for user USER@HOST (using password: YES)") 해결 (pymysql)

by 성딱이 2022. 4. 18.
반응형
Error Message : OperationalError: (1045, "Access denied for user 'USER'@'HOST' (using password: YES)")

 

실행 코드 : pymysql 라이브러리를 활용하여 DB접속을 위한 객체를 로드하는 사용자 정의 함수

In [40]:
def get_connection(db_name):
    '''
    DB Connection
    I : X
    O : conn, curs, engine

    '''
    # db_name = 'DATRAW'
    db_info = info_dict['db_info']

    host = db_info['host']
    port = db_info['port']
    user = db_info['user']
    password = db_info['password']
    
    conn = pymysql.connect(host=host, 
                   port=port,
                   user=user,
                   password=password,
                   database=db_name)
    curs = conn.cursor()
    engine = create_engine(f"mysql+pymysql://{user}:{password}@{host}:{str(port)}/" \
        + db_name + "?charset=utf8", encoding='utf-8')
    

    return conn, curs, engine
# end def get_connection
 
 
In [41]:
conn, curs, engine = get_connection('DB')
 
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-41-2398e0e9f729> in <module>
----> 1 conn, curs, engine = get_connection('DB')

<ipython-input-40-0df5a5916330> in get_connection(db_name)
     18                    user=user,
     19                    password=password,
---> 20                    database=db_name)
     21     curs = conn.cursor()
     22     engine = create_engine(f"mysql+pymysql://{user}:{password}@{host}:{str(port)}/" \

~\Anaconda3\lib\site-packages\pymysql\__init__.py in Connect(*args, **kwargs)
     92     """
     93     from .connections import Connection
---> 94     return Connection(*args, **kwargs)
     95 
     96 from . import connections as _orig_conn

~\Anaconda3\lib\site-packages\pymysql\connections.py in __init__(self, host, user, password, database, port, unix_socket, charset, sql_mode, read_default_file, conv, use_unicode, client_flag, cursorclass, init_command, connect_timeout, ssl, read_default_group, compress, named_pipe, autocommit, db, passwd, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout, bind_address, binary_prefix, program_name, server_public_key)
    323             self._sock = None
    324         else:
--> 325             self.connect()
    326 
    327     def _create_ssl_ctx(self, sslp):

~\Anaconda3\lib\site-packages\pymysql\connections.py in connect(self, sock)
    597 
    598             self._get_server_information()
--> 599             self._request_authentication()
    600 
    601             if self.sql_mode is not None:

~\Anaconda3\lib\site-packages\pymysql\connections.py in _request_authentication(self)
    859 
    860         self.write_packet(data)
--> 861         auth_packet = self._read_packet()
    862 
    863         # if authentication method isn't accepted the first byte

~\Anaconda3\lib\site-packages\pymysql\connections.py in _read_packet(self, packet_type)
    682 
    683         packet = packet_type(buff, self.encoding)
--> 684         packet.check_error()
    685         return packet
    686 

~\Anaconda3\lib\site-packages\pymysql\protocol.py in check_error(self)
    218             errno = self.read_uint16()
    219             if DEBUG: print("errno =", errno)
--> 220             err.raise_mysql_exception(self._data)
    221 
    222     def dump(self):

~\Anaconda3\lib\site-packages\pymysql\err.py in raise_mysql_exception(data)
    107         errval = data[3:].decode('utf-8', 'replace')
    108     errorclass = error_map.get(errno, InternalError)
--> 109     raise errorclass(errno, errval)

OperationalError: (1045, "Access denied for user 'USER'@'HOST' (using password: YES)")

 

원인 : password가 틀려서 발생한 에러

해결 : password를 알맞게 수정해 줌

 

 

끝 ;; 

 

Access denied라고 해서 권한 문제인 줄 알았는데, 단순하게 패스워드가 틀린상태로 입력된 것이었다 ;; 

 

 

반응형

댓글