gpt4 book ai didi

Mysql2::在database.yml中使用env变量时出现错误 "Access denied for user"

转载 作者:行者123 更新时间:2023-11-29 15:58:26 25 4
gpt4 key购买 nike

我的系统是 macOX Mojave 10.14
MySQL是MySQL:8.0.16
我的database.yml是:

development:
adapter: mysql2
encoding: utf8
database: dev_database
reconnect: false
pool: 5
username: <%= ENV['RAILS_DEV_DB_USSERNAME'] %>
password: <%= ENV['RAILS_DEV_DB_PASSWORD'] %>
socket: /tmp/mysql.sock

并且我已确认该变量是否有效。使用

Command-Line: erb config/database.yml

我可以获得:

development:
adapter: mysql2
encoding: utf8
database: dev_database
reconnect: false
pool: 5
username: root
password: Wle3S#23sv
socket: /tmp/mysql.sock

但是当我启动 rails s -edevelopent 并浏览到我的页面时,我无法连接到我的数据库。

enter image description here

我现在能做什么?

最佳答案

正如作者arpiagar建议的那样rails 3, how use an ENV config vars in a Settings.yml file?检查他的示例(26)

when you introduce a scriptlet tag in .yml file, it is more of erb template. So read it as a erb template first and then load the yml

您可以使用dotenvfigaro gems(受十二因素应用程序方法启发)加载环境变量。

例如,如果您使用 dotenv gem

By default, load will look for a file called .env in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.

require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
HOSTNAME = ENV['HOSTNAME']

更多信息请查看下面提到的链接

  1. http://railsapps.github.io/rails-environment-variables.html

  2. https://www.honeybadger.io/blog/ruby-guide-environment-variables/

关于Mysql2::在database.yml中使用env变量时出现错误 "Access denied for user",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56334989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com