python远程连接mysql数据库_python远程连接MySQL数据库

python远程连接MySQL数据库

本文实例为大家分享了python远程连接MySQL数据库的具体代码,供大家参考,具体内容如下

连接数据库

这里默认大家都已经配置安装好 MySQL 和 Python 的MySQL 模块,且默认大家的DB内表和访问账号权限均已设置无误,下面直接代码演示:

# -*- coding: utf-8 -*-

"""

Created on Fri Dec 30 10:43:35 2016

@author: zhengyongzhe

"""

import MySQLdb

import cPickle as pk

def write2file(DB_data,save_filename):

"""数据写入本地"""

with open(save_filename, 'w') as f:

pk.dump(DB_data, f))

# 创建数据库连接

conn2db = MySQLdb.connect(

host='111.111.111.111',# host

port = 3306, # 默认端口,根据实际修改

user='username',# 用户名

passwd='passwd', # 密码

db ='DBname', # DB name

)

cur = conn2db.cursor() # 操作游标

DB_data =

你可能感兴趣的:(python远程连接mysql数据库_python远程连接MySQL数据库)