virtualMachinejruby

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:

    development:
      adapter: jdbc
      driver: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/DBNAME_development
      username: APPNAME
      password: PASSWORD
    
    test:
      adapter: jdbc
      driver: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/DBNAME_test
      username: APPNAME
      password: PASSWORD
    
    production:
      adapter: jdbc
      driver: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/DBNAME_production
      username: APPNAME
      password: PASSWORD
    
Last modification September 5, 2018