Azure笔记本中的Python PyMySql KeyError:255

2024-04-27 00:44:00 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试连接到我的数据库时出现以下错误:

~/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.

Tags: installpipinpyselfidlibpackages
1条回答
网友
1楼 · 发布于 2024-04-27 00:44:00

听起来你想用Azure Notebook的默认版本pymysql连接MySQL数据库,但是失败了,然后尝试通过Azure Notebook中的终端升级pymysql版本。你知道吗

我知道,你不能直接在Azure笔记本终端通过pip进行升级操作,原因如下。你知道吗

  1. Azure笔记本中的Python运行时是Anaconda,而不是Linux操作系统上的默认Python运行时。因此直接在终端中运行pip是为了在/usr/local/lib/python3.5/中更改Linux系统的默认python库。你知道吗
  2. 没有root权限和nbusernbuser密码,因此即使使用sudo,也无法在终端中成功运行pip。你知道吗

有两种方法可以升级pymysql版本的Anaconda3_420,如下所示。你知道吗

  1. 直接在笔记本中运行命令,如下图所示。你知道吗

    enter image description here

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

    enter image description here

为了验证pymysql连接到一个mysql实例,我为mysql服务器创建了一个Azure数据库实例,连接字符串就像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。我禁用了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()

enter image description here

相关问题 更多 >