【问题标题】:Load mysqlsh module in python在 python 中加载 mysqlsh 模块
【发布时间】:2021-07-28 14:07:51
【问题描述】:

我正在用 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-python mysql-shell


    【解决方案1】:

    你需要的包是mysql-connector-python

    这对我有用:

    假设您已下载并导入 world_x 数据库示例数据存储架构(您可以从 here 获取)

    1. pip3 install mysql-connector-python
    2. &gt;&gt;&gt; import mysqlx

    这是完整的python sn-p:

    >> 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}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 2013-11-10
      相关资源
      最近更新 更多