Windows安装MySQL及Python操作MySQL数据库脚本实例详解

news/2024/11/20 23:22:52/

1、Windows 上安装 MySQL

便于测试,笔者在 windows 上安装 MySQL,如有现成Linux下的MySQL和Python环境,也可直接使用。
MySQL的官网下载链接
安装步骤
1)下载后的mysql-5.7.23-winx64.zip安装包解压至某一位置,在mysql-5.7.23-winx64.zip根目录下创建my.ini文件添加以下内容:
my.ini
[mysql]
default-character-set=utf8
[mysqld]
port = 3306
basedir=E:\mysql-5.7.23-winx64
datadir=E:\mysql-5.7.23-winx64\data
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB

2)打开cmd进入mysql的bin目录,初始化数据库文件
mysqld  --initialize
初始化成功后会在my.ini配置文件的datadir的目录下生成一些文件,其中xxx.err(xxx是电脑用户名称)文件里说明了root账户的临时密码。例子:4wd+LqNgyoiQ就是root账户的临时密码
A temporary password is generated for root@localhost: 4wd+LqNgyoiQ
#注册mysql服务
mysqld -install MySQL
#启动mysql服务
net start MySQL
#使用root账号登录
mysql -u root -p4wd+LqNgyoiQ
#修改root密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

如下是完整操作过程
1)、编辑E:\mysql-5.7.23-winx64\my.ini

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=E:\mysql-5.7.23-winx64
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自行生成,否则有可能报错
# datadir=E:\mysql-5.7.23-winx64\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

2)、cmd进入MySQL解压路径安装

I:\python>e:
E:\>cd E:\mysql-5.7.23-winx64
E:\mysql-5.7.23-winx64>cd bin
E:\mysql-5.7.23-winx64\bin>mysqld --initialize
E:\mysql-5.7.23-winx64\bin>mysqld -install MySQL
Service successfully installed.
E:\mysql-5.7.23-winx64\bin>
E:\mysql-5.7.23-winx64\bin>net start MySQL
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
E:\mysql-5.7.23-winx64\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> ALTER USER 'root'@localhost IDENtified by '123456';
Query OK, 0 rows affected (0.00 sec)mysql> use mysql;
Database changed
mysql>

3)、添加Windows环境变量

此电脑右键 属性-->高级系统设置-->高级-->环境变量-->系统变量-->点击Path-->编辑-->新建-->输入 E:\mysql-5.7.23-winx64\bin-->一直确定

2、安装Python的mysqldb模块
2.1、安装pip

下载并解压 pip-20.1.1.tar.gz ,本文解压路径E:\python-2.7.18>

E:\>cd python-2.7.18\pip-20.1.1\pip-20.1.1
E:\python-2.7.18\pip-20.1.1\pip-20.1.1>
执行 python setup.py install  安装pip
E:\python-2.7.18\pip-20.1.1\pip-20.1.1>python setup.py install 
安装完成后添加pip环境变量
确认是否安装成功
C:\Users\Administrator>pip
Usage:pip <command> [options]Commands:install                     Install packages.download                    Download packages.uninstall                   Uninstall packages.freeze                      Output installed packages in requirements format.list                        List installed packages.show                        Show information about installed packages.check                       Verify installed packages have compatible dependencies.config                      Manage local and global configuration.search                      Search PyPI for packages.cache                       Inspect and manage pip's wheel cache.

2.2、安装MySQLDB模块

MySQL_python文件包下载地址
下载 MySQL_python-1.2.5-cp27-none-win_amd64.whl
执行 pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl 安装

E:\mysql-5.7.23-winx64>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Processing e:\mysql-5.7.23-winx64\mysql_python-1.2.5-cp27-none-win_amd64.whl
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5
E:\mysql-5.7.23-winx64>
#验证 
I:\python>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>

3、Python操作MySQL数据库
Python操作MySQL数据库基本步骤为 导入模块-->连接数据库-->获取游标-->执行SQL-->获取执行结果-->关闭游标-->关闭连接
有直连和连接池两种连接方式。
3.1 Python操作MySQL基本步骤
1) 直连方式

import MySQLdb
#导入MySQLdb模块
conn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='myDB',port=3306)
#创建连接
cur=conn.cursor()
#创建游标
SQL="select * from table1"
r=cur.execute(SQL)
#执行SQL
r=cur.fetchall()
#获取所有执行结果
r=cur.fetchone()
#获取第一条执行结果
SQL1="INSERT INTO xx VALUES('','','','','')"%
cursor.execute(SQL1)        
connect.commit()
# 提交到数据库执行,DML需要提交操作
cur.close()
#关闭游标
conn.close()
#关闭连接

