首页 > 代码库 > Rails连接mysql数据库在ubuntu上跑

Rails连接mysql数据库在ubuntu上跑

Rails安装好 Rails 在ubuntu上安装rails(rvm ruby gem Bootstrap bootswatch)

rails 4.1.4

ruby 2.2.0


  1. sudo apt-get install mysql-server mysql-client 安装mysql(安装过程中设置mysql用户root密码)

  2. sudo apt-get install libmysqlclient-dev (https://github.com/brianmario/mysql2 看这段说明Linux and other Unixes)

  3. mysql -u root -p (用mysql的root用户登录mysql)

  4. create database railstest; (创建数据库)

  5. show databases; (查看数据库)

  6. exit (退出数据库)

  7. rails new trails -d mysql

  8. cd trails

  9. vi config/database.yml (连接数据的文件,修改为以下)

    default: &default
      adapter: mysql2
      encoding: utf8
      pool: 5
      username: root
      password: mysql用户root密码
      socket: /var/run/mysqld/mysqld.sock

    development:
      <<: *default
      database: railstest

  10. vi Gemfile (修改gem ‘mysql2‘)

    gem ‘mysql2‘,‘~> 0.3.20‘

  11. bundle install (安装Gemfile里的软件)

  12. 测试

    ails g scaffold product name:string price:decimal description:text  (创建表)

    rake db:migrate (创建表)


备注:

报错:Specified ‘mysql2‘ for database adapter, but the gem is not loaded.与mysql2的版本有关,参考这里https://github.com/brianmario/mysql2

Ruby on Rails / Active Record

  • mysql2 0.4.x works with Rails / Active Record 4.2.5 - 5.0 and higher.

  • mysql2 0.3.x works with Rails / Active Record 3.1, 3.2, 4.x, 5.0.

  • mysql2 0.2.x works with Rails / Active Record 2.3 - 3.0.




本文出自 “xs笔记” 博客,谢绝转载!

Rails连接mysql数据库在ubuntu上跑