无法转储远程数据库

时间:2013-03-15 11:02:15

标签: mysql ruby-on-rails database rake

我在database.yml文件中有这个数据库配置

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: oma_development
  pool: 5
  username: root
  password:
  host: localhost

test:
  # same as in development section

production:
  # same as in development section


development_blacklist:
  adapter: mysql2
  encoding: utf8
  database: webstuff
  username: login
  password: password
  host: remote-database.com

test_blacklist:
  adapter: mysql2
  encoding: utf8
  database: webstuff
  username: megas
  password:
  host: /var/run/mysqld/mysqld.sock

我想执行rake任务:

namespace :db do
  namespace :schema do
    desc 'Dump blacklist database schema'
    task :dump => [:environment, :load_config] do
      filename = "#{Rails.root}/db/blacklist_schema.rb"
      File.open(filename, 'w:utf-8') do |file|
        ActiveRecord::Base.establish_connection("development_blacklist")
        ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
      end
    end
  end
end

通过调用此命令:

rake db:schema:dump

我有错误:

rake aborted!
Access denied for user 'root'@'localhost' (using password: NO)

因此rake任务似乎试图从development部分连接到数据库。

我不明白为什么rake任务尝试连接到development数据库,而rake任务应该连接到development_blacklist数据库。

如何设置rake任务以仅连接到development_blacklist数据库?

1 个答案:

答案 0 :(得分:3)

您可以明确设置环境,例如:

$ RAILS_ENV=production rake db:schema:dump