Rails comes with sqlite3 in every way, but the free version lacks a deadly feature: add password! Although the third party has compiled the two-in-plate encryption version, but we do not toss the bird; go directly to MySQL.
Ubuntu installation MySQL is very simple, do not talk first, first of all, how to change rails to MySQL driver, with one of the following command is sufficient:
Rails new xxx-d MySQL
Because the new version of Rails to support the use of MySQL gem is mysql2, so to add the gem ' Mysql2 ' in the Gemfile, but with the above command, rails will be very clever automatically add this in the Gemfile, so do not bother birds.
Rails naturally wants to download and install the MYSQL2 package, but here will be an error, indicating that the MySQL header file is not found, it appears that you need to download the MySQL dev package and other things, can be installed with the following command:
sudo apt-get install Libmysqlclient-dev
There's no problem with rails new, so let's create a model:
Rails G Scaffold Book name:string Count:integer Price:integer remark:string
Here's a new database to create:
Rake Db:migrate
Error bird, prompt Unable to connect to Mysql,mysql service is not listening! OK, let's start installing the MySQL server side:
sudo apt-get install Mysql-server
Next is the MySQL client:
Apt-get Isntall mysql-client
When installing the server side will prompt for the root password, I am here to enter the ABC, if it is a pure number (such as 123), in the Rails database.yml configuration in the ' 123 ' (I did not try, the experience of other people on the Internet). After installing the server side can use the following command to determine whether the MySQL background to start listening:
sudo netstat-tap|grep MySQL
If the MySQL server is installed and listening, you can now create a database in rails:
Rake Db:create-v
Rake Db:migrate
MySQL database path location under Linux in:/var/lib/mysql
We can confirm the established MySQL database; Open the terminal and enter the command:
Mysql-u root-p
MySQL will prompt to enter the password to log in, enter the password we previously set ABC, successfully log on to MySQL; then you can use show databases to view the existing database:
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| Information_schema |
| db_test_development |
| Db_test_test |
| MySQL |
| Performance_schema |
+---------------------+
5 rows in Set (0.00 sec)
Select a current database using the use MySQL command:
mysql> use db_test_development;
Reading table information for completion of table and column names
Can turn off this feature to get a quicker startup with-a
Database changed
You can look at all the tables in the database:
Mysql> Show tables;
+-------------------------------+
| tables_in_db_test_development |
+-------------------------------+
| Books |
| schema_migrations |
+-------------------------------+
2 rows in Set (0.00 sec)
Finally, let's look at the structure of the books table:
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)
ubuntu14.04 using rails to connect to MySQL database