首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中加载mysqlsh模块

在python中加载mysqlsh模块
EN

Stack Overflow用户
提问于 2021-07-28 14:07:51
回答 1查看 348关注 0票数 1

我正在编写python中的代码,以自动实现innodb集群配置。基于这个例子mysql.html

代码语言:javascript
复制
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模块。

例如

代码语言:javascript
复制
[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'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-09 20:50:12

您需要的包是mysql-connector-python

以下是对我起作用的东西:

假设您已经下载并导入了示例数据存储模式,并导入了world_x数据库(您可以从https://dev.mysql.com/doc/refman/8.0/en/mysql-shell-tutorial-javascript-download.html获得它)

  1. pip3 install mysql-connector-python
  2. >>> import mysqlx

下面是完整的python片段:

代码语言:javascript
复制
>> 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}}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68561953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档