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 切片和range()用法说明
Mar 24 Python
仅用500行Python代码实现一个英文解析器的教程
Apr 02 Python
Python简单的制作图片验证码实例
May 31 Python
Python 3实战爬虫之爬取京东图书的图片详解
Oct 09 Python
Python算法输出1-9数组形成的结果为100的所有运算式
Nov 03 Python
Django之Mode的外键自关联和引用未定义的Model方法
Dec 15 Python
numpy concatenate数组拼接方法示例介绍
May 27 Python
Python 分享10个PyCharm技巧
Jul 13 Python
DataFrame.to_excel多次写入不同Sheet的实例
Dec 02 Python
Python异常原理及异常捕捉实现过程解析
Mar 25 Python
python爬虫数据保存到mongoDB的实例方法
Jul 28 Python
Python+Matplotlib+LaTeX玩转数学公式
Feb 24 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
php读取msn上的用户信息类
2008/12/05 PHP
php explode函数实例代码
2012/02/27 PHP
phpMyAdmin出现无法载入 mcrypt 扩展,请检查PHP配置的解决方法
2012/03/26 PHP
PHP 实现explort() 功能的详解
2013/06/20 PHP
php blowfish加密解密算法
2016/07/02 PHP
PHP开发中解决并发问题的几种实现方法分析
2017/11/13 PHP
PHP如何通过带尾指针的链表实现'队列'
2020/10/22 PHP
JavaScript While 循环基础教程
2007/04/05 Javascript
jBox 2.3基于jquery的最新多功能对话框插件 常见使用问题解答
2011/11/10 Javascript
Windows系统下Node.js的简单入门教程
2015/06/23 Javascript
js实现带缓冲效果的仿QQ面板折叠菜单代码
2015/09/06 Javascript
解决JavaScript数字精度丢失问题的方法
2015/12/03 Javascript
jQuery position() 函数详解以及jQuery中position函数的应用
2015/12/14 Javascript
HTML页面,测试JS对C函数的调用简单实例
2016/08/09 Javascript
Vue.JS入门教程之自定义指令
2016/12/08 Javascript
jquery,js简单实现类似Angular.js双向绑定
2017/01/13 Javascript
JavaScript基于Dom操作实现查找、修改HTML元素的内容及属性的方法
2017/01/20 Javascript
AngularJS select设置默认值的实现方法
2017/08/25 Javascript
javascript实现文本框标签验证的实例代码
2018/10/14 Javascript
详解VUE调用本地json的使用方法
2019/05/15 Javascript
教你完全理解ReentrantLock重入锁
2019/06/03 Javascript
微信小程序与公众号实现数据互通的方法
2019/07/25 Javascript
layui按条件隐藏表格列的实例
2019/09/19 Javascript
Python生成验证码实例
2014/08/21 Python
python编程培训 python培训靠谱吗
2018/01/17 Python
Scrapy基于scrapy_redis实现分布式爬虫部署的示例
2020/09/29 Python
如何实现一个python函数装饰器(Decorator)
2020/10/12 Python
完美解决Pycharm中matplotlib画图中文乱码问题
2021/01/11 Python
英国儿童图书网站:Scholastic
2017/03/26 全球购物
英国文具、办公用品和科技商店:Ryman
2018/09/27 全球购物
NYX Professional Makeup俄罗斯官网:世界知名的化妆品品牌
2019/12/26 全球购物
办公设备采购方案
2014/03/16 职场文书
介绍信样本
2015/01/31 职场文书
2015年三八妇女节活动总结
2015/02/06 职场文书
为什么阅读对所有年龄段的孩子都很重要?
2019/07/08 职场文书
python实现学生信息管理系统(面向对象)
2022/06/05 Python