在 python 中加载 mysqlsh 模块
Load mysqlsh module in python
我正在编写 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.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}}
我正在编写 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.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}}