将 Ruby on Rails 应用程序从 sqlite 转换为 MySQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1670154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:55:20  来源:igfitidea点击:

Convert a Ruby on Rails app from sqlite to MySQL?

ruby-on-rails

提问by Austin Richardson

I made an app in Ruby on Rails and now I want to get it hosted. However, they require that I use MySQL and I set it up using sqLite3. Is there any way to convert it to use MySQL?

我在 Ruby on Rails 中制作了一个应用程序,现在我想托管它。但是,他们要求我使用 MySQL 并使用 sqLite3 进行设置。有什么方法可以将其转换为使用 MySQL 吗?

回答by marshally

Step 0

步骤 0

To be safe, I recommend experimenting a bit with this technique in a virtual machine. Save yourself a bunch of heartache and build a virtual machine, check out your code, and have a safe playground that you can throw away if tragedy strikes.

为安全起见,我建议在虚拟机中尝试使用这种技术。为自己省去一大堆心痛,构建一个虚拟机,检查你的代码,并拥有一个安全的游乐场,如果悲剧发生,你可以把它扔掉。

Step 1

第1步

Make a backup copy of your database.yml file.

制作 database.yml 文件的备份副本。

(from your application root)

(来自您的应用程序根目录)

cp config/database.yml config.database.yml.sqlite3

cp config/database.yml config.database.yml.sqlite3

Step 2

第2步

Make a backup copy of your data

制作数据的备份副本

For Rails 3, install the YAML DB gem: https://github.com/ludicast/yaml_db

对于 Rails 3,安装 YAML DB gem:https: //github.com/ludicast/yaml_db

For Rails 2.x install the YAML DB plugin:

对于 Rails 2.x,安装 YAML DB 插件:

script/plugin install git://github.com/adamwiggins/yaml_db.git

script/plugin install git://github.com/adamwiggins/yaml_db.git

Run the dump task

运行转储任务

rake db:dump

rake db:dump

Step 3

第 3 步

Update your config/database.yml file. You will find entries like

更新您的 config/database.yml 文件。你会发现像这样的条目

development:
  adapter: sqlite3
  database: db/development.sqlite3
  timeout: 5000
test:
  adapter: sqlite3
  database: db/test.sqlite3
  timeout: 5000
production:
  adapter: sqlite3
  database: db/production.sqlite3
  timeout: 5000

Change them to

将它们更改为

development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: **myapp_development**
  pool: 5
  username: **root**
  password: **supersecretpassword**
  **socket: /opt/local/var/run/mysql5/mysqld.sock**
test:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: **myapp_test**
  pool: 5
  username: **root**
  password: **supersecretpassword**
  socket: **/opt/local/var/run/mysql5/mysqld.sock**

production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: **myapp_production**
  pool: 5
  username: **root**
  password: **supersecretpassword**
  socket: **/opt/local/var/run/mysql5/mysqld.sock**

Be sure to update the values surrounded by asterix as appropriate for your platform! The socket value is only good for Mac OSX using MacPorts. Most flavors of linux do not require this value.

请务必根据您的平台更新星号包围的值!套接字值仅适用于使用 MacPorts 的 Mac OSX。大多数 linux 版本不需要这个值。

Step 5

第 5 步

If you have some errors in the following step, you might have to install the mysql gem:

如果您在以下步骤中遇到一些错误,您可能需要安装 mysql gem:

sudo gem install mysql

Have rake create your database

让 rake 创建你的数据库

rake db:create
rake db:schema:load

Step 6

第 6 步

Use YamlDb to reload your data into MySql

使用 YamlDb 将数据重新加载到 MySql

rake db:load

rake db:load

回答by DanSingerman

As long as you have not written any SQL statements that run in sqlLite3 and not MySQL (which you won't have if all your database access is via ActiveRecord and ActiveRecord migrations) then all you need to do is change the database adapter in your database.yml config file.

只要您没有编写任何在 sqlLite3 而非 MySQL 中运行的 SQL 语句(如果您的所有数据库访问都是通过 ActiveRecord 和 ActiveRecord 迁移,那么您将不会拥有),那么您需要做的就是更改数据库中的数据库适配器.yml 配置文件。

回答by pantulis

Check Taps. I've successfully converted a Mysql database to Postgres with it --it should support SQLite.

检查水龙头。我已经成功地将 Mysql 数据库转换为 Postgres——它应该支持 SQLite。

Edit: Including working link from cony's comment here.

编辑:包括来自 cony 评论的工作链接。

回答by ScottJ

If there's no data to migrate, simply update database.yml and run 'rake db:schema:load' in the new environment. (NOT db:migrate which should only be used for incremental migrations!)

如果没有数据要迁移,只需更新 database.yml 并在新环境中运行 'rake db:schema:load'。(不是 db:migrate 应该只用于增量迁移!)

回答by Mikaele

myproject  user$ cd
user   $ rails new myproject -d mysql

Say 'no' for all question but for Overwrite .../myproject/config/*database.yml*? (enter "h" for help) [Ynaqdh]say 'yes'.

对所有问题说“不”,但Overwrite .../myproject/config/*database.yml*? (enter "h" for help) [Ynaqdh]要说“是”。