virtualMachineredmine

Create a new MySQL database for a Rails application

To create a new MySQL database for a Rails application:

  • Start the MySQL command line client, as shown below. Enter the password for the MySQL root user.

      $ mysql -u root -p
    
  • At the MySQL prompt, run the following commands. Remember to replace the DBNAME, APPNAME and PASSWORD placeholders with actual values for your database name, application name and database user password.

      mysql> CREATE DATABASE IF NOT EXISTS DBNAME_production;
      mysql> CREATE DATABASE IF NOT EXISTS DBNAME_development;
      mysql> CREATE DATABASE IF NOT EXISTS DBNAME_test;
      mysql> GRANT ALL PRIVILEGES on DBNAME_test.* to 'APPNAME'@'localhost' identified by 'PASSWORD';
      mysql> GRANT ALL PRIVILEGES on DBNAME_production.* to 'APPNAME'@'localhost';
      mysql> GRANT ALL PRIVILEGES on DBNAME_development.* to 'APPNAME'@'localhost';
      mysql> FLUSH PRIVILEGES;
    
  • Edit the config/database.yml file in your Rails project directory and update the database configuration. You can also copy the database.yml file from a sample or fresh project and modify the database details as needed:

      default: &default
        adapter: mysql
        pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
        username: APPNAME
        password: PASSWORD
        host: localhost
    
      development:
        <<: *default
        database: APPNAME_development
    
      test:
        <<: *default
        database: APPNAME_test
    
      production:
        <<: *default
        database: APPNAME_production
    
Last modification December 21, 2022