Fetch MySQL Data Using Database Connector in MuleSoft

Before jumping into this article, I highly recommended reading these articles for a better understanding.

If you would like to understand the basics of MuleSoft, refer to these articles before going next:

Open MySQL Workbench to connect the MySQL database; here is the screenshot of my sample database, which I will use in the MuleSoft connector.

Fetch MySQL Data Using Database Connector in MuleSoft

Getting Started

First, open Anypoint Studio and Create a New Mule Project.

Fetch MySQL Data Using Database Connector in MuleSoft

Enter a project name and selection location and click finish.

Fetch MySQL Data Using Database Connector in MuleSoft

Next, add a New Mule Configuration file for global settings.

Fetch MySQL Data Using Database Connector in MuleSoft

Next, go to the global.xml file, click the Global Elements tab, and add two config files.

Create a button, select HTTP Listener config, click OK, provide given connection values, test connection, and click OK.

Fetch MySQL Data Using Database Connector in MuleSoft

Next, add a new Database config connection.

Fetch MySQL Data Using Database Connector in MuleSoft

Enter the required details like config name, Connection, Library, Host, Port, User, Password, and database, and click the test connection and ok button.

Fetch MySQL Data Using Database Connector in MuleSoft

Both Global Configuration Elements have been created, and you can edit or delete them from here when needed.

Fetch MySQL Data Using Database Connector in MuleSoft

The global configuration is done for HTTP listener; let’s work on Message flow. Drag and drop HTTP Listener from HTTP Palette to Message Flow.

Setup Display Name and select connector configurations and provide endpoint path name.

Fetch MySQL Data Using Database Connector in MuleSoft

Now drag and drop Database Select from Mule Palette and enter the display name and Connector configurations, which we create in global configuration and write the query to fetch the data query text.

Fetch MySQL Data Using Database Connector in MuleSoft

Next, drag and drop Logger from the Core palette and set a Generic message as Json Payload.

Fetch MySQL Data Using Database Connector in MuleSoft

Fetch MySQL Data Using Database Connector in MuleSoft

Now, finally setup a Payload. Drag and Drop Set Payload from the Core palette and setup Payload value in Json format.

Fetch MySQL Data Using Database Connector in MuleSoft

Fetch MySQL Data Using Database Connector in MuleSoft

That’s it. Now run the application.

Fetch MySQL Data Using Database Connector in MuleSoft

Open Postman and enter Endpoint to see the output result.

Fetch MySQL Data Using Database Connector in MuleSoft

Conclusion

In this article, we learned how to setup a database connector to get MySQL database and display results in Postman.


Similar Articles