你能在 Rails 中获取数据库用户名、密码、数据库名称吗?
我正在编写一个 rake 任务,它在 Rails/ActiveRecord 之外执行一些数据库工作。
有没有办法获取database.yml
中定义的当前环境的数据库连接信息(主机、用户名、密码、数据库名称)?
我想得到它,这样我就可以用它来像这样连接......
con = Mysql.real_connect("host", "user", "pw", "current_db")
I'm writing a rake task that does some DB work outside of Rails/ActiveRecord.
Is there a way to get the DB connection info (host, username, password, DB name) for the current environment as defined in database.yml
?
I'd like to get it so I can use it to connect like this...
con = Mysql.real_connect("host", "user", "pw", "current_db")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 Rails 中,您可以创建一个配置对象并从中获取必要的信息:
请参阅文档< /a> 用于 Rails::Configuration 了解详细信息。
这只是使用 YAML::load 从数据库配置加载配置文件 (
database.yml
),您可以自己使用该文件从 Rails 环境外部获取信息:From within rails you can create a configuration object and obtain the necessary information from it:
See the documentation for Rails::Configuration for details.
This just uses YAML::load to load the configuration from the database configuration file (
database.yml
) which you can use yourself to get the information from outside the rails environment:布莱恩在上面评论中的回答值得更多的曝光:
Bryan's answer in the comment above deserves a little more exposure:
以散列形式返回连接配置:
正如
tpett
在他们的评论中所说:此解决方案考虑了合并来自database.yml
和环境变量DATABASE_URL< 的配置/代码>。
returns the connection configuration in a hash:
As
tpett
remarked in their comment: this solution accounts for merging the configuration fromdatabase.yml
and from the environment variableDATABASE_URL
.从 Rails 6.1 开始,
ActiveRecord::Base.connection_config
已被弃用。 较新的访问器是ActiveRecord::Base.connection_db_config
As of Rails 6.1,
ActiveRecord::Base.connection_config
is deprecated. The newer accessor isActiveRecord::Base.connection_db_config
我认为这是最简单的解决方案。 经过一些测试(至少在 Rails 5.2 中),这将正确解析 DATABASE_URL。
I think this is the simplest solution. After some testing (in Rails 5.2 at least) this will resolve DATABASE_URL correctly.
在 Rails 6.1+ 中,您可以使用:
这将返回包含当前环境的连接信息的哈希值:
In Rails 6.1+, you can use:
This returns a hash containing the current environment's connection information:
老问题,但这是我寻找如何做到这一点的第一站之一,所以我认为这可能对其他人有帮助。 我通常在主目录中有 .my.cnf 文件。 因此,在我的database.yml配置文件中使用“parseconfig”gem和一些ERB语法意味着我拥有动态文件,我可以很好地检查源代码控制并简化部署(在我的例子中)。 另请注意常见套接字列表,这使得我可以更轻松地将应用程序移动到可能具有不同 Unix 套接字路径的不同操作系统。
参考: http://effectif.com/articles/database-yml-should -签入
Old question but this was one of my first stops in looking up how to do this so I figure this may help someone else. I normally have .my.cnf files in the home directory. So using the 'parseconfig' gem and some ERB syntax in my database.yml config file means I've got dynamic file that I can feel good about checking into source control and also simplify deployments (in my case). Also note the list of common sockets, this makes it easier to move my app to different operating systems that might have a different Unix socket path.
ref: http://effectif.com/articles/database-yml-should-be-checked-in
我试图获取用户名和密码以便连接到 pgAdmin。 我尝试了上面的每种方法,但找不到在 Rails 7 中开发的数据库的
username
和password
值。事实证明 pgAdmin 实际上并不需要用户名以及各个应用程序数据库的密码; 相反,它将向 postgres 服务器进行身份验证,因此可以访问服务器上的所有数据库。
对于出于与我类似的原因需要
用户名
和密码
的人来说,这可能会解决问题。但这并不能解释为什么我无法获取特定(rails 7)应用程序的
用户名
和密码
。 我能想到的最好的猜测是,这是因为 Rails 7 默认使用默认凭据连接到本地数据库。就我而言(PostgreSQL)默认使用用户名“postgres”,没有密码或密码为空。
因此,如果您签入
config/database.yml
并且开发环境引用的是default
,并且这些默认值不会列出username
的任何内容和密码
明确,它可能使用数据库的默认信用。 不过,我找不到任何方法来确认这一点,这只是我根据我在 Rails 7 的database.yml
中看到的内容进行的怀疑,事实上ENV
没有提供与db,并且上述所有方法都没有给出结果。I was trying to get the username and password in order to connect to pgAdmin. I tried every method above and couldn't find the values for
username
andpassword
for my database in development in rails 7.It turns out pgAdmin didn't actually need the username and password for the individual app's database; instead, it will authenticate to the postgres server and hence have access to all databases on the server.
This may solve the problem for people needing the
username
andpassword
for similar reasons to me.But this doesn't explain why I was unable to get the
username
andpassword
for my specific (rails 7) app. The best I can guess is it's because Rails 7 defaults to using default credentials to connect to the local database.In my case (PostgreSQL) uses the username "postgres" by default with no password or a blank password.
So if you check in
config/database.yml
and the dev environment is referring todefault
, and those defaults don't list anything forusername
andpassword
explicitly, it might be using your database's default creds. I couldn't find any way to confirm this though, it's merely my suspicion based on what I see in rails 7'sdatabase.yml
, the factENV
gives nothing related to the db, and that all the above methods gave no results.