# Cannot execute SQL from mysql.mysql\_query

**URL:** <https://forum.ansible.com/t/cannot-execute-sql-from-mysql-mysql-query/44595>\
**Category:** Get Help\
**Tags:** mysql, redhat, aap, mariadb, linux\
**Created:** [October 1, 2025, 1:10pm UTC](https://forum.ansible.com/t/cannot-execute-sql-from-mysql-mysql-query/44595 "2025-10-01T13:10:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![engstroem](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@engstroem](https://forum.ansible.com/u/engstroem)\
**Post date:** [October 1, 2025, 1:10pm UTC](https://forum.ansible.com/t/cannot-execute-sql-from-mysql-mysql-query/44595/1 "2025-10-01T13:10:20Z")

</div>

**Issue with executing query-file through mysql.mysql\_query**  
I am new within ansible and it modules, so sorry if I’m just using the wrong module.

I need to run query (~7000 lines) to set up a database on my database server. I am using the `mysql.mysql_query` module because it seems to be the correct module for the task (but might me wrong and getting `fatal: [db-server]: FAILED! => {"changed": false, "msg": "Cannot execute SQL '(...sql-file...)'.`

The db-server is running MariaDB and running ansible through AAP, and it works manually doing `source /var/tmp/redcap_install.sql`, am I doing it correct or should I use another module?

```auto
- name: Move script to db-server
  copy:
    src: redcap_install.sql
    dest: /var/tmp

- name: Create REDCap database with config
  community.mysql.mysql_query:
    login_db: '{{ db_name }}'
    login_user: '{{ db_username }}'
    login_password: '{{ database_password }}'
    query: "/var/tmp/redcap_install.sql"

```

---

<div class="post-metadata">

**Author:** ![Narizz28](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/narizz28/32/9476_2.png) [@Narizz28](https://forum.ansible.com/u/Narizz28)\
**Post date:** [October 1, 2025, 3:23pm UTC](https://forum.ansible.com/t/cannot-execute-sql-from-mysql-mysql-query/44595/2 "2025-10-01T15:23:46Z")

</div>

You’re using a file name in the query text. In order to accomplish what you are trying to do, try this instead:

```auto
    - name: Execute SQL query from file
      community.mysql.mysql_db:
        login_user: your_db_user
        login_password: your_db_password
        login_host: localhost # Or the database server's IP if different
        name: your_database_name
        state: import
        target: /tmp/query.sql

```

Why? Becasue community.mysql.mysql\_db has the parameter STATE which offers the value “import” value.

Source: [Ansible Galaxy - community.mysql](https://galaxy-dev.ansible.com/ui/repo/published/community/mysql/content/module/mysql_query/)

---

<div class="post-metadata">

**Author:** ![engstroem](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@engstroem](https://forum.ansible.com/u/engstroem)\
**Post date:** [October 2, 2025, 6:39am UTC](https://forum.ansible.com/t/cannot-execute-sql-from-mysql-mysql-query/44595/3 "2025-10-02T06:39:59Z")

</div>

Thanks @Narizz28 it works, I though mysql\_query also could take a file 😅

---

<div class="post-metadata">

**Author:** ![Narizz28](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/narizz28/32/9476_2.png) [@Narizz28](https://forum.ansible.com/u/Narizz28)\
**Post date:** [October 2, 2025, 5:08pm UTC](https://forum.ansible.com/t/cannot-execute-sql-from-mysql-mysql-query/44595/4 "2025-10-02T17:08:45Z")

</div>

Glad I could help @engstroem

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 1, 2025, 5:09pm UTC](https://forum.ansible.com/t/cannot-execute-sql-from-mysql-mysql-query/44595/5 "2025-11-01T17:09:43Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
