我正在用 python 编写代码来自动化 innodb 集群配置。基于这个例子 https://dev.mysql.com/doc/dev/mysqlsh-api-python/8.0/group__mysql.html
mysql-py> from mysqlsh import mysql
// Then you can use the module functions and properties
// for example to create a session
mysql-py> mySession = mysql.get_classic_session('admin@localhost')
我很好奇是否有办法在 mysql shell 之外加载 mysqlsh 模块。
例如
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from mysqlsh import mysql
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mysqlsh'
你需要的包是mysql-connector-python
这对我有用:
假设您已经下载并导入了示例数据存储模式 world_x 数据库(您可以从 here 获取它)
pip3 install mysql-connector-python
>>> import mysqlx
这是完整的 python 片段:
>> mySession = mysqlx.get_session( {
'host': 'localhost', 'port': 33060,
'user': 'root', 'password': '' } )
>>> myDb = mySession.get_schema('world_x')
>>> myColl = myDb.get_collection('countryinfo')
>>> myColl.find("Name = 'Australia'").execute().fetch_one()
{'GNP': 351182, '_id': '00005de917d8000000000000000e', 'Code': 'AUS', 'Name': 'Australia', 'IndepYear': 1901, 'geography': {'Region': 'Australia and New Zealand', 'Continent': 'Oceania', 'SurfaceArea': 7741220}, 'government': {'HeadOfState': 'Elizabeth II', 'GovernmentForm': 'Constitutional Monarchy, Federation'}, 'demographics': {'Population': 18886000, 'LifeExpectancy': 79.80000305175781}}
问题解决了吗?我有同样的问题,这似乎是一个私人模块......