|
| 1 | +# Random data generator for MySQL |
| 2 | + |
| 3 | +Many times in my job i need to generate random data for a specific table in order to reproduce an issue. |
| 4 | +After writing many random generators for every table, I decided to write a random data generator, able to get the table structure and generate random data for it. |
| 5 | +Plase take into consideration that this is the first version and it doesn't support all field types yet! |
| 6 | + |
| 7 | +**NOTICE** |
| 8 | +This is an early stage project. |
| 9 | + |
| 10 | +## Supported fields: |
| 11 | + |
| 12 | +|Field type|Generated values| |
| 13 | +|----------|----------------| |
| 14 | +|tinyint|0 ~ 0xFF| |
| 15 | +|smallint|0 ~ 0XFFFF| |
| 16 | +|mediumint|0 ~ 0xFFFFFF| |
| 17 | +|int - integer|0 ~ 0xFFFFFFFF| |
| 18 | +|bigint|0 ~ 0xFFFFFFFFFFFFFFFF| |
| 19 | +|float|0 ~ 1e8| |
| 20 | +|decimal(m,n)|0 ~ 10^(m-n)| |
| 21 | +|double|0 ~ 1000| |
| 22 | +|char(n)|up to n random chars| |
| 23 | +|varchar(n)|up to n random chars| |
| 24 | +|date|NOW() - 1 year ~ NOW()| |
| 25 | +|datetime|NOW() - 1 year ~ NOW()| |
| 26 | +|timestamp|NOW() - 1 year ~ NOW()| |
| 27 | +|time|00:00:00 ~ 23:59:59| |
| 28 | +|year|Current year - 1 ~ current year| |
| 29 | +|blob|up to 100 chars random paragraph| |
| 30 | +|text|up to 100 chars random paragraph| |
| 31 | +|mediumblob|up to 100 chars random paragraph| |
| 32 | +|mediumtext|up to 100 chars random paragraph| |
| 33 | +|longblob|up to 100 chars random paragraph| |
| 34 | +|longtext|up to 100 chars random paragraph| |
| 35 | +|varbinary|up to 100 chars random paragraph| |
| 36 | +|enum|A random item from the valid items list| |
| 37 | +|set|A random item from the valid items list| |
| 38 | + |
| 39 | +### How strings are generated |
| 40 | + |
| 41 | +- If field size < 10 the program generates a random "first name" |
| 42 | +- If the field size > 10 and < 30 the program generates a random "full name" |
| 43 | +- If the field size > 30 the program generates a "lorem ipsum" paragraph having up to 100 chars. |
| 44 | + |
| 45 | +The program can detect if a field accepts NULLs and if it does, it will generate NULLs ramdomly (~ 10 % of the values). |
| 46 | + |
| 47 | +## Usage |
| 48 | +`random-data-generator <database> <table> <number of rows> [options...]` |
| 49 | + |
| 50 | +## Options |
| 51 | +|Option|Description| |
| 52 | +|------|-----------| |
| 53 | +|--host|Host name/ip| |
| 54 | +|--port|Port number| |
| 55 | +|--user|Username| |
| 56 | +|--password|Password| |
| 57 | +|--bulk-size|Number of rows per INSERT statement (Default: 1000)| |
| 58 | +|--debug|Show some debug information| |
| 59 | + |
| 60 | +### Example |
| 61 | +``` |
| 62 | +CREATE DATABASE IF NOT EXISTS test; |
| 63 | +
|
| 64 | +CREATE TABLE `test`.`t3` ( |
| 65 | + `id` int(11) NOT NULL AUTO_INCREMENT, |
| 66 | + `tcol01` tinyint(4) DEFAULT NULL, |
| 67 | + `tcol02` smallint(6) DEFAULT NULL, |
| 68 | + `tcol03` mediumint(9) DEFAULT NULL, |
| 69 | + `tcol04` int(11) DEFAULT NULL, |
| 70 | + `tcol05` bigint(20) DEFAULT NULL, |
| 71 | + `tcol06` float DEFAULT NULL, |
| 72 | + `tcol07` double DEFAULT NULL, |
| 73 | + `tcol08` decimal(10,2) DEFAULT NULL, |
| 74 | + `tcol09` date DEFAULT NULL, |
| 75 | + `tcol10` datetime DEFAULT NULL, |
| 76 | + `tcol11` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 77 | + `tcol12` time DEFAULT NULL, |
| 78 | + `tcol13` year(4) DEFAULT NULL, |
| 79 | + `tcol14` varchar(100) DEFAULT NULL, |
| 80 | + `tcol15` char(2) DEFAULT NULL, |
| 81 | + `tcol16` blob, |
| 82 | + `tcol17` text, |
| 83 | + `tcol18` mediumtext, |
| 84 | + `tcol19` mediumblob, |
| 85 | + `tcol20` longblob, |
| 86 | + `tcol21` longtext, |
| 87 | + `tcol22` mediumtext, |
| 88 | + `tcol23` varchar(3) DEFAULT NULL, |
| 89 | + `tcol24` varbinary(10) DEFAULT NULL, |
| 90 | + `tcol25` enum('a','b','c') DEFAULT NULL, |
| 91 | + `tcol26` set('red','green','blue') DEFAULT NULL, |
| 92 | + `tcol27` float(5,3) DEFAULT NULL, |
| 93 | + `tcol28` double(4,2) DEFAULT NULL, |
| 94 | + PRIMARY KEY (`id`) |
| 95 | +) ENGINE=InnoDB; |
| 96 | +``` |
| 97 | +To generate 10K random rows, just run: |
| 98 | +``` |
| 99 | +random-data-load test t3 100000 --max-threads=8 --user=root --password=root |
| 100 | +``` |
| 101 | +``` |
| 102 | +mysql> select * from t3 limit 1\G |
| 103 | +*************************** 1. row *************************** |
| 104 | + id: 1 |
| 105 | +tcol01: 10 |
| 106 | +tcol02: 173 |
| 107 | +tcol03: 1700 |
| 108 | +tcol04: 13498 |
| 109 | +tcol05: 33239373 |
| 110 | +tcol06: 44846.4 |
| 111 | +tcol07: 5300.23 |
| 112 | +tcol08: 11360967.75 |
| 113 | +tcol09: 2017-09-04 |
| 114 | +tcol10: 2016-11-02 23:11:25 |
| 115 | +tcol11: 2017-03-03 08:11:40 |
| 116 | +tcol12: 03:19:39 |
| 117 | +tcol13: 2017 |
| 118 | +tcol14: repellat maxime nostrum provident maiores ut quo voluptas. |
| 119 | +tcol15: Th |
| 120 | +tcol16: Walter |
| 121 | +tcol17: quo repellat accusamus quidem odi |
| 122 | +tcol18: esse laboriosam nobis libero aut dolores e |
| 123 | +tcol19: Carlos Willia |
| 124 | +tcol20: et nostrum iusto ipsa sunt recusa |
| 125 | +tcol21: a accusantium laboriosam voluptas facilis. |
| 126 | +tcol22: laudantium quo unde molestiae consequatur magnam. |
| 127 | +tcol23: Pet |
| 128 | +tcol24: Richard |
| 129 | +tcol25: c |
| 130 | +tcol26: green |
| 131 | +tcol27: 47.430 |
| 132 | +tcol28: 6.12 |
| 133 | +1 row in set (0.00 sec) |
| 134 | +``` |
| 135 | + |
| 136 | +## To do |
| 137 | +- [ ] Add suport for all data types. |
| 138 | +- [ ] Add supporrt for foreign keys. |
| 139 | +- [ ] Support config files to override default values/ranges. |
| 140 | +- [ ] Support custom functions via LUA plugins. |
0 commit comments