1

Working on an app that wil be used by admin only to create multiple instances of another app.

In other words, app A will be used to create clones of app B on server and create all the configurations needed to run that cloned app on a subdomain new_clone.domain.com.

Managed to clone the app, created the apache config file, unicorn server settings file too. Managed to run rake db:create; db:migrate but I did this with the root user of mysql.

At the point where I clone the app, I generate a database.yml file for the new cloned app that has at this moment username and password set as root, but I woould like to have a different user for each cloned app.

The app A has a model called Subdomain, in the subdomains_controller.rb at the create action I do all the things, clone, generate config files, running rake tasks, etc... and also in this create action I need to create a new Mysql user with privileges for a specific database.

What I have tried so far, can't say I did much, I tried to run the mysql command within controller create action:

def create
  if @subdomain.save
    ....
    system "mysql -u root -p root; create database new_database;..."
    ....
  else
    ....
  end
end

but this puts my create action on hold until I type in the password, and even if I'll find a way to go over it I am not sure the rest of mysql commands will work. Probably there is a better way to add a Mysql user with one command line without going into mysql console.

Thank you.

5
  • Does your user have the privileges needed for database or user creation? Jun 20, 2013 at 9:48
  • well, my root user yes, but that's the idea - when creating a new user it should have privileges to create a database and have all kind of privileges only on that database. Jun 20, 2013 at 10:19
  • That command ask you for the root password just because there is a space between -p and root, check the mysql man page.
    – toro2k
    Jun 20, 2013 at 10:34
  • 1
    Don't think it's a good idea to play with mysql users from ruby script, it may be a big seciruty threat in the future. Jun 20, 2013 at 10:37
  • @toro2k thank you, that was indeed a part of the problem. Jun 20, 2013 at 11:32

2 Answers 2

2

As far as I understood, your create method runs under Rails app with root mysql user. Then you can simply execute mysql commands via AR adapter:

# Create DB
ActiveRecord::Base.connection.execute("CREATE DATABASE IF NOT EXISTS \
`#{ActiveRecord::Base.sanitize(db_name)}` CHARACTER SET = `utf8` \ 
COLLATE = `utf8_general_ci`")    
# Create User
ActiveRecord::Base.connection.execute("REPLACE INTO mysql.user \ 
(Host, User, Password) VALUES(\"localhost\", \ 
\"#{ActiveRecord::Base.sanitize(db_user)}\", \ 
PASSWORD(\"#{ActiveRecord::Base.sanitize(db_password)}\"))")
# Grant access
ActiveRecord::Base.connection.execute("GRANT ALL PRIVILEGES ON \ 
`#{ActiveRecord::Base.sanitize(db_name)}`.* TO \ 
`#{ActiveRecord::Base.sanitize(db_user)}`")
# Apply 
ActiveRecord::Base.connection.execute("FLUSH PRIVILEGES")
1
  • Thank you, you answer helped a lot, however I managed to make it a little shorter. I'll post it as an answer too. Jun 20, 2013 at 11:28
1

Thanks to Yuriy's answer I got it working in a shorter way:

  def create_mysql_user
    @db_name = "#{@subdomain.name}_domain_production"
    @db_user = "#{@subdomain.name}_user"
    @db_password = "#{@subdomain.name}domain_password"

    ActiveRecord::Base.connection.execute("GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER,
        INDEX ON `#{@db_name}`.* TO #{ActiveRecord::Base.sanitize(@db_user)}@localhost
        IDENTIFIED BY #{ActiveRecord::Base.sanitize(@db_password)};")
    # Apply
    ActiveRecord::Base.connection.execute("FLUSH PRIVILEGES")
  end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.