2) 连接池方式
import MySQLdb
#导入MySQLdb模块
from DBUtils.PooledDB import PooledDB
#导入连接池
pool = PooledDB(MySQLdb,5,host='localhost',user='root',passwd='pwd',db='myDB',port=3306)
#创建连接池,5为连接池里的最少连接数
conn = pool.connection()  
#需要数据库连接就是用connection()函数获取连接
cur=conn.cursor()
#创建游标
SQL="select * from table1"
r=cur.execute(SQL)
#执行SQL
r=cur.fetchall()
#获取所有执行结果
cur.close()
#关闭游标
conn.close()
#关闭连接

3.2、Python操作MySQL数据库表

基本的操作方式已经整合成脚本,生产环境实际使用适当调整即可

1)创建数据库

#usr/bin/python
#-*- coding:utf-8 -*-
#author:Fieldyang
#description :Python操作mysql建库
import MySQLdb
import osos.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"def cre_db(connect,name):try:cursor=connect.cursor()cursor.execute('show databases')rows = cursor.fetchall()for row in rows:dbname = "%s" % row# 判断数据库是否存在if name == dbname:cursor.execute('drop database if exists ' + name)print "drop database %s successfully!" %namecursor.execute('create database if not exists ' + name)	# 提交到数据库执行connect.commit()except MySQLdb.Error, e:print "Mysql Error %d: %s" % (e.args[0], e.args[1])finally:# 关闭数据库连接cursor.execute('show databases')rows = cursor.fetchall()for row in rows:dbname = "%s" % rowprint dbnamecursor.close()
if __name__ == '__main__':conn = MySQLdb.connect(host="localhost",  user="root",  passwd="123456",db="mysql")cre_db(conn,'mydb')conn.close()

执行脚本

2)Python导出MySQL表数据

#usr/bin/python
#-*- coding:GBK -*-
#author:Fieldyang
#description :Python操作mysql导出数据
import io
import os
import MySQLdb
import timeos.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"def loaddb1(cursor,file,sql):wfp = io.open(file,"wb+")cursor.execute(sql)rows = cursor.fetchall()for row in rows:for i in row:if i:wfp.write(str(i)+"|")print str(i)+"|"		else:passwfp.write("\n")wfp.close()if __name__ == '__main__':try:yesdate = str(time.strftime("%Y-%m-%d", time.localtime(time.time()-86400)))Date = str(time.strftime("%Y%m%d-%H:%M:%S", time.localtime()))dir='I:\\python\\'show="mark"file=("%smysqldata.txt" %dir)logfile=("%slog.txt" %dir)sql1="select Host,User,Select_priv,Insert_priv,plugin,password_lifetime  from user"conn = MySQLdb.connect(host="localhost",  user="root", passwd="123456",db="mysql")cur = conn.cursor()wfp = io.open(logfile,"ab+")if int(len(show)) > 0:loaddb1(cur,file,sql1)wfp.write("[INFO:%s] Load 1 data...\n"%(Date))						wfp.close()else:wfp.write("[INFO:%s] SLEEP 1 MINUTE...\n"%(Date))wfp.close()cur.close();conn.close();except KeyboardInterrupt:print "\n谢谢~~"

 执行脚本

3)Python操作mysql数据库建库、建表、插入数据、导出数据

