Can not connect to mysql

hello
I am new in moqui and when I want to configure mysql database in devconfig under runtime the below error will happen
Error connecting to DataSource transactional (mysql), try 4 of 5: java.lang.ClassNotFoundException: Class [com.mysql.jdbc.jdbc2.optional.MysqlXADataSource] not found
can any one help me solve the problem ?

Welcome!

Have you added the mysql JDBC driver to your runtime/lib directory?

You also need to make sure that you have something like this in your moqui.conf file:

    <!-- Properties for the primary (transactional group) datasource -->
    <default-property name="entity_ds_db_conf" value="mysql"/>
    <default-property name="entity_ds_host" value="localhost"/>
    <default-property name="entity_ds_port" value="3306"/>
    <default-property name="entity_ds_database" value="mysql"/>
    <default-property name="entity_ds_url" value="jdbc:${moqui_runtime}/lib/mysql-jdbc-version.jar"/>
    <default-property name="entity_ds_user" value="mysql"/>
    <default-property name="entity_ds_password" value="changeme"/>
    <default-property name="entity_ds_crypt_pass" value="changeMeToSomethingDifferent"/>

Hope that helps :slight_smile:

BTW unless you’re doing a production install, I would suggest to just use the default H2 database.

Thank you so much for your answer
as you can see in below images I add datasource to devConf and as you mention , there is such definition in default conf file but not still work

I believe you can’t connect because you don’t have a database with the username of root and an unspecified password. To connect to a database you need to have a database setup. Before having moqui connect to it, try to connect to it yourself as a test. Once you have ensured that your database is setup, go over the configuration files carefully and make sure that your credentials, connection type, and port are correct.

1 Like

The problem was when I use below format to config:
<inline-jdbc><xa-properties user="root" password="" pinGlobalTxToPhysicalConnection="true" serverName="localhost" port="3306" databaseName="mahdi" autoReconnectForPools="true" useUnicode="true" encoding="UTF-8"/></inline-jdbc>
so I change my config method to below code and it work:
<inline-jdbc jdbc-uri="jdbc:mysql://localhost:3306/mahdi?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8" jdbc-username="root" jdbc-password=""/>

1 Like

I’m glad you got it to work!

1 Like

Thanks so much for your helping

1 Like

yeah of course glad to help

1 Like

I don’t know if this will be helpful, but some things to consider:

  1. a ClassNotFoundException means what it says, and in this case may mean you have the wrong version of the JDBC driver, one that does not have the com.mysql.jdbc.jdbc2.optional.MysqlXADataSource class; the solution is make sure the correct JDBC driver is on the classpath
  2. using the inline-jdbc attributes instead of the xa-properties element is not recommended because it forces the transaction manager to wrap the simpler non-XA DataSource in an XA DataSource; this works in general, except when you need 2PC for transactions across more than one database, but it will not run as efficiently as JDBC drivers implement database-specific ways of handling transaction management behavior that are generally better than the generic stuff in the transaction manager (Bitronix by default in Moqui)
2 Likes

Thanks ,
So you suggest me to use XA DataSource in general ?
I put mysql-connector-java-8.0.22 in my runtime\lib folder
Can you help me How can I found the correct version of JDBC for my project ?
and I have another question ,
when I config database and then use gradlew load after that when I open localhost8080 the create initial admin account form open and I set the values and create account but it does not work and show same form again and I can not login to moqui
do you have any idea about that how can I solve it ?