I want to create my Rails application with MySQL, because I like it so much. How can I do that in the latest version of Rails instead of the default SQLite?
original title: "Create a new Ruby on Rails application using MySQL instead of SQLite"
I want to create my Rails application with MySQL, because I like it so much. How can I do that in the latest version of Rails instead of the default SQLite?
Ich möchte meine Rails-Anwendung mit MySQL erstellen, weil es mir so gut gefällt. Wie kann ich das in der neuesten Version von Rails anstelle der Standard-SQLite tun?
Dies ist die Zusammenfassung nach der Übersetzung. Wenn Sie die vollständige Übersetzung anzeigen möchten, klicken Sie auf das Symbol "Übersetzen"
If you already have a rails project, change the adapter in the
config/database.yml
file tomysql
and make sure you specify a valid username and password, and optionally, a socket:Next, make sure you edit your Gemfile to include the mysql2 or activerecord-jdbcmysql-adapter (if using jruby).
Normally, you would create a new Rails app using
To use MySQL, use
For Rails 3 you can use this command to create a new project using mysql:
Go to the terminal and write:
If you have not created your app yet, just go to cmd(for windows) or terminal(for linux/unix) and type the following command to create a rails application with mysql database:
$rails new <your_app_name> -d mysql
It works for anything above rails version 3. If you have already created your app, then you can do one of the 2 following things:
OR
development:
adapter: mysql2
database: db_name_name
username: root
password:
host: localhost
socket: /tmp/mysql.sock
Moreover, remove gem 'sqlite3' from your Gemfile and add the gem 'mysql2'
If you are using rails 3 or greater version
if you have earlier version
So before you create your project you need to find the rails version. that you can find by
OR
Changes in config/database.yml
Create application with -d option
is always your best friend
usage:
also note that options should be given after the application name
rails and mysql
rails and postgresql
You should use the switch -D instead of -d because it will generate two apps and mysql with no documentation folders.
Alternatively you just use the
--database
option.Just go to rails console and type:
In Rails 3, you could do
If you are creating a new rails application you can set the database using the -d switch like this:
Its always easy to switch your database later though, and using sqlite really is easier if you are developing on a Mac.
On new project, easy peasy:
On existing project, definitely trickier. This has given me a number of issues on existing rails projects. This kind of works with me:
First make sure that mysql gem is installed, if not? than type following command in your console
Than create new rails app and set mysql database as default database by typing following command in your console
Use following command to create new app for API with mysql database
database.yml
Gemfile: