I have a Ruby on Rails project where I want to let the user execute SQL SELECT queries.
For safety purposes, I have two mysql databases. One of them is my main database, with user information and so on. The second database is intended to hold the "test" tables where the users are supposed to be able to SELECT stuff from using a specific database user that I granted "select" permission.
Basically, I want my user to submit a query, then I want to change to my test database, run that query, return a result, and then go back to my old database.
Database.yml
mysql: &mysql
adapter: mysql
database: sql_detective
user: root
password: root
timeout: 5000
mysql_tests: &mysql_tests
adapter: mysql
database: sql_detective_tests
user: user
password: user
timeout: 5000
test:
<<: *mysql_tests
development:
<<: *mysql
Funcionario.rb
class Funcionario < ActiveRecord::Base
establish_connection :test
end
stages_controller.rb
@query = Funcionario.find_by_sql(params[:query])
When I try to execute the query, I get this error:
Mysql::Error in StagesController#sql_query
Access denied for user 'root'@'localhost' (using password: YES)