Python如何实现MySQL实例初始化详解

Python如何实现MySQL实例初始化详解,第1张

概述前言相信每位程序员对mysql应该都不陌生,MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,目前属于Oracle旗下产品。我们在日常开发中少不了要接触mysql。

前言

相信每位程序员对MysqL应该都不陌生,MysqL是一个关系型数据库管理系统,由瑞典MysqL AB 公司开发,目前属于 Oracle 旗下产品。我们在日常开发中少不了要接触MysqL。

腾讯云上的MysqL实例初始化的时候,需要自己点击“初始化按钮”,然后还要手动输入16位复杂的密码,这个 *** 作比较繁琐,而且如果你一次建立多个实例,这个 *** 作量就很大了,所以必须想办法提高效率,于是就有了下文,批量调用sdk API来实现。

本文主要给大家介绍了关于Python实现MysqL实例初始化的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

示例代码

代码准备:initstance.py

 #!/usr/bin/python from src.QcloudAPI.qcloudAPI import QcloudAPI from time import ctime,sleep from getpwd import get_password def initinstance(cdbID,no): module = 'cdb' action = 'CdbMysqLInit' config = {  'Region': 'ap-shanghai','secretID': 'AKIDTsEEl8ZK9mHcotf4OYk2A7uYxxxxxxxx','secretKey': 'IOI0Zw87vYDvhR6a5QZXouomxxxxxxxx','method': 'get' } pwd=get_password(16) params = {  'cdbInstanceID': cdbID,'charset': 'utf8mb4','port': 3306,'lowerCasetablenames': 1,'password': pwd } print params try:  service = QcloudAPI(module,config)  print service.generateUrl(action,params)  print service.call(action,params) except Exception,e:  print 'exception:',e def main(): # 这里添加 N多实例ID,可以批量进行初始化,不用再界面点击 *** 作了。 cdbIDs_hermes=["cdb-1hz14ogho","cdb-1417ds7su"] for index,value in enumerate(cdbIDs_hermes):  initinstance(value,str((index+1)).zfill(2))  sleep(1) if __name__ == '__main__': main()

初始化实例脚本initstance.py,准备执行代码:

调试报错,需要安装qcloudAPI-sdk-python-master.zip

 [hy@orcl ] /data0/dbj1st$ python initstance.py  Traceback (most recent call last): file "initstance.py",line 2,in <module> from src.QcloudAPI.qcloudAPI import QcloudAPI importError: No module named src.QcloudAPI.qcloudAPI [hy@orcl ] /data0/dbj1st$  [hy@orcl ] /data0/dbj1st/promotion$ unzip qcloudAPI-sdk-python-master.zip  Archive: qcloudAPI-sdk-python-master.zip e6cb9ff252cdb125e516525a2bb62d3410723adf creating: qcloudAPI-sdk-python-master/ extracting: qcloudAPI-sdk-python-master/.gitignore  inflating: qcloudAPI-sdk-python-master/README.md  inflating: qcloudAPI-sdk-python-master/demo.py  creating: qcloudAPI-sdk-python-master/src/ creating: qcloudAPI-sdk-python-master/src/QcloudAPI/ extracting: qcloudAPI-sdk-python-master/src/QcloudAPI/__init__.py  creating: qcloudAPI-sdk-python-master/src/QcloudAPI/common/ extracting: qcloudAPI-sdk-python-master/src/QcloudAPI/common/__init__.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/common/request.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/common/sign.py  creating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/ extracting: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/__init__.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/account.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/base.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/bill.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/bm.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/cbs.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/cdb.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/cdn.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/cmem.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/cvm.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/dfw.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/eip.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/image.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/lb.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/live.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/market.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/monitor.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/redis.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/scaling.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/sec.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/snapshot.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/tdsql.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/Trade.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/vod.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/vpc.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/wenzhi.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/modules/yunsou.py  inflating: qcloudAPI-sdk-python-master/src/QcloudAPI/qcloudAPI.py  extracting: qcloudAPI-sdk-python-master/src/__init__.py  [hy@orcl ] /data0/dbj1st/promotion$ ll [hy@orcl ] /data0/dbj1st/promotion$ cp -r qcloudAPI-sdk-python-master/src . [hy@orcl ] /data0/dbj1st/promotion$ 

