我有一些代码试图连接到mySQL数据库并检索查询。
连接代码位于名为connection
的模块中,如下所示:
#!/usr/bin/env python3
import pandas as pd
import pymysql
from sqlalchemy import create_engine
COMPANY_SERVER = create_engine(
'mysql+pymysql://view_user:connectiondetails')
def read_sql(query):
return pd.read_sql_query(query, COMPANY_SERVER)
我省略了安全性的连接细节。但这种联系在过去一直有效。
我正在尝试连接和查询数据库表(使用变量名table
拉入)并尝试以下列方式返回表的DESCRIBE
:
#!/usr/bin/env python3
import datetime as dt
import numpy as np
import pandas as pd
from copy import copy
import pymysql
from sql.connection import read_sql
class DBSnapData(object):
def __init__(self,
start_dt,
end_dt,
start_point,
end_point,
variables,
table,
stock_id
):
self.start_dt = start_dt
self.end_dt = end_dt
self.start_point = start_point
self.end_point = end_point
self.variables = variables
self.table = table
self.stock_id = stock_id
self.schema = self.load_schema()
def load_schema(self):
return run_schema_sql_query(self.table)
def run_schema_sql_query(table):
return read_sql('DESCRIBE %s' % table)
我收到以下错误输出:
2018-01-17 22:34:02,717 (pymysql.err.OperationalError) (1142, "SELECT command denied to user 'view_user'@'192.168.9.132' for table 'TBL_MKTDATA_SNAPS_AUS'") [SQL: 'DESCRIBE TBL_MKTDATA_SNAPS_AUS']
Traceback (most recent call last):
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1142, "SELECT command denied to user 'view_user'@'192.168.9.132' for table 'TBL_MKTDATA_SNAPS_AUS'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/scoleman/bin/export_mkt_data", line 405, in main
extract_by_id(sc_id, bbg_ticker, bb_id, lot_size_changes)
File "/home/scoleman/bin/export_mkt_data", line 346, in extract_by_id
stock_id)
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 100, in __init__
self.schema = self.load_schema()
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 106, in load_schema
return run_schema_sql_query(self.table)
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 19, in run_schema_sql_query
return read_sql('DESCRIBE %s' % table)
File "/home/scoleman/git/sql_data/sql/connection.py", line 17, in read_sql
return pd.read_sql_query(query, COMPANY_SERVER)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 331, in read_sql_query
parse_dates=parse_dates, chunksize=chunksize)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 1084, in read_query
result = self.execute(*args)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 975, in execute
return self.connectable.execute(*args, **kwargs)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 2064, in execute
return connection.execute(statement, *multiparams, **params)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 939, in execute
return self._execute_text(object, multiparams, params)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1097, in _execute_text
statement, parameters
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
exc_info
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/sit
如果有人能给我一个关于解决方案的指针,我将不胜感激。我认为问题可能出在pymysql
连接中,但实际上我不确定。
由于
答案 0 :(得分:0)
您的错误显示view_user
无权查询192.168.9.132
此服务器上的数据。
你必须这样做
GRANT SELECT ON *.* TO 'view_user'@'host_or_wildcard' IDENTIFIED BY 'password';
允许您的user
在指定的服务器上进行查询。
或
GRANT ALL PRIVILEGES ON *.* TO 'view_user'@'192.168.9.132';
授予所有特权。
P.S。要做到以上,您必须是root
用户。使用mysql -u root -p
命令连接到mysql服务器。