Crea una nuova applicazione Ruby on Rails utilizzando MySQL anziché SQLite
-
08-06-2019 - |
Domanda
Voglio creare la mia applicazione Rails con MySQL, perché mi piace così tanto.Come posso farlo nell'ultima versione di Rails invece che in SQLite predefinita?
Soluzione
Se hai già un progetto di binari, cambia l'adattatore nel config/database.yml
file a mysql
e assicurati di specificare un nome utente e una password validi e, facoltativamente, un socket:
development:
adapter: mysql2
database: db_name_dev
username: koploper
password:
host: localhost
socket: /tmp/mysql.sock
Successivamente, assicurati di modificare il tuo Gemfile per includere l'adattatore mysql2 o activerecord-jdbcmysql (se usi jruby).
Altri suggerimenti
Normalmente, creeresti una nuova app Rails utilizzando
rails ProjectName
Per utilizzare MySQL, utilizzare
rails new ProjectName -d mysql
Per Rails 3 puoi utilizzare questo comando per creare un nuovo progetto utilizzando mysql:
$ rails new projectname -d mysql
Vai al terminale e scrivi:
rails new <project_name> -d mysql
Se non hai ancora creato la tua app, vai su cmd (per Windows) o terminale (per Linux/Unix) e digita il seguente comando per creare un'applicazione rails con il database MySQL:
$rails new <your_app_name> -d mysql
Funziona con qualsiasi cosa sopra i binari versione 3.Se hai già creato la tua app, puoi eseguire una delle 2 operazioni seguenti:
- Creare un un altro nome app con database mysql, vai su cd un altro nome/config/ e copia il file database.yml da questa nuova app.Incollalo nel database.yml di tuo_nome_app app.Assicurati però di modificare i nomi dei database e di impostare nome utente/password del tuo database di conseguenza nel file database.yml dopo averlo fatto.
O
- Vai al cd tuo_nome_app/config/ e apri database.yml.Rinominare come segue:
sviluppo:
adattatore:mysql2
Banca dati:nome_db_nome
nome utente:radice
parola d'ordine:
ospite:localhost
PRESA:/tmp/mysql.sock
Inoltre, rimuovi la gemma 'sqlite3' dal tuo Gemfile e aggiungi la gemma 'mysql2'
Se utilizzi la versione Rails 3 o successiva
rails new your_project_name -d mysql
se hai una versione precedente
rails new -d mysql your_project_name
Quindi prima di creare il tuo progetto devi trovare la versione dei binari.che puoi trovare da
rails -v
rails -d mysql ProjectName
rails new <project_name> -d mysql
O
rails new projectname
Modifiche in config/database.yml
development:
adapter: mysql2
database: db_name_name
username: root
password:
host: localhost
socket: /tmp/mysql.sock
Crea un'applicazione con l'opzione -d
rails new AppName -d mysql
$ rails --help
è sempre il tuo migliore amico
utilizzo:
$ rails new APP_PATH[options]
si noti inoltre che le opzioni dovrebbero essere fornite dopo il nome dell'applicazione
binari e mysql
$ rails new project_name -d mysql
rail e postgresql
$ rails new project_name -d postgresql
Dovresti usare l'opzione -D invece di -d perché genererà due app e mysql senza cartelle di documentazione.
rails -D mysql project_name (less than version 3)
rails new project_name -D mysql (version 3 and up)
In alternativa basta usare il file --database
opzione.
Basta andare sulla console dei binari e digitare:
rails new YOURAPPNAME -d mysql
In Rails 3, potresti farlo
$rails new projectname --database=mysql
Se stai creando una nuova applicazione di binari puoi impostare il database utilizzando l'opzione -d in questo modo:
rails -d mysql myapp
Tuttavia, è sempre facile cambiare database in un secondo momento e utilizzare sqlite è davvero più semplice se stai sviluppando su un Mac.
Sul nuovo progetto, facile facile:
rails new your_new_project_name -d mysql
Sul progetto esistente, decisamente più complicato.Ciò mi ha dato una serie di problemi sui progetti ferroviari esistenti.Questo tipo di lavoro con me:
# On Gemfile:
gem 'mysql2', '>= 0.3.18', '< 0.5' # copied from a new project for rails 5.1 :)
gem 'activerecord-mysql-adapter' # needed for mysql..
# On Dockerfile or on CLI:
sudo apt-get install -y mysql-client libmysqlclient-dev
Per prima cosa assicurati che mysql gem sia installato, altrimenti?piuttosto che digitare il comando seguente nella console
gem install mysql2
Quindi crea una nuova app rails e imposta il database mysql come database predefinito digitando il seguente comando nella console
rails new app-name -d mysql
Utilizza il seguente comando per creare una nuova app per API con il database mysql
rails new <appname> --api -d mysql
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
database.yml
# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: localhost
database: database_name
username: username
password: secret
development:
<<: *default
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
Gemfile:
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'