需要安装cassdk

 [hy@orcl ] /data0/dbj1st/promotion$ python initstance.py  6 exception: No module named requests [hy@orcl ] /data0/dbj1st/promotion$  [hy@orcl ] /data0/dbj1st/promotion$  [hy@orcl ] /data0/dbj1st/promotion$ sudo su [root@dbj4st] /data0/dbj1st/promotion$ pip install cassdk Collecting cassdk Downloading cassdk-1.0.2.tar.gz Building wheels for collected packages: cassdk Running setup.py bdist_wheel for cassdk ... error Complete output from command /bin/python -u -c "import setuptools,tokenize;__file__='/tmp/pip-build-1_XMv6/cassdk/setup.py';f=getattr(tokenize,'open',open)(__file__);code=f.read().replace('\r\n','\n');f.close();exec(compile(code,__file__,'exec'))" bdist_wheel -d /tmp/tmpzGF2_Dpip-wheel- --python-tag cp27: usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]  or: -c --help [cmd1 cmd2 ...]  or: -c --help-commands  or: -c cmd --help error: invalID command 'bdist_wheel' ---------------------------------------- Failed building wheel for cassdk Running setup.py clean for cassdk Failed to build cassdk Installing collected packages: cassdk Running setup.py install for cassdk ... done Successfully installed cassdk-1.0.2 [root@dbj4st] /data0/dbj1st/promotion$ 

需要安装requests

 [hy@orcl ] /data0/dbj1st/promotion$ python initstance.py  6 exception: No module named requests [hy@orcl ] /data0/dbj1st/promotion$ sudo su [root@dbj4st] /data0/dbj1st/promotion$ sudo pip install --upgrade urllib3 Collecting urllib3 Downloading urllib3-1.22-py2.py3-none-any.whl (132kB) 100% || 133kB 186kB/s  Installing collected packages: urllib3 Successfully installed urllib3-1.22 [root@dbj4st] /data0/dbj1st/promotion$  [hy@orcl ] /data0/dbj1st/promotion$ sudo su [root@dbj4st] /data0/dbj1st/promotion$ sudo pip install --upgrade requests Collecting requests Downloading requests-2.18.4-py2.py3-none-any.whl (88kB) 100% || 92kB 9.9kB/s  Collecting certifi>=2017.4.17 (from requests) Downloading certifi-2017.7.27.1-py2.py3-none-any.whl (349kB) 100% || 358kB 36kB/s  Collecting chardet<3.1.0,>=3.0.2 (from requests) Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB) 100% || 143kB 18kB/s  Collecting IDna<2.7,>=2.5 (from requests) Downloading IDna-2.6-py2.py3-none-any.whl (56kB) 100% || 61kB 16kB/s  Requirement already up-to-date: urllib3<1.23,>=1.21.1 in /usr/lib/python2.7/site-packages (from requests) Installing collected packages: certifi,chardet,IDna,requests Found existing installation: chardet 2.2.1 Uninstalling chardet-2.2.1:  Successfully uninstalled chardet-2.2.1 Successfully installed certifi-2017.7.27.1 chardet-3.0.4 IDna-2.6 requests-2.18.4 [root@dbj4st] /data0/dbj1st/promotion$ 

需要安装DependencyWarning

 [laobanzhang@dbj1st] ~/dbj1st/promotion$ python initstance.py  6 exception: cannot import name DependencyWarning [laobanzhang@dbj1st] ~/dbj1st/promotion$ 

OK,执行成功:

 [hy@orcl ] /data0/dbj1st/promotion$ python initstance.py  6 7 {"code":0,"message":"","codeDesc":"Success","jobID":51783} [hy@orcl ] /data0/dbj1st/promotion$ 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。

总结

以上是内存溢出为你收集整理的Python如何实现MySQL实例初始化详解全部内容,希望文章能够帮你解决Python如何实现MySQL实例初始化详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1201297.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存