# Database (MySQL) conncectivity for custom actions

**URL:** <https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592>\
**Category:** Rasa Open Source\
**Created:** [February 28, 2019, 7:42am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592 "2019-02-28T07:42:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [February 28, 2019, 7:42am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/1 "2019-02-28T07:42:22Z")

</div>

Rasa\_NLU version: 0.14.1 Rasa\_Core version: 0.13.0

I am trying to fetch data for user queries using custom actions, but it is not working.

Below is my custom action:

class ActionDB(Action): def name(self): return ‘action\_db’

```
def run(self, dispatcher, tracker, domain):
	import MySQLdb
	db = MySQLdb.connect("localhost","root","Abcd_1234","DBforChatbot")
	cursor = db.cursor()
	PersonID = tracker.get_slot('personid')
	q = "select * from Persons where PersonID='{0}' limit 1".format(PersonID)
	result = db.query(q)
	return [SlotSet("matches", result if result is not None else [])]

```

and I am getting error as below:

Failed to run custom action ‘action\_db’. Action server responded with a non 200 status code of 404. Make sure your action server properly runs actions and returns a 200 once the action is executed. Error: 404 Client Error: NOT FOUND for url: [http://localhost:5055/webhook/](http://localhost:5055/webhook/) 2019-02-28 13:03:08 ERROR rasa\_core.processor - Encountered an exception while running action ‘action\_db’. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code. 2019-02-28 13:03:08 DEBUG rasa\_core.processor - Failed to execute custom action.

---

<div class="post-metadata">

**Author:** ![mohan](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@mohan](https://forum.rasa.com/u/mohan)\
**Post date:** [February 28, 2019, 9:12am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/2 "2019-02-28T09:12:19Z")

</div>

@Ujjwalsas9 run “python -m rasa\_core\_sdk.endpoint --actions actions” directory where actions.py is located

---

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [February 28, 2019, 12:47pm UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/3 "2019-02-28T12:47:54Z")

</div>

Already tried it. It is not working. Thanks

---

<div class="post-metadata">

**Author:** ![mohan](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@mohan](https://forum.rasa.com/u/mohan)\
**Post date:** [February 28, 2019, 3:02pm UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/4 "2019-02-28T15:02:31Z")

</div>

can you share yml and actions.py

---

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [February 28, 2019, 4:13pm UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/5 "2019-02-28T16:13:05Z")

</div>

# action.py

from **future** import absolute\_import from **future** import division from **future** import unicode\_literals

from rasa\_core\_sdk import Action from rasa\_core\_sdk.events import SlotSet

class ActionDB(Action): def name(self): return ‘action\_db’

```
    def run(self, dispatcher, tracker, domain):
	import MySQLdb
	db = MySQLdb.connect("localhost","root","Abcd_1234","DBforChatbot")
	cursor = db.cursor()
	PersonID = tracker.get_slot('personid')
	q = "select * from Persons where PersonID='{0}' limit 1".format(PersonID)
	result = db.query(q)
	return [SlotSet("matches", result if result is not None else [])]

```

# testbot\_domain.yml

slots: personid: type: text stockid: type: text

intents:

- greet
- bye
- affirmative
- negative
- personid
- stockid
- inform

entities:

- personid
- stockid

templates: utter\_greet: - ‘Hello! How can I help?’ utter\_goodbye: - ‘Talk to you later.’ - ‘Bye bye :(’ utter\_ask\_personid: - ‘Tell me your id please’ - ‘May I know your id please?’ utter\_ask\_stock: - ‘Tell me the stock name please’ - ‘Could you please tell me the stock name?’

actions:

- utter\_greet
- utter\_goodbye
- utter\_ask\_stock
- utter\_ask\_personid
- action\_db

---

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [February 28, 2019, 4:15pm UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/6 "2019-02-28T16:15:47Z")

</div>

Sorry, I was using online markup editor but it did not work well. So the format of the code is pretty much messed up.

---

<div class="post-metadata">

**Author:** ![mohan](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@mohan](https://forum.rasa.com/u/mohan)\
**Post date:** [March 1, 2019, 7:38am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/7 "2019-03-01T07:38:24Z")

</div>

@Ujjwalsas9 try replacing “result = db.query(q)” with “result = cursor.execute(q)” if it doesn’t work try to connect to db using python script. As per my knowledge you are having issue with the db. Make sure you have installed MySQL-python and MySQL-python-connector packages

---

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [March 1, 2019, 9:20am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/8 "2019-03-01T09:20:57Z")

</div>

@mohan Thanks, I tried it but it is not working. Below is the code which is working on python. But when I am trying to replicate the same with rasa custom actions, it is not working. I think, I am doing something wrong in actions code. Please let me know if you could find the error in my actions code. Thanks for your help.

 ![code_snippet](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/4/4ebdd6a4739411672791cd8264c71d4621be3a77.png)

---

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [March 1, 2019, 9:46am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/10 "2019-03-01T09:46:16Z")

</div>

@mohan It does not work. Anyways, thanks.

---

<div class="post-metadata">

**Author:** ![mohan](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@mohan](https://forum.rasa.com/u/mohan)\
**Post date:** [March 1, 2019, 10:31am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/11 "2019-03-01T10:31:02Z")

</div>

[Problem loading actions on 0.11.2](http://forum.rasa.com/t/problem-loading-actions-on-0-11-2/906) might help

---

<div class="post-metadata">

**Author:** ![vinhphat89](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/vinhphat89/32/417_2.png) [@vinhphat89](https://forum.rasa.com/u/vinhphat89)\
**Post date:** [March 4, 2019, 3:57am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/12 "2019-03-04T03:57:38Z")

</div>

Surround your code with ‘try catch’ then print the exception message. It will help you find more info.

```
try:
	# your code here
except Exception as exc:
	print(str(exc))
```

---

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [March 7, 2019, 11:56am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/13 "2019-03-07T11:56:59Z")

</div>

Thanks will try it.

---

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [March 7, 2019, 11:59am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/14 "2019-03-07T11:59:41Z")

</div>

I am getting an error as below:

2019-03-07 17:24:16 ERROR rasa\_core.actions.action - Failed to run custom action ‘action\_db’. Action server responded with a non 200 status code of 404. Make sure your action server properly runs actions and returns a 200 once the action is executed. Error: 404 Client Error: NOT FOUND for url: [http://localhost:5055/webhook/](http://localhost:5055/webhook/)

2019-03-07 17:24:16 ERROR rasa\_core.processor - Encountered an exception while running action ‘action\_db’. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code.

2019-03-07 17:24:16 DEBUG rasa\_core.processor - Failed to execute custom action.

Any ideas on how to get rid of it. FYI: my database is running on port 3306, Rasa action server on 5005 and rasa endpoint is showing 5505.

---

<div class="post-metadata">

**Author:** ![vinhphat89](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/vinhphat89/32/417_2.png) [@vinhphat89](https://forum.rasa.com/u/vinhphat89)\
**Post date:** [March 8, 2019, 6:19am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/15 "2019-03-08T06:19:52Z")

</div>

Let’s try changing your class name from “ActionDB” to “ActionDb”.

---

<div class="post-metadata">

**Author:** ![Ujjwalsas9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ujjwalsas9/32/2049_2.png) [@Ujjwalsas9](https://forum.rasa.com/u/Ujjwalsas9)\
**Post date:** [March 11, 2019, 4:45am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/16 "2019-03-11T04:45:53Z")

</div>

Thanks, but it does not help.

---

<div class="post-metadata">

**Author:** ![tomarraj008](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/tomarraj008/32/124_2.png) [@tomarraj008](https://forum.rasa.com/u/tomarraj008)\
**Post date:** [March 13, 2019, 6:41am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/17 "2019-03-13T06:41:05Z")

</div>

i am also facing same issue for custom response

---

<div class="post-metadata">

**Author:** ![varunsapre10](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/varunsapre10/32/6520_2.png) [@varunsapre10](https://forum.rasa.com/u/varunsapre10)\
**Post date:** [January 28, 2020, 11:49am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/19 "2020-01-28T11:49:04Z")

</div>

Hi @nitz, try the following:

1. Check if you have this in your credentials.py file.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/2/222f716f44366796ee4dfb35eb22aa0fb5ee39b1.png)

1. Check if you are running your action server by “rasa run actions”.

2. Check if you have added your custom action name to the domain file under “actions”

![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/a/a7f3339d05dcf57ab7d27f2c4880790ef40b46a0.png)

---

<div class="post-metadata">

**Author:** ![nitz](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nitz](https://forum.rasa.com/u/nitz)\
**Post date:** [January 28, 2020, 3:18pm UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/20 "2020-01-28T15:18:10Z")

</div>

Thx i will try it , but it will be helpful if u share ur ation.py file.

---

<div class="post-metadata">

**Author:** ![varunsapre10](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/varunsapre10/32/6520_2.png) [@varunsapre10](https://forum.rasa.com/u/varunsapre10)\
**Post date:** [January 29, 2020, 4:47am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/21 "2020-01-29T04:47:32Z")

</div>

Sure @nitz Here is the file : [actions.py](https://forum.rasa.com/uploads/short-url/jGzniwVCLp4wzfaVsVRDmO2TKet.py) (9.8 KB)

---

<div class="post-metadata">

**Author:** ![nitz](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nitz](https://forum.rasa.com/u/nitz)\
**Post date:** [February 3, 2020, 10:30am UTC](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592/23 "2020-02-03T10:30:47Z")

</div>

@varunsapre10 can u please help me with this

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/e/eea21d27dfe1c4bda10effdc7fdad5e777a1607c.png)

[Next page](https://forum.rasa.com/t/database-mysql-conncectivity-for-custom-actions/5592.md?page=2)
