Answers:
如果您已经有一个rails项目,请将config/database.yml
文件中的适配器更改为mysql
,并确保您指定了有效的用户名和密码,以及可选的套接字:
development:
adapter: mysql2
database: db_name_dev
username: koploper
password:
host: localhost
socket: /tmp/mysql.sock
接下来,确保您编辑Gemfile以包括mysql2或activerecord-jdbcmysql-adapter(如果使用jruby)。
gem 'sqlite3'
和添加gem 'mysql2'
对于Rails 3,您可以使用以下命令使用mysql创建一个新项目:
$ rails new projectname -d mysql
如果尚未创建应用程序,则只需转到cmd(对于Windows)或terminal(对于linux / unix),然后键入以下命令以使用mysql数据库创建Rails应用程序:
$rails new <your_app_name> -d mysql
它适用于Rails版本3以上的任何版本。如果您已经创建了应用程序,则可以执行以下两项操作之一:
要么
开发:
适配器:mysql2
数据库:db_name_name
用户名:root
密码:
主机:localhost
套接字:/tmp/mysql.sock
此外,从Gemfile中删除gem'sqlite3'并添加gem'mysql2'
您应该使用-D而不是-d开关,因为它将生成两个没有文档文件夹的应用程序和mysql。
rails -D mysql project_name (less than version 3)
rails new project_name -D mysql (version 3 and up)
或者,您仅使用该--database
选项。
只需转到rails console并输入:
rails new YOURAPPNAME -d mysql
在新项目中,轻松实现:
rails new your_new_project_name -d mysql
在现有项目上,绝对棘手。这给我带来了有关现有Rails项目的许多问题。这种与我合作的作品:
# On Gemfile:
gem 'mysql2', '>= 0.3.18', '< 0.5' # copied from a new project for rails 5.1 :)
gem 'activerecord-mysql-adapter' # needed for mysql..
# On Dockerfile or on CLI:
sudo apt-get install -y mysql-client libmysqlclient-dev
使用以下命令为带有MySQL数据库的API创建新应用
rails new <appname> --api -d mysql
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
数据库
# MySQL. Versions 5.1.10 and up are supported.
#
# 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:
# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: localhost
database: database_name
username: username
password: secret
development:
<<: *default
# 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
# 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
宝石文件:
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
如果您使用的是Ubuntu或任何Debian发行版,则首先应确保MySQL驱动程序在系统上(如果未在终端上运行该驱动程序)
sudo apt-get install mysql-client libmysqlclient-dev
并将其添加到您的Gemfile
gem 'mysql2', '~> 0.3.16'
然后在项目的根目录中运行
bundle install
之后,您可以将mysql配置添加到config / database.yml作为之前的答案