使用mysql连接数据库提示ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using passwor)
在学习sqlalchemy的时候,遇到了无法连接数据库的情况,确认了账号密码等都没有错误,却发现navicat可以先ssh在进行连接的方式访问数据库,于是查询了部分资料,有些数据库为了安全性考虑,不允许个人pc直接通过3306连接mysql,需要通过ssh到跳板机,在通过3306端口连接数据库
# coding=utf-8
import pymysql
import threading
from config import config
class DbUtil:
def __init__(self):
self.conn = pymysql.Connect(
host=config.db_host,
port=config.db_port,
user=config.db_username,
password=config.db_password,
database=config.database,
charset=config.db_charset
)
# 查询单条数据
def query(self, sql):
# 获取游标
cur = self.conn.cursor()
try:
threading.Lock().acquire()
cur.execute(sql)
threading.Lock().release()
return cur.fetchone()