Skip to content

Instantly share code, notes, and snippets.

@nowlinuxing
Created October 1, 2014 03:28
Show Gist options
  • Save nowlinuxing/5b8aaed98559b74f057f to your computer and use it in GitHub Desktop.
Save nowlinuxing/5b8aaed98559b74f057f to your computer and use it in GitHub Desktop.
Generate test datas and import to MySQL via tempolary CSV file
#!/bin/sh
DB="some_db"
DB_USER="user"
DB_PASS="pass"
TABLE="users"
CSV_FILE="/tmp/users.csv"
N_RECORD=1000000
mysql -u$DB_USER -p$DB_PASS $DB -e "DELETE FROM $TABLE"
# create a CSV file
# #id,name,created_at,updated_at
ruby -e '1.upto('$N_RECORD').each { |n| t = Time.at(1234567890+n).strftime("%Y-%m-%d %H:%M:%S"); puts [n, "user#{n}", t, t] }' > $CSV_FILE
# import the CSV file
mysql -u$DB_USER -p$DB_PASS $DB -e "LOAD DATA INFILE '$CSV_FILE' INTO TABLE $TABLE FIELDS TERMINATED BY ','"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment