Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup mysql database for development in Ruby!
require 'mysql2'
@db_host = 'localhost'
@login_user = 'root'
STDOUT.sync = true
puts "Enter password for mysql root user:"
@login_pass = gets.chomp
puts "\nDatabase name:"
@db_name = gets.chomp
puts "\nDatabase username:"
@db_user = gets.chomp
puts "\nDatabase user password:"
@db_pass = gets.chomp
client = Mysql2::Client.new(:host => @db_host,
:username => @login_user,
:password => @login_pass)
client.query("CREATE DATABASE #{@db_name} CHARACTER SET utf8")
client.query("GRANT USAGE ON *.* TO #{@db_user}@#{@db_host} IDENTIFIED BY '#{@db_pass}'")
client.query("GRANT ALL PRIVILEGES ON #{@db_name}.* TO #{@db_user}@#{@db_host}")
client.query("FLUSH PRIVILEGES")
client.close
require 'mysql2'
require 'pry'
@db_host = 'localhost'
@login_user = 'root'
STDOUT.sync = true
puts "Enter password for mysql root user:"
@login_pass = gets.chomp
puts "\nDatabase name (without 'db' suffix):"
@db_name = gets.chomp
puts "\nDatabase username:"
@db_user = gets.chomp
puts "\nDatabase user password:"
@db_user_pass = gets.chomp
@dev_db = @db_name + 'db'
@test_db = @db_name + 'testdb'
client = Mysql2::Client.new(:host => @db_host,
:username => @login_user,
:password => @login_pass)
client.query("CREATE DATABASE #{@dev_db} CHARACTER SET utf8")
client.query("CREATE DATABASE #{@test_db} CHARACTER SET utf8")
client.query("GRANT USAGE ON *.* TO #{@db_user}@#{@db_host} IDENTIFIED BY '#{@db_user_pass}'")
client.query("GRANT ALL PRIVILEGES ON #{@dev_db}.* TO #{@db_user}@#{@db_host}")
client.query("GRANT ALL PRIVILEGES ON #{@test_db}.* TO #{@db_user}@#{@db_host}")
client.query("FLUSH PRIVILEGES")
client.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment