Connecting MySql to Grails

By default, Grails applications are configured to use an in-memory HSQLDB database.

Steps to connect to MySql database:

1. Create database to be used.

create database database_name

2. Create your application.

grails create-app app_name

3. After creating app, go to BuildConfig.groovy file and add following:

     dependencies {

         runtime ‘mysql:mysql-connector-java:5.1.24′

}

Uncomment maven repository if it is commented.

4. Now update datasource.groovy file.

 dataSource {
   pooled = true
driverClassName = “com.mysql.jdbc.Driver”
dialect = “org.hibernate.dialect.MySQL5InnoDBDialect”
//logSql=true
}

5. For environment specific settings:

      environments {
                     development {
                                  dataSource {
                                            dbCreate = “update” // one of ‘create’, ‘create-drop’, ‘update’, ‘validate’, ”
                                            url = “jdbc:mysql://localhost/database_name?                     useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true”
                                            username = “XXX”
                                            password = “XXX”
                                                   }
                      }
test {
                     dataSource {
                               dbCreate = “update”
                                url = “jdbc:mysql://localhost/database_name?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true”
                               username = “XXX”
                               password = “XXX”
                      }
}
production {
                    dataSource {
                               dbCreate = “update”
                               url = “jdbc:mysql://localhost/database_name?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true”
                              username = “XXX”
                              password = “XXX”
                              pooled = true

                            }
             }
      }
}

 


ProsperaSoft offers Grails development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Grails experts and consultants.