我正在尝试安装mysql-connector-python,我收到以下错误:
Could not find any downloads that satisfy the requirement mysql-connector-python==2.0.4 (from -r requirements.txt (line 2))
Cleaning up...
No distributions at all found for mysql-connector-python==2.0.4 (from -r requirements.txt (line 2))
Run Code Online (Sandbox Code Playgroud)
我遵循的步骤是:
virtualenv -p python3 env/
source env/bin/activate
pip3 install -r requirements.txt --allow-external mysql-connector-python
Run Code Online (Sandbox Code Playgroud)
将requirements.txt包含以下内容:
beautifulsoup4==4.4.1
mysql-connector-python==2.0.4
requests==2.9.1
wheel==0.24.0
Run Code Online (Sandbox Code Playgroud)
怎么能克服这个问题?
我在制作练习脚本以自学一些 Python 和 mysql.connector 库时遇到了这个问题。当我使用单列执行查询并打印值时,我得到的结果如下:
('tech-pc-1',) #Python 3.4.3
(u'tech-pc-1',) #Python 2.7.6
但是,当我执行多列查询并打印值时,我得到了我想要的结果。tech-pc-1 jdoe
我在运行 Ubuntu 14.04 的服务器上执行此操作。
from mysql.connector import (connection)
import datetime<br>
conn = connection.MySQLConnection(user='user',password='pass',host='host',database='db')
single_col_query = "select comp from logons where dt between %s and %s"
multi_col_query = "select comp,user from logons where dt between %s and %s"
end_dt = datetime.datetime.now()
begin_dt = datetime.datetime(end_dt.year, end_dt.month, 1, 0, 0, 0)
cursor = conn.cursor()
cursor.execute(single_col_query, (begin_dt, end_dt))
for(comp) in cursor:
print(comp) # ex. ('tech-pc-1',) or (u'tech-pc-1',)
cursor.execute(multi_col_query, (begin_dt, end_dt)) …Run Code Online (Sandbox Code Playgroud) 我必须将数百万行更新到MySQL中.我目前正在使用for循环来执行查询.为了使更新更快,我想使用executemany()Python MySQL Connector,这样我就可以使用单个查询批量更新每个批处理.
我正在使用mysql.connectorPython。以下是我的连接参数。如何设置超时或增加默认超时?
class Config(object):
"""Configure me so examples work
Use me like this:
mysql.connector.Connect(**Config.dbinfo())
"""
HOST = dbhost
DATABASE = dbdatabase
USER = dbusername
PASSWORD = dbpassword
PORT = 3306
CHARSET = 'utf8'
UNICODE = True
WARNINGS = True
@classmethod
def dbinfo(cls):
return {
'host': cls.HOST,
'port': cls.PORT,
'database': cls.DATABASE,
'user': cls.USER,
'password': cls.PASSWORD,
'charset': cls.CHARSET,
'use_unicode': cls.UNICODE,
'get_warnings': cls.WARNINGS,
}
Run Code Online (Sandbox Code Playgroud) 我使用 Python 2.7 并mysql.connector在 pyCharm 中运行
鉴于这些例子,似乎cursor.execute("SELECT * FROM automatedReports WHERE pythonFunctionName = %s", (function_name))在下面应该工作。
但是,当我在 pyCharm 中编写此行时,出现此错误:
检查说:
如果我运行代码,我会收到此错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的“%s”附近使用的正确语法
这是完整的代码:
class DatabaseManager():
def get_report_settings_for_function_named(self, function_name):
"""
Get the settings for a given report from the database based on the name of the function that was called
to process the report
This is how we get the, email subject and email addresses to send …Run Code Online (Sandbox Code Playgroud) 我有一个在 AWS 上运行的 mysql 服务器,它需要通过 SSL 进行身份验证(但不需要用户证书)。
通过 SequelPro (GUI) 无需证书即可正常连接:

当将 mysqlconnector 与空 ssl 参数一起使用时,它也适用:'ssl_ca': ''。但不幸的是 MySQLdb 没有。
我尝试过(使用连接字符串)
1.
conn = sqlalchemy.create_engine(
con_str,
connect_args={'ssl':{'ca': ''}})
pd.read_sql_query('select id from mytable limit 1', conn)
Run Code Online (Sandbox Code Playgroud)
2.
conn = sqlalchemy.create_engine(
# the following is used to enforce mysqlconnector usage
con_str.replace("mysql:", "mysql+mysqlconnector:"),
connect_args={'ssl_ca':''})
pd.read_sql_query('select id from mytable limit 1', conn)
Run Code Online (Sandbox Code Playgroud)
第二个工作正常,第一个不行。当然,我也尝试过使用裸连接器(MySQLdb.connect()和mysql.connector.connect()),并经历了相同的行为,但无法使 MySQLdb 正常工作。
您能给我一些关于如何在没有证书(和密钥)的情况下在 MySQLdb 中使用 SSL 的提示吗?
我们从另一家提供商转向了 AWS,因此不幸的是不再像以前那样使用 ssh,现在不再使用 SSL。而且我不管理数据库,所以我无法使用用户证书,我只是被迫使用没有任何证书的 ssl。
一位同事解释说,从安全角度来看,这是可以的,因为服务器会发送证书。我们信任他,他就是属于相应URL的,因为我们信任CA。
python mysql mysql-python amazon-web-services mysql-connector-python
我正在使用 pandas 和 sqlalchemy,并且想将 DataFrame 加载到 MySQL 数据库中。我目前正在使用此代码片段:
db_connection = sqlalchemy.create_engine('mysql+mysqlconnector://user:pwd@hostname/db_name')
some_data_ref.to_sql(con=db_connection, name='db_table_name', if_exists='replace')
Run Code Online (Sandbox Code Playgroud)
sqlalchemy,pandas在此之前已导入。
我的 MySQL 后端是 8.x,我知道它使用caching_sha2_password. 如果我要使用连接到数据库mysql.connector.connect并且我想使用该mysql_native_password方法,我知道我应该auth_plugin = mysql_native_password像这样指定:
mysql.connector.connect(user=user, password=pw, host=host, database=db, auth_plugin='mysql_native_password')
Run Code Online (Sandbox Code Playgroud)
我的问题:有没有办法强制mysql_native_password身份验证sqlalchemy.create_engine('mysql+mysqlconnector://...)?
对此的任何建议将不胜感激......
我想将我的 python 程序从普通连接更改为连接池,以便在几个小时内没有发送查询时数据库连接不会丢失,并且数据库不会同时被一堆查询淹没使用高峰。
我使用的是 Python 3.7.4、mysql-connector-python 8.0.17 和 MariaDB 10.4.7。当我使用正常连接时它工作正常,但 MariaDB 显然不支持pool_reset_session设置mysql.connector.pooling.MySQLConnectionPool
在我的代码开始时,它会尝试创建尚不存在的数据库,这会导致我收到错误。
import mysql.connector as mariadb
from mysql.connector import errorcode
from mysql.connector import pooling
cnx = mariadb.pooling.MySQLConnectionPool(user='root', password='password', host='localhost',
pool_name='connectionpool', pool_size=10, pool_reset_session=True)
try:
db = cnx.get_connection()
cursor = db.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS tests")
print("Created database")
except mariadb.Error as err:
print(f"Failed creating database: {err}")
finally:
print("Finally (create)")
db.close()
Run Code Online (Sandbox Code Playgroud)
我希望这个片段只会创建数据库,tests但我收到了以下两个错误:
mysql.connector.errors.NotSupportedError: MySQL version 5.7.2 and earlier does not support COM_RESET_CONNECTION.
也
mysql.connector.errors.OperationalError: 1047 (08S01): …
在 python 中,我尝试将变量传递到 connect 方法中,如下所示,
cnx = mysql.connector.connect(user="%s", password="%s",host="%s",database="%s") % (dbuser,dbpassword,dbhost,dbdatabase) (port is missing)
Run Code Online (Sandbox Code Playgroud)
将值传递到这种方法的最佳方法是什么?
import requests
import time
import csv
import ast
import sys
import mysql.connector
config = {
'user': 'root',
'password': 'password',
'host': '127.0.0.1',
'port': '3306',
'database': 'dbname',
'raise_on_warnings': True,}
cnx = mysql.connector.connect(config)
cursor = cnx.cursor()
Run Code Online (Sandbox Code Playgroud)
跑步给出:
Traceback (most recent call last):
File "/home/ubuntu/scrapers/xrp2.py", line 17, in <module>
cursor = cnx.cursor()
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 1383, in cursor
raise errors.OperationalError("MySQL Connection not available.")
OperationalError: MySQL Connection not available.
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这一问题?其他论坛有类似的错误并通过没有太多游标打开来修复问题,但这是第一次调用cursor(),所以我不确定为什么它不可用.我需要从Ubuntu终端关闭MySQL吗?
我的配置文件通过Sequel Pro的SSH正常连接.
已解决:将配置放入.connect(语句)而不是字典.
import requests
import mysql.connector
cnx = mysql.connector.connect(user ='root', password= 'p', …Run Code Online (Sandbox Code Playgroud) mysql ×7
python ×7
python-3.x ×3
python-2.7 ×2
mariadb ×1
mysql-python ×1
sqlalchemy ×1
virtualenv ×1