简体   繁体   English

Ruby on Rails不会更改为mysql

[英]Ruby on rails wont change to mysql

I have just started using Ruby on rails and I have run into a very wierd problem. 我刚刚开始在rails上使用Ruby,但遇到了一个非常棘手的问题。 It seems like my rails app just wont use mysql as a database. 看来我的Rails应用程序不会使用mysql作为数据库。

I managed to create my app with a mysql db but then when I tried to generate a scaffold it started to nag about me needing to install postgres which I thought was wierd but I did that and installed all the required libraries. 我设法用mysql db创建我的应用程序,但是当我尝试生成一个脚手架时,它开始it恼我需要安装postgres,我认为这很奇怪,但是我做到了,并安装了所有必需的库。 Now I was able to create my scaffold but when I try to run rake db:migrate it tries to connect to a postgres database on my system. 现在我可以创建支架了,但是当我尝试运行rake db:migrate时,它尝试连接到系统上的postgres数据库。

Isn't this really wierd? 这真的很奇怪吗? Why would it bother me about pg when I said in my rails new command that I wanted to use mysql? 当我在rails new命令中说我想使用mysql时,为什么对pg感到困扰? And why wont it let me use mysql even though there is not a single reference to pg or postgres in my entire application? 为何即使我在整个应用程序中都没有对pg或postgres的单个引用,它也为什么不能让我使用mysql?

I have tried reading a lot of tutorials and stuff but I can not seem to find what I have done wrong, but I really hope some of u guys can point me in the right direction! 我尝试阅读许多教程和资料,但似乎无法发现自己​​做错了什么,但我真的希望你们中的一些人能指出正确的方向!

UPDATE: From the connection output in db:create and db:migrate it seems like the server has cached some config from another project with the same name that I deleted a few days ago. 更新:从db:create和db:migrate中的连接输出中,服务器似乎已经从另一个项目中缓存了一些配置,该配置与我几天前删除的名称相同。 I'm not able to find this cache, does it exist? 我找不到此缓存,它存在吗? If then where? 如果在哪里呢?

Commands I have run: 我运行的命令:

rails new MyApp -d mysql
rails generate scaffold name:string
rake db:migrate (results in connection timeout to the nonexistent pg db)

Gemfile: 的Gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.1'
# Use mysql as the database for Active Record
gem 'mysql2'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more:         https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background.     Read more: https://github.com/rails/spring
gem 'spring',        group: :development

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

database.yml: database.yml的:

# MySQL.  Versions 5.0+ are recommended.
#
# Install the MYSQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
#
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: password
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
  database: rento_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: rento_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: rento
  username: rento
  password: <%= ENV['RENTO'] %>

System: 系统:

  • Ubuntu 14.04 x64 Ubuntu 14.04 x64
  • Rails 4.1.1 Rails 4.1.1

Your adapter option on database.yml should be mysql instead of mysql2 . 您在database.yml上的适配器选项应该是mysql而不是mysql2

And you could run rake db:create before rake db:migrate , this will create the database for you before running the migrations. 您可以在rake db:migrate之前运行rake db:create ,这将在运行迁移之前为您创建数据库。

Hope I could help. 希望我能帮上忙。

Wow this was a silly error. 哇,这是一个愚蠢的错误。 Seems like I have a environment variable from a JAVA project I'm working in that is named DATABASE_URL. 好像我有一个正在使用的JAVA项目中的环境变量,名为DATABASE_URL。 And I guess this is a standard that Rails uses. 我想这是Rails使用的标准。 Changing the env fixes all my problems! 更改环境可以解决我所有的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM