一、安装datafaker模块
pip install datafaker
二、安装对应数据库包
对于不同的数据库需要用到不同的python包,若在执行过程中报包缺失问题。 请pip安装对应包
| 数据库 | python包 | 备注 |
| mysql/tidb | mysql-python/mysqlclient | windows+python3请使用mysqlclient |
| oracle | cx-Oracle | 同时需要下载orale相关库 |
| postgresql/redshift | psycopg2 | 根据sqlachemy选择对应包 |
| sqlserver | pyodbc | mssql+pyodbc |
| Hbase | happybase,thrift | |
| es | elasticsearch | |
| hive | pyhive | |
| kafka | kafka-python |
安装过程
pip install psycopg2 -i https://mirrors.aliyun.com/pypi/simple/
pip临时加速地址
国内主要镜像地址如下:
清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 华中理工大学:http://pypi.hustunique.com/ 山东理工大学:http://pypi.sdutlinux.org/ 豆瓣:http://pypi.douban.com/simple/
故而临时加速pip速度的命令为:
pip install
永久提速
Windows系统配置
1、在 C:\Users\Administrator\pip 建一个文件 pip.ini如果Administrator 中 没有pip文件夹则自己新建一个,然后新建一个 pip.ini 文件
2、在 pip.ini 文件输入:
[global] index-url=https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com
使用记事本默认的ANSI编码格式复制上面的文本粘贴即可。
Mac/Linux系统配置
1、打开terminal
2、输入命令:
mkdir .pip vim .pip/pip.conf
这两步是在home目录下新建文件: .pip/pip.conf)
按 i 键进入输入模式,在这个文件中复制粘贴写入如下内容:
[global] index-url = https://mirrors.aliyun.com/pypi/simple/ timeout = 1000
[install] use-mirrors = true mirrors = https://mirrors.aliyun.com//
实际操作
- #step1 查看python的版本信息
-
- $ python3 --version Python 3.7.10
-
- #step2 创建虚拟运行环境
-
- $ python3 -m venv env
-
- $ source env/bin/activate (env)
-
- #查看虚拟运行环境中目前已安装的python模块
-
- (env) $ pip3 list
-
- #安装datafaker模块
-
- (env) $ pip3 install datafaker
-
- # 安装psycopy模块
-
- (env) $ pip3 install psycopg
-
- # step4 安装mysql-client模块
-
- (env) $ pip3 install mysql-client
-
- Amazon Linu2部署安装MySQL8
-
- #step1 添加mysql仓库
-
- sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
-
- #step2 安装mysql服务软件
-
- sudo yum -y install mysql-community-server
-
- #step3 启动mysql服务
- sudo systemctl enable --now mysqld
- #step4 获取mysql临时密码
- sudo grep 'temporary password' /var/log/mysqld.log
- #step5 修改密码
- $sudo mysql_secure_installation -p 【使用获取的临时密码登录】
- $ sudo mysql_secure_installation -p
- Enter password: Securing the MySQL server deployment.
- The existing password for the user account root has expired.
- Please set a new password.
- New password:
- Re-enter new password:
- The 'validate_password' component is installed on the server.
- The subsequent steps will run with the existing configuration of the component.
- Using existing password for root.
- Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : No
- ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them.
- This is intended only for testing, and to make the installation go a bit smoother.
- You should remove them before moving into a production environment.
- Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
- Success. Normally, root should only be allowed to connect from 'localhost'.
- This ensures that someone cannot guess at the root password from the network.
- Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
- Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- - Dropping test database...
- Success. - Removing privileges on test database...
- Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
- Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
- Success. All done!
- #step6 验证使用修改之后的密码是否可以正常登录操作mysql数据库
- $ mysql -uroot -p
-
- mysql>
- #Step7 创建数据库demo
- mysql>create database demo;
- #step8 切换到demo数据库
- mysql> use demo;
- #Step9 创建表
- create table stu ( id int unsigned auto_increment primary key COMMENT '自增id', name varchar(20) not null comment '学生名字', school varchar(20) not null comment '学校名字', nickname varchar(20) not null comment '学生小名', age int not null comment '学生年龄', class_num int not null comment '班级人数', score decimal(4,2) not null comment '成绩', phone bigint not null comment '电话号码', email varchar(64) comment '家庭网络邮箱', ip varchar(32) comment 'IP地址', address text comment '家庭地址' ) engine=InnoDB default charset=utf8;
-
- #Step10 编写刚创建好的stu表的元数据
-
- $ vi meta.txt (env)
- $ cat meta.txt
- id||int||自增id[:inc(id,1)]
- name||varchar(20)||学生名字
- school||varchar(20)||学校名字[:enum(file://names.txt)]
- nickname||varchar(20)||学生小名[:enum(鬼泣, 高小王子, 歌神, 逗比)]
- age||int||学生年龄[:age]
- class_num||int||班级人数[:int(10, 100)]
- score||decimal(4,2)||成绩[:decimal(4,2,1)]
- phone||bigint||电话号码[:phone_number]
- email||varchar(64)||家庭网络邮箱[:email]
- ip||varchar(32)||IP地址[:ipv4]
- address||text||家庭地址[:address]
-
- #step11 设置学校可获取的值信息names.txt文件
-
- $ cat names.txt 清华中学 人和中心 广东中学 猪场 旧大院
- #step12 安装pymysql模块
- $ pip3 install pymysql
- Collecting pymysql Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.8/43.8 kB 8.6 MB/s eta 0:00:00 Installing collected packages: pymysql Successfully installed pymysql-1.0.2
元数据列说明
meta.txt文件中每行数据为元数据的一个字段描述,以||分割为三列,若以#开头,则忽略该行。
name不加标记则会随机产生20字符内的字符串,可以加上改为:学生名字[:name]
其中学校名字[:enum(file://names.txt)]表示从本地文件names.txt中读取枚举数据,表示学校名称只能从下面这5所学校中随机产生。names.txt内容如下:
清华中学
人和中心
广东中学
猪场
旧大院
从本地meta.txt中读取元数据,以,,分隔符构造10条数据,打印到屏幕上
$ datafaker rdb mysql+pymysql://root:Aa123456#@localhost:3306/test?charset=utf8 stu 10 --outprint --meta meta.txt --outspliter ,,
命令说明: datafaker连接mysql使用的数据库类型为rdb或者mysql。
mysql+pymysql为其使用的包
root为mysql数据库连接的用户名
Aa123456#为数据库连接使用的密码
3306为mysql的端口号
将生成的数据直接写入mysql中
#step1 清空mysql表数据
delete from stu;
或者使用
truncte table stu;
#Step2 将生成的数据加载到mysql数据库的表中去
$ datafaker mysql mysql+pymysql://root:Aa123456#@localhost:3306/demo?charset=utf8 stu 10 --meta meta.txt insert 10 records time used: 0.657 s
注意事项
若要再次运行,需要修改meta.txt文件中为
id[:inc(id,11)]起始值为11或更大值,不然数据库会报主键重复错误。
验证数据是否成功插入到MySQL中去

参考资料
datafaker/README.md at master · gangly/datafaker · GitHub
datafaker --- 测试数据生成工具-阿里云开发者社区