Ubuntu 14.04使用Rails连接MySQL数据库
rails自带的sqlite3各方面都不错,但是免费版缺少一个致命功能:加密码!虽说第三方有编译好的二进制版的加密版,但咱先不折腾鸟;直接
rails自带的sqlite3各方面都不错,但是免费版缺少一个致命功能:加密码!虽说第三方有编译好的二进制版的加密版,但咱先不折腾鸟;直接上mysql吧.
Ubuntu安装mysql非常简单,先不聊;先说一下rails如何改为mysql驱动,用以下一条命令足矣:
rails new xxx -d mysql
因为新版的rails对mysql的支持使用的gem是mysql2,所以要在Gemfile中加上gem 'mysql2'一句;不过有了上面那条命令,rails会很聪明的自动在Gemfile中加上这句,so不用劳神鸟.
rails自然要下载安装mysql2包,不过这里会报错,提示找不到mysql的头文件,看来需要下载mysql dev包之类的东西,可以用如下命令安装:
sudo apt-get install libmysqlclient-dev
这次rails new没问题了,接下来我们创建一个model:
rails g scaffold book name:string count:integer price:integer remark:string
下面要创建一个新的数据库:
rake db:migrate
出错鸟,提示无法连接到mysql,mysql服务不在监听状态!好吧,我们现在开始安装mysql服务器端吧:
sudo apt-get install mysql-server
接着是mysql的客户端:
apt-get isntall mysql-client
在安装服务器端时会提示输入root密码,我这里输入的是abc,如果是纯数字的话(比如123),在rails的database.yml配置中要用'123'括起来(具体我也没有试,网上其他人的经验之谈).安装完服务器端可以用如下命令确定是否mysql后台开始监听:
sudo netstat -tap|grep mysql
如果mysql服务器安装完毕且处于监听状态的话,现在可以创建rails中的数据库啦:
rake db:create -v
rake db:migrate
linux下mysql数据库路径位置在:/var/lib/mysql
我们可以确认下建立的的mysql数据库;打开终端,输入指令:
mysql -u root -p
mysql会提示输入密码来登陆,输入我们之前设定的密码abc,成功登陆mysql;接下来可以使用show databases;查看存在的数据库:
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| db_test_development |
| db_test_test |
| mysql |
| performance_schema |
+---------------------+
5 rows in set (0.00 sec)
使用use mysql命令选择一个当前的数据库:
mysql> use db_test_development;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
可以看一下数据库中的所有表:
mysql> show tables;
+-------------------------------+
| Tables_in_db_test_development |
+-------------------------------+
| books |
| schema_migrations |
+-------------------------------+
2 rows in set (0.00 sec)
最后让我们看一下books表的结构:
mysql> show columns from books;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| count | int(11) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| remark | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
Ubuntu下Apache服务器安装以及使用Passenger插件部署Rails应用
CentOS系统搭建Ruby On Rails平台
Ubuntu下搭建Ruby On Rails
《Web开发敏捷之道应用Rails进行敏捷Web开发(原书第4版)》.((美)Sam Ruby).[PDF]+源代码
Rails 的详细介绍:请点这里
Rails 的下载地址:请点这里
本文永久更新链接地址:
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]
