Skip to content

Instantly share code, notes, and snippets.

@bnd5k
Created October 8, 2012 16:19
Show Gist options
  • Save bnd5k/3853376 to your computer and use it in GitHub Desktop.
Save bnd5k/3853376 to your computer and use it in GitHub Desktop.
Script for autogenerating user and password MySql
#!/bin/sh
# vim: se ft=sh ts=2 sw=2:
#
set -e
echo ""
echo " Hello"
echo ""
# Check for Bundler
if test ! $(which bundle)
then
echo " x You need to install Bundler:"
echo " gem install bundler"
exit
else
echo " + Bundler found."
fi
# Install gems
echo " + Installing your bundle."
bundle install --quiet
# Setup database config
if test -f "config/database.yml"
then
echo " + Database configuration found in config/database.yml"
else
echo ""
echo " - Database configuration not found."
echo ""
echo " + Copying example configuration from config/database.example.yml"
echo ""
echo " - Note: You will want to check this file to match your local DB config"
cp config/database.example.yml config/database.yml
fi
echo ""
echo "You are ready to rock!"
echo ""
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
# Example database config
# Customize to your environment
defaults: &defaults
adapter: mysql2
encoding: utf8
pool: 5
username: es_tech
socket: /tmp/mysql.sock
development:
<<: *defaults
database: tester_development
password:
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test: &test
<<: *defaults
database: tester_test
password:
#!/bin/sh
# vim: se ft=sh ts=2 sw=2:
set -e
echo ""
echo " Whaddup..."
echo ""
#grab user's password for dbms
echo "Enter the password for you root MySql user: "
echo "(just press enter if there is no password)"
echo "> "
stty -echo
read root_password
stty echo
echo ""
#First, for Dev db, create 14-character "random" string as password
#http://tldp.org/LDP/abs/html/string-manipulation.html#CVT
###Note: this should ultimately be alphanumeric, not just numeric
#use PID of script as start-string.
str0="$$"
POS=2 # Starting from position 2 in the string.
LEN=14 # Extract a bunch of characters
str1=$( echo "$str0" | md5 | md5 )
# Doubly scramble ^^^ ^^^
#+ by piping and repiping to md5.
randstring="${str1:$POS:$LEN}"
echo "$randstring"
dev_db="tester_development"
#how can I pull db name from YML file instead of hardcoding it?
dev_pass=$randstring;
newpass=dev_pass;
database=' database: $dev_db';
##this is way to convoluted as is...
sed '/^'"${database}"'/,/^\s*$/s/password:/&'"${newpass}"'/' config/database.yml
#this sed sure as hell isn't working...
#sed '/^' database: tester_development'/,/^\s*$/s/password:/&'$dev_pass'/' config/database.yml
#sed '/^' database: tester_development"'/,/^\s*$/s/password:/&'"$dev_pass"'/' config/database.yml
##Next, figure out a DRY way to repeat for the test dbt
#log into mysql using user and password, then add passwords to dbs
mysql --user=root --password=$root_password <<EOF
GRANT All PRIVILEGES ON '$dev_db'.* TO 'tester_user'@'localhost'
IDENTIFIED BY '$dev_pass';
EOF
echo ""
echo "You are ready to rock!"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment