July 10, 2017

Install MariaDB (MySQL Open Source) on RHEL 7 and JBoss EAP 7 DataSource

First what is MariaDB? "MariaDB is a community-developed fork of the MySQL relational database management system intended to remain free under the GNU GPL. Development is led by some of the original developers of MySQL, who forked it due to concerns over its acquisition by Oracle Corporation." [https://en.wikipedia.org/wiki/MariaDB]

Now lets install MariaDB Server (mariadb-server) and Client (mariadb) on RHEL 7.

yum install mariadb-server mariadb -y

And start it.

# systemctl start mariadb

When installing MariaDB fresh on RHEL7 there is a default user 'root' with empty password. To test this execute as user jboss and run mysql select on system table user:

# sudo -u jboss mysql -u root -e "select Host, User, Password from mysql.user;"

We will now install MySQL JDBC driver to JBoss and configure a test DataSource following https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html-single/configuration_guide/#example_mysql_datasource

# yum install mysql-connector-java -y

After installing the Official JDBC driver for MySQL, we create the EAP module.

# mkdir -p /opt/rh/eap7/root/usr/share/wildfly/modules/com/mysql/main
# ln -s /usr/share/java/mysql-connector-java.jar /opt/rh/eap7/root/usr/share/wildfly/modules/com/mysql/main

# vi /opt/rh/eap7/root/usr/share/wildfly/modules/com/mysql/main/module.xml


  
    
  
  
    
    
  


# chown jboss:jboss -Rv /opt/rh/eap7/root/usr/share/wildfly/modules/com
# restorecon -RFv /opt/rh/eap7/root/usr/share/wildfly/modules/

Now configure EAP 7 datasource.

# vi /opt/rh/eap7/root/usr/share/wildfly/standalone/configuration/standalone.xml
...
        <subsystem xmlns="urn:jboss:domain:datasources:4.0">
            <datasources>
                ...
                <datasource jndi-name="java:jboss/MySqlDS" pool-name="MySqlDS">
                    <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
                    <driver>mysql</driver>
                    <security>
                        <user-name>root</user-name>
                        <password></password>
                    </security>
                    <validation>
                        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
                        <validate-on-match>true</validate-on-match>
                        <background-validation>false</background-validation>
                        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
                    </validation>
                </datasource>
                <drivers>
                    <driver name="mysql" module="com.mysql">
                        <driver-class>com.mysql.jdbc.Driver</driver-class>
                        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                    </driver>
                    ...
                </drivers>
            </datasources>
        </subsystem>
...

Now lets create a simple web, just containing one JSP file, so inside the war file there is only one JSP file. The code I copied from http://www.java2s.com/Code/Java/JSP/UsingaDataSource.htm, but for it to work with your datasource you need to change the JNDI lookup code and sql statement to.

Context context = new InitialContext();
ds =  (DataSource)context.lookup("java:jboss/MySqlDS");
if (ds != null) {
    conn = ds.getConnection();
    stmt = conn.createStatement();
    result = stmt.executeQuery("select Host, User, Password from mysql.user");
}

No comments: