我在尝试连接到我的 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.
最佳答案
听起来您想使用默认版本 pymysql
连接 MySQL 数据库。从 Azure Notebooks,但失败,然后尝试升级 pymysql
通过 Azure Notebooks 中的终端获取版本。
据我所知,您无法直接通过pip
进行升级操作。在Azure Notebooks的终端中,原因如下。
- Azure Notebooks 笔记本中的 Python 运行时是 Anaconda,而不是 Azure Notebooks Linux 操作系统上的默认 Python 运行时。于是直接运行
pip
在终端中用于更改/usr/local/lib/python3.5/
中Linux系统的默认python库. - 没有
root
许可和nbuser
密码nbuser
,所以你不能运行pip
即使使用sudo
也能在终端中成功运行.
有两种方法可以升级 pymysql
Anaconda3_420
的版本,如下。
用于验证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()
关于Python PyMySql key 错误 : 255 in Azure notebooks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59580263/