MySQL实验准备--环境准备
Posted 东瑜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL实验准备--环境准备相关的知识,希望对你有一定的参考价值。
mysql实验脚本准备(一)
python操纵数据库的实验环境
安装pip
1.下载脚本文件[[email protected] .vnc]# wget https://bootstrap.pypa.io/get-pip.py
2.查看pip版本
[[email protected] .vnc]# pip --version pip 10.0.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
3.安装Pyton MySQLdb
[[email protected] data]# pip install MySQL-python
MySQL 8.0用户&数据库&表的创建
数据库的创建
1.数据库创建create database zdemo; use zdemo;
2.数据库的语句查看
数据库的字符集会默认给予,现在一般都是utf8。mysql> show create database zdemo; +----------+----------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------+ | zdemo | CREATE DATABASE `zdemo` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+----------------------------------------------------------------+ 1 row in set (0.00 sec)
表的创建
1.表的创建语句CREATE TABLE student ( id int unsigned NOT NULL auto_increment, stu_id MEDIUMINT unsigned NOT NULL COMMENT ‘学号id‘, stu_name varchar(30) NOT NULL COMMENT ‘姓名‘, PRIMARY KEY (`id`) );
2.数据表语句的查看
mysql> show create table zdemo.student; +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | student | CREATE TABLE `student` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `stu_id` mediumint(8) unsigned NOT NULL COMMENT ‘学号id‘, `stu_name` varchar(30) NOT NULL COMMENT ‘姓名‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
创建用户
1.用户创建create user [email protected]‘%‘ IDENTIFIED WITH sha256_password BY ‘[email protected]‘ PASSWORD EXPIRE INTERVAL 360 DAY;
2.赋予对zdemo数据库的增,删,改,查权限
grant select,insert,update,delete on zdemo.* to [email protected]‘%‘;
其中[email protected]‘%‘是匹配所有IP地址都可以访问这个数据库,权限管理应该设置为
[email protected]‘192.168.35.%‘
只匹配某一个网段,或者某个指定IP地址,由于实验环境。安全不做过多考虑。
3.查看用户zsd的权限mysql> show grants for [email protected]‘%‘; +----------------------------------------------------------------+ | Grants for [email protected]% | +----------------------------------------------------------------+ | GRANT USAGE ON *.* TO `zsd`@`%` | | GRANT SELECT, INSERT, UPDATE, DELETE ON `zdemo`.* TO `zsd`@`%` | +----------------------------------------------------------------+ 2 rows in set (0.01 sec)
python 链接MySQL数据库实例
- 实验环境状态
客户端:10.1.11.18
mysql服务端:10.1.11.170 python链接实例语句
#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb db = MySQLdb.connect("10.1.11.170", "zsd", "[email protected]", "zdemo", charset=‘utf8‘ ) cursor = db.cursor() cursor.execute("SELECT VERSION()") data = cursor.fetchone() print "Database version : %s " % data db.close()
执行结果
[[email protected] data]# python test.py Database version : 8.0.11
目的:搭建好客户端和数据库端的基础环境,下一节通过编写python脚本,批量插入语句和查询语句,简单模拟负载。并通过基准测试收集mysql的性能状态变量。做分析研究
以上是关于MySQL实验准备--环境准备的主要内容,如果未能解决你的问题,请参考以下文章