<p>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?</p>

[{'answer_id': 3586, 'author': 'Daniel Broekman', 'author_id': 124, 'author_profile': 'https://Stackoverflow.com/users/124', 'pm_score': 8, 'selected': False, 'text': '<p>Normally, you would create a new Rails app using</p>\n\n<pre><code>rails ProjectName\n</code></pre>\n\n<p>To use MySQL, use</p>\n\n<pre><code>rails new ProjectName -d mysql\n</code></pre>\n'}, {'answer_id': 3588, 'author': 'James Avery', 'author_id': 537, 'author_profile': 'https://Stackoverflow.com/users/537', 'pm_score': 3, 'selected': False, 'text': '<p>If you are creating a new rails application you can set the database using the -d switch like this:</p>\n\n<pre><code>rails -d mysql myapp\n</code></pre>\n\n<p>Its always easy to switch your database later though, and using sqlite really is easier if you are developing on a Mac.</p>\n'}, {'answer_id': 3601, 'author': 'Michiel de Mare', 'author_id': 136, 'author_profile': 'https://Stackoverflow.com/users/136', 'pm_score': 8, 'selected': True, 'text': '<p>If you already have a rails project, change the adapter in the <code>config/database.yml</code> file to <code>mysql</code> and make sure you specify a valid username and password, and optionally, a socket:</p>\n\n<pre><code>development:\n  adapter: mysql2\n  database: db_name_dev\n  username: koploper\n  password:\n  host: localhost\n  socket: /tmp/mysql.sock\n</code></pre>\n\n<p>Next, make sure you edit your Gemfile to include the mysql2 or activerecord-jdbcmysql-adapter (if using jruby).</p>\n'}, {'answer_id': 792421, 'author': 'huacnlee', 'author_id': 83558, 'author_profile': 'https://Stackoverflow.com/users/83558', 'pm_score': 4, 'selected': False, 'text': '<pre><code>rails -d mysql ProjectName\n</code></pre>\n'}, {'answer_id': 4438499, 'author': 'Robbie Done', 'author_id': 541839, 'author_profile': 'https://Stackoverflow.com/users/541839', 'pm_score': 6, 'selected': False, 'text': '<p>For Rails 3 you can use this command to create a new project using mysql:</p>\n\n<pre><code>$ rails new projectname -d mysql\n</code></pre>\n'}, {'answer_id': 6046965, 'author': 'andy318', 'author_id': 204180, 'author_profile': 'https://Stackoverflow.com/users/204180', 'pm_score': 3, 'selected': False, 'text': '<p>In Rails 3, you could do</p>\n\n<pre><code>$rails new projectname --database=mysql\n</code></pre>\n'}, {'answer_id': 6936105, 'author': 'Coder', 'author_id': 876011, 'author_profile': 'https://Stackoverflow.com/users/876011', 'pm_score': 4, 'selected': False, 'text': '<p>If you are using rails 3 or greater version</p>\n\n<pre><code>rails new your_project_name -d mysql\n</code></pre>\n\n<p>if you have earlier version </p>\n\n<pre><code>rails new -d mysql your_project_name\n</code></pre>\n\n<p>So before you create your project you need to find the rails version. that you can find by</p>\n\n<pre><code>rails -v\n</code></pre>\n'}, {'answer_id': 8183962, 'author': 'George Bellos', 'author_id': 89724, 'author_profile': 'https://Stackoverflow.com/users/89724', 'pm_score': 3, 'selected': False, 'text': '<pre><code>$ rails --help \n</code></pre>\n\n<p>is always your best friend</p>\n\n<p>usage:</p>\n\n<pre><code>$ rails new APP_PATH[options]\n</code></pre>\n\n<p>also note that options should be given after the application name</p>\n\n<p>rails and mysql</p>\n\n<pre><code>$ rails new project_name -d mysql\n</code></pre>\n\n<p>rails and postgresql</p>\n\n<pre><code>$ rails new project_name -d postgresql\n</code></pre>\n'}, {'answer_id': 9921786, 'author': 'Marthinus A. Botha', 'author_id': 1300257, 'author_profile': 'https://Stackoverflow.com/users/1300257', 'pm_score': 3, 'selected': False, 'text': '<p>You should use the switch -D instead of -d because it will generate two apps  and mysql with no documentation folders.</p>\n\n<pre><code>  rails -D mysql project_name  (less than version 3)\n\n  rails new project_name -D mysql (version 3 and up)\n</code></pre>\n\n<p>Alternatively you just use the <code>--database</code> option.</p>\n'}, {'answer_id': 11138212, 'author': 'vijay chouhan', 'author_id': 2079997, 'author_profile': 'https://Stackoverflow.com/users/2079997', 'pm_score': 4, 'selected': False, 'text': '<pre><code>rails new &lt;project_name&gt; -d mysql\n</code></pre>\n\n<p>OR</p>\n\n<pre><code>rails new projectname\n</code></pre>\n\n<p>Changes in config/database.yml</p>\n\n<pre><code>development:\n  adapter: mysql2\n  database: db_name_name\n  username: root\n  password:\n  host: localhost\n  socket: /tmp/mysql.sock\n</code></pre>\n'}, {'answer_id': 14438074, 'author': 'Abhinav', 'author_id': 1996835, 'author_profile': 'https://Stackoverflow.com/users/1996835', 'pm_score': 5, 'selected': False, 'text': '<p>Go to the terminal and write:</p>\n\n<pre><code>rails new &lt;project_name&gt; -d mysql\n</code></pre>\n'}, {'answer_id': 14440100, 'author': 'Dipali Nagrale', 'author_id': 1645570, 'author_profile': 'https://Stackoverflow.com/users/1645570', 'pm_score': 4, 'selected': False, 'text': '<p>Create application with -d option</p>\n\n<pre><code>rails new AppName -d mysql\n</code></pre>\n'}, {'answer_id': 24365127, 'author': 'Drake Mandin', 'author_id': 3767282, 'author_profile': 'https://Stackoverflow.com/users/3767282', 'pm_score': 5, 'selected': False, 'text': "<p>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:</p>\n\n<p><code>$rails new &lt;your_app_name&gt; -d mysql</code></p>\n\n<p>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:</p>\n\n<ol>\n<li>Create a <i>another_name</i> app with mysql database, go to cd <i>another_name</i>/config/ and copy the database.yml file from this new app. Paste it into the database.yml of <i>your_app_name</i> app. But ensure to change the database names and set username/password of your database accordingly in the database.yml file after doing so.</li>\n</ol>\n\n<p>OR</p>\n\n<ol>\n<li>Go to cd <i>your_app_name</i>/config/ and open database.yml. Rename as following:</li>\n</ol>\n\n<p>development:<br>\n  adapter: mysql2 <br>\n  database: db_name_name<br>\n  username: root<br>\n  password:<br>\n  host: localhost<br>\n  socket: /tmp/mysql.sock<br></p>\n\n<p>Moreover, remove gem 'sqlite3' from your Gemfile and add the gem 'mysql2'</p>\n"}, {'answer_id': 42243150, 'author': 'Amarpreet Jethra', 'author_id': 6375692, 'author_profile': 'https://Stackoverflow.com/users/6375692', 'pm_score': 3, 'selected': False, 'text': '<p>Just go to rails console and type:</p>\n\n<pre><code>rails new YOURAPPNAME -d mysql\n</code></pre>\n'}, {'answer_id': 46322499, 'author': 'Shabbir', 'author_id': 8572496, 'author_profile': 'https://Stackoverflow.com/users/8572496', 'pm_score': 2, 'selected': False, 'text': '<p>First make sure that mysql gem is installed, if not? than type following command in your console</p>\n\n<pre><code>gem install mysql2\n</code></pre>\n\n<p>Than create new rails app and set mysql database as default database by typing following command in your console</p>\n\n<pre><code>rails new app-name -d mysql\n</code></pre>\n'}, {'answer_id': 48695295, 'author': 'Riccardo', 'author_id': 362420, 'author_profile': 'https://Stackoverflow.com/users/362420', 'pm_score': 2, 'selected': False, 'text': "<p>On new project, easy peasy: </p>\n\n<pre><code>rails new your_new_project_name -d mysql\n</code></pre>\n\n<p>On existing project, definitely trickier. This has given me a number of issues on existing rails projects. This kind of works with me:</p>\n\n<pre><code># On Gemfile:\ngem 'mysql2',  '&gt;= 0.3.18', '&lt; 0.5' # copied from a new project for rails 5.1 :)\ngem 'activerecord-mysql-adapter' # needed for mysql..\n\n# On Dockerfile or on CLI:\nsudo apt-get install -y  mysql-client libmysqlclient-dev \n</code></pre>\n"}, {'answer_id': 52131248, 'author': 'Dinesh Vaitage', 'author_id': 5710925, 'author_profile': 'https://Stackoverflow.com/users/5710925', 'pm_score': 0, 'selected': False, 'text': '<p><strong>Use following command to create new app for API with mysql database</strong></p>\n\n<pre><code>rails new &lt;appname&gt; --api -d mysql\n\n\n  adapter: mysql2\n  encoding: utf8\n  pool: 5\n  username: root\n  password: \n  socket: /var/run/mysqld/mysqld.sock\n</code></pre>\n'}, {'answer_id': 54820357, 'author': 'artamonovdev', 'author_id': 5754223, 'author_profile': 'https://Stackoverflow.com/users/5754223', 'pm_score': 0, 'selected': False, 'text': '<p><strong>database.yml</strong></p>\n\n<pre><code># MySQL. Versions 5.1.10 and up are supported.\n#\n# Install the MySQL driver\n#   gem install mysql2\n#\n# Ensure the MySQL gem is defined in your Gemfile\n#   gem \'mysql2\'\n#\n# And be sure to use new-style password hashing:\n#   https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html\n#\ndefault: &amp;default\n  adapter: mysql2\n  encoding: utf8\n  pool: &lt;%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %&gt;\n  host: localhost\n  database: database_name\n  username: username\n  password: secret\n\ndevelopment:\n  &lt;&lt;: *default\n\n# Warning: The database defined as "test" will be erased and\n# re-generated from your development database when you run "rake".\n# Do not set this db to the same as development or production.\ntest:\n  &lt;&lt;: *default\n\n# As with config/secrets.yml, you never want to store sensitive information,\n# like your database password, in your source code. If your source code is\n# ever seen by anyone, they now have access to your database.\n#\n# Instead, provide the password as a unix environment variable when you boot\n# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database\n# for a full rundown on how to provide these environment variables in a\n# production deployment.\n#\n# On Heroku and other platform providers, you may have a full connection URL\n# available as an environment variable. For example:\n#\n#   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"\n#\n# You can use this database configuration with:\n#\n#   production:\n#     url: &lt;%= ENV[\'DATABASE_URL\'] %&gt;\n#\nproduction:\n  &lt;&lt;: *default\n</code></pre>\n\n<p><strong>Gemfile</strong>:</p>\n\n<pre><code># Use mysql as the database for Active Record\ngem \'mysql2\', \'&gt;= 0.4.4\', \'&lt; 0.6.0\'\n</code></pre>\n'}, {'answer_id': 61106989, 'author': 'Muhammad Elbadawy', 'author_id': 8111491, 'author_profile': 'https://Stackoverflow.com/users/8111491', 'pm_score': 0, 'selected': False, 'text': "<p>you first should make sure that MySQL driver is on your system if not run this on your terminal if you are using Ubuntu or any Debian distro</p>\n\n<pre><code>sudo apt-get install mysql-client libmysqlclient-dev\n</code></pre>\n\n<p>and add this to your Gemfile</p>\n\n<pre><code>gem 'mysql2', '~&gt; 0.3.16'\n</code></pre>\n\n<p>then run in your root directory of the project </p>\n\n<pre><code>bundle install\n</code></pre>\n\n<p>after that you can add the mysql config to config/database.yml as the previous answers</p>\n"}]
['https://Stackoverflow.com/questions/3585', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/124/']