Ruby on Rails 使用 SQLite3 作为其默认数据库。 虽然 Sqlite 与 Rails 配合得很好,但有时它可能不足以满足您的 Rails 应用程序。 如果您想要可扩展性、并发性、集中化和控制,您可能想尝试更强大的数据库,如 MySQL 或 PostgreSQL。 在本指南中,我们将看到如何在 Ubuntu Linux 中使用 MySQL 和 Ruby on Rails 应用程序。
内容
1. 在 Ubuntu 中安装 MySQL
MySQL 在大多数 Linux 和类 Unix 发行版的默认存储库中可用。
要在 Debian、Ubuntu 及其衍生产品上安装 MySQL,请运行:
$ sudo apt install mysql-server mysql-client libmysqlclient-dev
这 libmysqlclient-dev
包提供了编译所需的文件 mysql2
宝石。 Ruby on Rails 使用 mysql2
gem 在您设置 Rails 应用程序时连接到 MySQL。
1.1. 设置 MySQL Root 密码
接下来,通过运行以下命令设置 MySQL root 用户密码:
$ sudo mysql_secure_installation
Enter “和” 设置验证密码组件:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
选择密码验证级别。 可用的密码验证是 低的, 中等的 和 强的. 在这里,我通过输入零 (0) 来选择低级别密码验证。
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
接下来,输入两次 MySQL Root 密码。 密码应该是强密码并且至少应该有 8 个字符。 按 和 接着说:
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
接下来,您将被问到一系列问题。 类型 和 并为每个问题按 ENTER。 这将删除匿名用户,禁止 root 用户远程登录并删除测试数据库。
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done!
完毕! 我们已经设置了 MySQL root 用户的密码。
1.2. 安装 MySQL 驱动程序
安装名为的 MySQL 驱动程序 mysql2
gem 允许 Rails 应用程序连接到 MySQL 数据库。
$ gem install mysql2
全部完成! Rails 应用程序现在可以连接到 MySQL 数据库服务器。
让我们继续前进并使用 MySQL 数据库创建一个新的 Rails 应用程序。
2. 在 Ruby on Rails 应用程序中使用 MySQL
创建一个名为的新 Rails 应用程序 ostechnixapp
并使用 MySQL 数据库作为其默认数据库,运行:
$ rails new ostechnixapp -d mysql
这将在名为 ostechnixapp 的目录中创建一个名为 ostechnixapp 的 Rails 应用程序 ostechnixapp
并安装已经提到的 gem 依赖项 Gemfile
使用 bundle install
.
创建后 ostechnixapp
应用程序,切换到其目录:
$ cd ostechnixapp/
编辑应用程序的数据库配置文件:
$ vi config/database.yml
在默认部分下,替换 MySQL root
用户密码与您之前创建的密码相同:
[...] default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: Password123#@! socket: /var/run/mysqld/mysqld.sock [...]
Save 和 close 文件。
现在使用命令为您的 Rails 应用程序创建新数据库:
$ rake db:create
这将使用您的 Rails 应用程序的名称创建两个数据库。 为了 example, 如果应用程序名称是 ostechnixapp, 那么它会创建 ostechnixapp_development
和 ostechnixapp_test
.
您也可以验证是否从 MySQL 成功创建了数据库。
以身份登录 MySQL root
用户:
$ mysql -u root -p
通过输入以下命令验证是否创建了数据库:
mysql> show databases;
示例输出:
+--------------------------+
| Database |
+--------------------------+
| information_schema |
| mysql |
| ostechnixapp_development |
| ostechnixapp_test |
| performance_schema |
| sys |
+--------------------------+
6 rows in set (0.01 sec)
mysql>
Exit 从 MySQL 提示符。
现在使用以下命令启动您的 Rails Web 服务器:
$ rails server -b 0.0.0.0
这将在默认端口上启动您的 Rails 应用程序 3000
. 在这里,我们传递了参数 -b 0.0.0.0
为了从局域网上的任何机器访问 Rails 应用程序。
您可以通过导航到以下 URL 在 Web 浏览器上访问 Rails 测试页面: https://ip-address:3000

如果你想使用不同的端口,通过 -p 标志以及端口号,如下所示:
$ rails server -b 0.0.0.0 -p 8080
现在,您可以使用以下 URL 访问您的 Rails 应用程序: https://ip-address:8080
3.删除Rails应用程序
如果您使用了默认数据库 SQLite,则只需删除其 app 目录即可删除 Rails 应用程序。
$ rm -fr ostechnixapp
如果您使用过 MySQL,首先您需要使用以下命令删除应用程序的相关数据库:
$ rake db:drop
然后删除应用程序目录:
$ rm -fr ostechnixapp
结论
在本教程中,我们讨论了为什么需要为 Rails 应用程序使用客户端/服务器模型数据库以及如何使用 MySQL 数据库创建 Rails 应用程序。 我们还看到了如何删除 Rails 应用程序。
如果多个 Rails 应用程序副本同时运行,多个应用程序和用户使用同一个数据库,您应该远离 Sqlite 并尝试像 MySQL 这样的客户端/服务器模型数据库。 如果您想为单个应用程序进行本地数据存储,Sqlite 绰绰有余。
相关阅读:
- 如何在 Ruby On Rails 应用程序中使用 PostgreSQL
- 使用 Rails 中的环境变量配置数据库连接