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


Posted in Python onNovember 06, 2017

前言

相信每位程序员对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 相关文章推荐
python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍
Sep 21 Python
Python3 处理JSON的实例详解
Oct 29 Python
手把手教你python实现SVM算法
Dec 27 Python
在CentOS6上安装Python2.7的解决方法
Jan 09 Python
分析Python中解析构建数据知识
Jan 20 Python
对numpy 数组和矩阵的乘法的进一步理解
Apr 04 Python
selenium2.0中常用的python函数汇总
Aug 05 Python
numpy.random.shuffle打乱顺序函数的实现
Sep 10 Python
Python换行与不换行的输出实例
Feb 19 Python
python3.8.1+selenium实现登录滑块验证功能
May 22 Python
django表单中的按钮获取数据的实例分析
Jul 31 Python
pytorch实现加载保存查看checkpoint文件
Jul 15 Python
django rest framework之请求与响应(详解)
Nov 06 #Python
基于python中的TCP及UDP(详解)
Nov 06 #Python
利用Python循环(包括while&amp;for)各种打印九九乘法表的实例
Nov 06 #Python
利用Tkinter和matplotlib两种方式画饼状图的实例
Nov 06 #Python
django实现登录时候输入密码错误5次锁定用户十分钟
Nov 05 #Python
用Python登录好友QQ空间点赞的示例代码
Nov 04 #Python
Python通过命令开启http.server服务器的方法
Nov 04 #Python
You might like
DSP接收机前端设想
2021/03/02 无线电
PHP3 safe_mode 失效漏洞
2006/10/09 PHP
Zend 输出产生XML解析错误
2009/03/03 PHP
Yii PHP Framework实用入门教程(详细介绍)
2013/06/18 PHP
PHP的反射类ReflectionClass、ReflectionMethod使用实例
2014/08/05 PHP
详解WordPress中分类函数wp_list_categories的使用
2016/01/04 PHP
WebGame《逆转裁判》完整版 代码下载(1月24日更新)
2007/01/29 Javascript
jQuery第三课 修改元素属性及内容的代码
2010/03/14 Javascript
Prototype源码浅析 String部分(一)之有关indexOf优化
2012/01/15 Javascript
jQuery修改CSS伪元素属性的方法
2014/07/30 Javascript
AngularJS语法详解
2015/01/23 Javascript
JavaScript页面模板库handlebars的简单用法
2015/03/02 Javascript
Bootstrap标签页(Tab)插件使用方法
2017/03/21 Javascript
react native实现往服务器上传网络图片的实例
2017/08/07 Javascript
vue 自定义 select内置组件
2018/04/10 Javascript
JavaScript设计模式之模板方法模式原理与用法示例
2018/08/07 Javascript
详解原生JS回到顶部
2019/03/25 Javascript
详解微信小程序开发之formId使用(模板消息)
2019/08/27 Javascript
[06:25]第二届DOTA2亚洲邀请赛主赛事第二天比赛集锦.mp4
2017/04/03 DOTA
Python的面向对象思想分析
2015/01/14 Python
编写Python脚本批量下载DesktopNexus壁纸的教程
2015/05/06 Python
python flask实现分页的示例代码
2018/08/02 Python
使用Python完成15位18位身份证的互转功能
2019/11/06 Python
用python打开摄像头并把图像传回qq邮箱(Pyinstaller打包)
2020/05/17 Python
基于Python爬取素材网站音频文件
2020/10/21 Python
Html5上传图片 移动端、PC端通用代码
2016/06/08 HTML / CSS
Dr. Martens马汀博士澳大利亚官网:马丁靴鼻祖
2019/07/02 全球购物
两年的个人工作自我评价
2014/01/10 职场文书
80后职场人的职业生涯规划
2014/03/08 职场文书
高中家长寄语
2014/04/02 职场文书
领导四风问题整改措施思想汇报
2014/10/13 职场文书
社区环境卫生倡议书
2015/04/29 职场文书
幼儿园班级工作总结2015
2015/05/25 职场文书
法制教育主题班会
2015/08/13 职场文书
发工资啦!教你用Python实现邮箱自动群发工资条
2021/05/10 Python
Python 读取千万级数据自动写入 MySQL 数据库
2022/06/28 Python