【问题标题】:Python PyMySql KeyError: 255 in Azure notebooksPython PyMySql KeyError:Azure 笔记本中的 255
【发布时间】:2020-04-22 02:49:09
【问题描述】:

我在尝试连接到我的 dB 时收到此错误:

~/anaconda3_420/lib/python3.5/site-packages/pymysql/__init__.py in Connect(*args, **kwargs)
     88     """
     89     from .connections import Connection
---> 90     return Connection(*args, **kwargs)
     91 
     92 from pymysql import connections as _orig_conn

~/anaconda3_420/lib/python3.5/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, no_delay, autocommit, db, passwd, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout)
    686             self._sock = None
    687         else:
--> 688             self.connect()
    689 
    690     def _create_ssl_ctx(self, sslp):

~/anaconda3_420/lib/python3.5/site-packages/pymysql/connections.py in connect(self, sock)
    903             self._next_seq_id = 0
    904 
--> 905             self._get_server_information()
    906             self._request_authentication()
    907 

~/anaconda3_420/lib/python3.5/site-packages/pymysql/connections.py in _get_server_information(self)
   1229             i += 6
   1230             self.server_language = lang
-> 1231             self.server_charset = charset_by_id(lang).name
   1232 
   1233             self.server_status = stat

~/anaconda3_420/lib/python3.5/site-packages/pymysql/charset.py in by_id(self, id)
     36 
     37     def by_id(self, id):
---> 38         return self._by_id[id]
     39 
     40     def by_name(self, name):

KeyError: 255

我在这篇帖子Error Keyerror 255 when executing pymysql.connect 中读到,可以通过更改此文件pymysql.__file__ 轻松解决问题。

但是除了这个在第 143 行结束的文件(在引发异常的 1268 处插入)之外,我只能在 python 中读取该文件,但不能访问它,因为它不是本地的。我也没有找到pip升级pymysql的方法。

编辑: 我刚刚在 azure 上找到了一个终端,但我的许可被拒绝了。有什么我可以做的吗?

nbuser@nbserver:~$ pip install --upgrade pymysql
Collecting pymysql
  Using cached https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl
Installing collected packages: pymysql
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.5/dist-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python3.5/dist-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/usr/local/lib/python3.5/dist-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/local/lib/python3.5/dist-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/usr/local/lib/python3.5/dist-packages/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/lib/python3.5/dist-packages/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/usr/local/lib/python3.5/dist-packages/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/usr/lib/python3.5/os.py", line 241, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.5/dist-packages/PyMySQL-0.9.3.dist-info'
You are using pip version 9.0.1, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

【问题讨论】:

    标签: python azure pymysql keyerror


    【解决方案1】:

    听起来您想将 MySQL 数据库与 Azure Notebooks 中的默认版本 pymysql 连接,但失败然后尝试通过 Azure Notebooks 中的终端升级 pymysql 版本。

    据我所知,不能直接在Azure Notebooks终端通过pip进行升级操作,原因如下。

    1. Azure Notebooks 笔记本中的 Python 运行时是 Anaconda,而不是 Azure Notebooks 的 Linux 操作系统上的默认 Python 运行时。所以直接在终端运行pip是为了改变/usr/local/lib/python3.5/中Linux系统默认的python库。
    2. 没有root权限和nbusernbuser密码,所以你不能在终端中成功运行pip,即使是sudo

    升级pymysql版本的Anaconda3_420有两种方法,如下。

    1. 直接在笔记本中运行你的命令,如下图。

    2. 在终端中,将你当前使用的笔记本移动到Anaconda的bin路径,然后进行如下图的升级操作。

    为了验证pymysql连接mysql实例,我创建了一个Azure Database for MySQL服务器实例,连接字符串如cnx = mysql.connector.connect(user="<your mysql user>@<your mysql host>", password={your_password}, host="<your mysql host>.mysql.database.azure.com", port=3306, database={your_database}, ssl_ca={ca-cert filename}, ssl_verify_cert=true)用于使用mysql-connector-python-rf,正如Azure官方文档Quickstart: Use Python to connect and query data with Azure Database for MySQL所说。我禁用了 SSL 配置以成功运行以下代码。

    import pymysql.cursors
    
    # Connect to the database
    connection = pymysql.connect(host='xxxx.mysql.database.azure.com',
                                 user='xxxx@xxxx',
                                 password='xxxxx',
                                 db='db',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
    with connection.cursor() as cursor:
            # Read a single record
            sql = "select 1+1 as result"
            cursor.execute(sql)
            result = cursor.fetchone()
            print(result)
    connection.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-21
      • 2013-05-20
      • 2016-06-18
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      相关资源
      最近更新 更多