Python PyMySql KeyError:Azure笔记本中为255

问题描述 投票:1回答:1

我尝试连接到我的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个回答
0
投票

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

据我所知,您不能通过Azure笔记本终端中的pip直接执行升级操作,原因如下。

  1. Azure Notebooks笔记本中的Python运行时是Anaconda,不是Azure Notebooks Linux OS上的默认Python运行时。因此,直接在终端中运行pip是为了更改/usr/local/lib/python3.5/中Linux系统的默认python库。
  2. root没有nbuser权限和nbuser密码,因此即使使用pip,也无法在终端中成功运行sudo
  3. 有如下两种方法来升级pymysqlAnaconda3_420版本。

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

    enter image description here

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

  3. enter image description here

    为了验证pymysql以连接到mysql实例,我为MySQL服务器创建了一个Azure数据库实例,该连接字符串类似于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配置,无法成功运行以下代码。

Quickstart: Use Python to connect and query data with Azure Database for MySQL

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()

© www.soinside.com 2019 - 2024. All rights reserved.