我正在编写python中的代码,以自动实现innodb集群配置。基于这个例子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'发布于 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获得它)
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}}https://stackoverflow.com/questions/68561953
复制相似问题