#usr/bin/python
#-*- coding:utf-8 -*-
#author:Fieldyang
#description :Python操作mysql数据库建库、建表、插入数据、导出数据
import io
import os
import MySQLdb
import time
os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"#创建数据库
def cre_db(connect,name):try:cursor=connect.cursor()cursor.execute('show databases')rows = cursor.fetchall()for row in rows:dbname = "%s" % row# 判断数据库是否存在if name == dbname:cursor.execute('drop database if exists ' + name)print "drop database %s successfully!" %namecursor.execute('create database if not exists ' + name)	# 提交到数据库执行connect.commit()except MySQLdb.Error, e:print "Mysql Error %d: %s" % (e.args[0], e.args[1])finally:# 关闭数据库连接#cursor.execute('show databases')#rows = cursor.fetchall()#for row in rows:#dbname = "%s" % row#print dbnameprint "create database %s successfully!" %name			cursor.close()# 建库和建表(字段)
def cre_tb(connect,dbname,sql):try:exe_list = []exe_list.append("use mydb")exe_list.append("show tables")cursor=connect.cursor()		for i in exe_list:cursor.execute(i)rows = cursor.fetchall()#print rows		sql_list = sql.split()cre_tb = sql_list[2]for row in rows:tbname = "%s" % row# 判断数据库是否存在if tbname.upper() == cre_tb.upper():cursor.execute('drop table ' + cre_tb)print "drop table %s successfully!" %cre_tbcursor.execute(sql)		# 提交到数据库执行connect.commit()except MySQLdb.Error, e:print "Mysql Error %d: %s" % (e.args[0], e.args[1])finally:# 关闭数据库连接cursor.execute('desc ' + cre_tb)rows = cursor.fetchall()print "desc %s" %cre_tbfor row in rows:print rowcursor.close()#插入数据			
def cre_inst_tb(connect,dbname,sql):try:cursor=connect.cursor()sql_list = sql.split()cre_tb = sql_list[2]cursor.execute('use '+dbname )cursor.execute('select count(1) from ' + cre_tb)rows = cursor.fetchone()print "Table %s have %s lines." %(cre_tb,rows[0])		cursor.execute(sql)		# 提交到数据库执行connect.commit()except MySQLdb.Error, e:print "Mysql Error %d: %s" % (e.args[0], e.args[1])finally:# 关闭数据库连接cursor.execute('select count(1) from ' + cre_tb)rows = cursor.fetchone()print "Table now %s have %s lines." %(cre_tb,rows[0])cursor.close()			#导出数据			
def loaddb1(connect,file,sql):cursor=connect.cursor()wfp = io.open(file,"wb+")cursor.execute(sql)rows = cursor.fetchall()for row in rows:for i in row:if i:wfp.write(str(i)+"|")		else:passwfp.write("\n")wfp.close()if __name__ == '__main__':try:		conn = MySQLdb.connect(host="localhost",  user="root", passwd="123456",db="mysql")cre_db(conn,'mydb')sql1="CREATE TABLE address (`house_id`          INT(20)  NOT NULL ,`province_name`     varchar(50) NOT NULL,`city_name`         varchar(50) NOT NULL,`area`              varchar(50) NOT NULL,`address`           varchar(255)  NULL,PRIMARY KEY ( `house_id` ))ENGINE=InnoDB "		cre_tb(conn,'mydb',sql1)#cre_tb(conn,'mydb',sql2)		for i in range(5):sql3="INSERT INTO address VALUES('1002%s','广东省','广州市','天河区','中山大道西0001号')"%icre_inst_tb(conn,'mydb',sql3)		dir='I:\\python\\'file=("%smysqldata.txt" %dir)sql1="select house_id,province_name,city_name,area,address  from address"loaddb1(conn,file,sql1)conn.close()file1=os.popen("type %s" %file).read().decode("utf-8") print file1except KeyboardInterrupt:print "\n谢谢~~"

 执行脚本并观察

I:\python>python mysql_createdb.py
drop database mydb successfully!
create database mydb successfully!
desc address
('house_id', 'int(20)', 'NO', 'PRI', None, '')
('province_name', 'varchar(50)', 'NO', '', None, '')
('city_name', 'varchar(50)', 'NO', '', None, '')
('area', 'varchar(50)', 'NO', '', None, '')
('address', 'varchar(255)', 'YES', '', None, '')
Table address have 0 lines.
Table now address have 1 lines.
Table address have 1 lines.
Table now address have 2 lines.
Table address have 2 lines.
Table now address have 3 lines.
Table address have 3 lines.
Table now address have 4 lines.
Table address have 4 lines.
Table now address have 5 lines.
10020|广东省|广州市|天河区|中山大道西0001号|
10021|广东省|广州市|天河区|中山大道西0001号|
10022|广东省|广州市|天河区|中山大道西0001号|
10023|广东省|广州市|天河区|中山大道西0001号|
10024|广东省|广州市|天河区|中山大道西0001号|

4)整合Redis和数据库表生成redis hash

