[英]Creating a Mysql user within a Rails controller action
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
. 换句话说,
app A
将用于在服务器上创建app B
克隆,并创建在子域new_clone.domain.com
上运行该克隆应用程序所需的所有配置。
Managed to clone the app, created the apache
config file, unicorn server settings
file too. 管理克隆应用程序,也创建了
apache
配置文件, unicorn server settings
文件。 Managed to run rake db:create; db:migrate
管理运行rake
db:create; db:migrate
db:create; db:migrate
but I did this with the root
user of mysql. db:create; db:migrate
但是我用mysql的root
用户做了这个。
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. 在克隆应用程序的时候,我为新的克隆应用程序生成了一个
database.yml
文件,此时此username
和password
设置为root
,但我希望每个克隆应用程序都有不同的用户。
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. 应用程序A有一个名为
Subdomain
的模型,在create
操作的subdomains_controller.rb
中,我执行所有操作,克隆,生成配置文件,运行rake任务等等...而且在此create
操作中我需要创建一个新的具有特定数据库权限的Mysql
用户。
What I have tried so far , can't say I did much, I tried to run the mysql command within controller create action: 到目前为止我所尝试的 ,不能说我做了太多,我试着在控制器创建动作中运行mysql命令:
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. 但这会让我的创建操作暂停,直到我输入密码,即使我找到了一种方法来检查它,我也不确定其余的mysql命令是否可行。 Probably there is a better way to add a Mysql user with one command line without going into mysql console.
可能有更好的方法来添加一个命令行的Mysql用户,而无需进入mysql控制台。
Thank you. 谢谢。
As far as I understood, your create method runs under Rails app with root
mysql user. 据我所知,你的create方法在
root
mysql用户的Rails应用程序下运行。 Then you can simply execute mysql commands via AR adapter: 然后你可以通过AR适配器简单地执行mysql命令:
# 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")
Thanks to Yuriy's answer I got it working in a shorter way: 感谢Yuriy的回答,我让它以更短的方式工作:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.