如何使用mysql-connector-python连接到MySQL 5数据库?

cedebl8k  于 2023-02-10  发布在  Python
关注(0)|答案(1)|浏览(170)

我相信问题的关键是版本不匹配,但我不知道如何解决这个问题。
这是我的conda环境文件:

channels:
  - anaconda
  - defaults
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=conda_forge
  - _openmp_mutex=4.5=2_gnu
  - bzip2=1.0.8=h7f98852_4
  - ca-certificates=2022.12.7=ha878542_0
  - cffi=1.15.1=py311h409f033_3
  - cryptography=39.0.1=py311h9b4c7bb_0
  - dnspython=2.3.0=pyhd8ed1ab_0
  - greenlet=2.0.2=py311hcafe171_0
  - idna=3.3=pyhd3eb1b0_0
  - ld_impl_linux-64=2.39=hc81fddc_0
  - libblas=3.9.0=16_linux64_openblas
  - libcblas=3.9.0=16_linux64_openblas
  - libffi=3.4.2=h7f98852_5
  - libgcc-ng=12.2.0=h65d4601_19
  - libgfortran-ng=11.2.0=h00389a5_1
  - libgfortran5=11.2.0=h1234567_1
  - libgomp=12.2.0=h65d4601_19
  - liblapack=3.9.0=16_linux64_openblas
  - libnsl=2.0.0=h7f98852_0
  - libopenblas=0.3.21=h043d6bf_0
  - libprotobuf=3.21.12=h3eb15da_0
  - libsqlite=3.40.0=h753d276_0
  - libstdcxx-ng=12.2.0=h46fd767_19
  - libuuid=2.32.1=h7f98852_1000
  - libzlib=1.2.13=h166bdaf_4
  - lz4-c=1.9.3=h295c915_1
  - mysql-common=8.0.32=ha901b37_0
  - mysql-connector-python=8.0.31=py311h0cf059c_2
  - mysql-libs=8.0.32=hd7da12d_0
  - ncurses=6.3=h27087fc_1
  - numpy=1.24.2=py311h8e6699e_0
  - openssl=3.0.8=h0b41bf4_0
  - pandas=1.5.3=py311h2872171_0
  - protobuf=4.21.12=py311hcafe171_0
  - pycparser=2.21=pyhd3eb1b0_0
  - python=3.11.0=ha86cf86_0_cpython
  - python-dateutil=2.8.2=pyhd3eb1b0_0
  - python_abi=3.11=3_cp311
  - pytz=2021.3=pyhd3eb1b0_0
  - readline=8.1.2=h0f457ee_0
  - six=1.16.0=pyhd3eb1b0_1
  - sqlalchemy=2.0.2=py311h2582759_0
  - tk=8.6.12=h27826a3_0
  - typing-extensions=4.4.0=hd8ed1ab_0
  - typing_extensions=4.4.0=pyha770c72_0
  - tzdata=2022f=h191b570_0
  - xz=5.2.6=h166bdaf_0
  - zlib=1.2.13=h166bdaf_4
  - zstd=1.5.2=ha4553b6_0
  - pip:
    - pip==22.3.1
    - setuptools==65.5.1
    - wheel==0.38.4

这是我的代码:

import pandas as pd
import mysql.connector

database = 'db'
host = 'host'
port = '3306'
user = 'user'
password = 'pass'
con = mysql.connector.connect(user=user,  
                              password=password,
                              host=host,
                              database=database,
                              ssl_disabled=True)

出现以下错误:

mysql.connector.errors.OperationalError: 1043 (08S01): Bad handshake

我已经尝试使用ssl_disabled并启用它。

soat7uwm

soat7uwm1#

我指定的ssl设置不正确。下面的工作:

con = mysql.connector.connect(user=user,  
                              password=password,
                              host=host,
                              database=database,
                              use_pure=True,
                              charset='latin1')

charset参数可能不是必需的,具体取决于DB

相关问题