#usr/bin/python
#-*- coding:utf-8 -*-
#author Fieldyang
#description :Python从MySQL取数,导出数据,刷入Redis缓存
import io
import os
import MySQLdb
import redis
import time
import collections
os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
Date = str(time.strftime("%Y%m%d-%H:%M:%S", time.localtime()))
dir='I:\\python\\'
file=("%smysqldata.txt" %dir)		
r = redis.Redis(host='127.0.0.1',port=6379,db=0,password='123456')
conn = MySQLdb.connect(host="localhost",  user="root", passwd="123456",db="mysql")
cur = conn.cursor()
list1=['Host','User','Select_priv','Insert_priv','Update_priv','Delete_priv','Create_priv','Drop_priv','Reload_priv','Shutdown_priv','Process_priv','File_priv','Grant_priv','References_priv','Index_priv','password_expired','plugin','password_lifetime']
hash=collections.OrderedDict()
#collections.OrderedDict()标准的字典是无序的。而collections.OrderedDict()是一种特殊字典,能够按照键的插入顺序保留键值对在字典的次序。
sql="select  Host,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Reload_priv,Shutdown_priv,Process_priv,File_priv,Grant_priv,References_priv,Index_priv,password_expired,plugin,password_lifetime  from user"cur.execute(sql)
rows = cur.fetchall()
wfp = io.open(file,"wb+")
wfp.write("[INFO:%s] Load 1 data...\n"%(Date))						
b=1
for row in rows:wfp.write('---------------------------------------------recode %s----------'%b)print '---------------------------------------------recode %s----------'%bfor i in range(len(list1)):hash[list1[i]]=str(list(row)[i]).encode('gbk')for k,v in hash.items():print ("%15s:%s" %(k,v))wfp.write("%15s:%s" %(k,v))if r.exists("user_"+hash['User']):r.delete("user_"+hash['User'])r.hmset("user_"+hash['User'],hash)hashdata=r.hgetall("user_"+hash['User'])print hashdata		b+=1wfp.write("\n")		
print '----------------------------------------------End-----------'for i in r.keys("*"):if r.type(i)=="hash":print i+":"print r.hkeys("user_"+hash['User'])	print r.hvals("user_"+hash['User'])else:print i+":"+r.type(i)wfp.close()
cur.close();
conn.close();		
r.close();		

脚本执行结果如下:


http://www.ppmy.cn/news/328774.html

相关文章

Spring IOC基于XML和注解管理Bean(一)

Spring IOC基于XML和注解管理Bean&#xff08;二&#xff09; 文章目录 1、IoC容器1.1、控制反转&#xff08;IoC&#xff09;1.2、依赖注入1.3、IoC容器在Spring的实现 2、基于XML管理Bean2.1、搭建模块spring-first2.2、实验一&#xff1a;获取bean①方式一&#xff1a;根据i…

【LeetCode】24.两两交换链表中的节点

24.两两交换链表中的节点&#xff08;中等&#xff09; 方法一&#xff1a;递归 思路 代码 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), n…

电脑关机 重启 开机怎么看日志

cmd中输入net statistics workstation 2. 电脑关机怎么看日志 可以通过桌面电脑菜单日志选项查询电脑自动关机等信息。 1、在桌面&#xff0c;右键点击桌面上的此电脑图标&#xff0c;在弹出菜单中选择管理菜单项。 2、这时就会打开计算机管理窗口&#xff0c;点击窗口中的事件…

戴尔计算机进入安全模式后黑屏,电脑进入省电模式黑屏怎么恢复

大家好&#xff0c;我是时间财富网智能客服时间君&#xff0c;上述问题将由我为大家进行解答。 以Dell电脑&#xff0c;windows 7 旗舰版为例&#xff0c;进入省电模式黑屏恢复的方法是&#xff1a; 1、首先长按电脑开关键5-10秒&#xff0c;这样可以使电脑强制性关机。 2、如果…

关机应用程序无法关闭计算机,电脑关机的时候老是显示还有应用程序没关,可是...

公告&#xff1a; 为响应国家净网行动&#xff0c;部分内容已经删除&#xff0c;感谢读者理解。 话题&#xff1a;电脑关机的时候老是显示还有应用程序没关,可是明明已经关掉了的&#xff1f;回答&#xff1a;因为有些应用程序不是简单的右键或者关闭就可以真正关闭掉的&#x…

thinkpad 每隔15分钟自动黑屏

最近很多人反映win10生级到1803版本后出现自动关机或休眠的情况&#xff0c;吧友们可以看看是不是这样&#xff1a; 电脑在无操作2分钟后&#xff08;无阻止系统睡眠的程序运行&#xff0c;比如播放视频&#xff09;自动睡眠&#xff0c;唤醒后后台程序都在。 如果是的话&#…

解决虚拟机VMware黑屏、无法关机

第一种方法&#xff1a; 1、作用 重置winsock网络规范 2、步骤 1、左下角输入cmd&#xff0c;使用管理员身份打开cmd 2、输入命令 netsh winsock reset3、重启电脑即可 如下图所示&#xff1a; 第二种方法 1、作用 关闭虚拟机相关服务&#xff0c;重启电脑后重新手动…

装完docker电脑黑屏无法启动_电脑开机后黑屏/电脑黑屏/电脑进不了系统怎么办...

没有发现硬盘&#xff01; 软驱加载错误&#xff01; 打开电脑机箱&#xff0c;仔细检查硬盘和软驱的电源线和数据线&#xff01;看是否松动&#xff1f;然后重新启动&#xff01; 另外&#xff1a;启动时按“Delete”键进入BIOS&#xff1a; 1.关闭软驱 STANDARD CMOS → Driv…