Java Developer

Configuring MySQL JDBC connection for Grails

During development, I recommend using a standalone database server like MySQL, instead of using the default HSQLDB or H2 database. Because you can open a client and inspect the table structure created, and also perform queries outside the Grails application.
To configure MySQL in your datasource, first download the driver from: http://dev.mysql.com/downloads/connector/j/

Extract the jar file from the compressed package. The file is something like mysql-connector-java-x.x.xx-bin.jar. Place it under the lib folder of your Grails project:
grails-mysql

Assuming your MySQL user is root with password secret, create the database:

$ mysql -u root -psecret
mysql> create schema myprojdb;
mysql> exit;

Open up your DataSource.groovy and under development section:

environments {
    development {
        dataSource {
            username = "root"
            password = "secret"
            dbCreate = "update"
            url = "jdbc:mysql://localhost:3306/myprojdb"
        }
    }
    ...
}

You may also want to check my previous post on how to use JNDI datasource with Grails.
Tags: database, grails, jdbc, mysql