n8n

How to Connect MySQL for Self Serve Data Analysis?

Turn natural language questions into SQL and view results in one place. Teams can ask for metrics in plain English, and get a clear answer plus the query used. It is ideal for operations, product, or data leads who need fast insights without waiting on analysts.

The system runs in two parts. First, a one time setup scans your MySQL database, builds a full schema map of all tables and columns, and saves it to a local JSON file. After that, every chat message uses the Chat Trigger. The flow loads the schema from the local file, sends it with the user question into an AI Agent powered by an OpenAI chat model with short memory. The agent only sees structure, not data. If the agent proposes a valid SQL statement, a regex step extracts the query. An If node checks for a query. When a query exists, it runs on MySQL, formats the rows, and merges both the AI explanation and the results for the final reply. If no SQL is needed, it responds right away.

You need a MySQL database and an OpenAI API key. Run the schema step once, then use the chat endpoint for everyday questions. Expect faster answers, fewer ad hoc report requests, and better clarity on how numbers are pulled. Great for internal analytics chat, quick sales ops checks, and safe database exploration without exposing raw data.

What are the key features?

  • Chat Trigger collects user questions and starts the flow
  • One time schema builder lists tables and describes columns to map structure
  • Schema is saved as a local JSON file for fast lookups
  • OpenAI chat model with low temperature generates precise SQL
  • Window Buffer Memory keeps the last 10 messages for context
  • Regex extraction pulls the SQL statement from the agent reply
  • If branch routes either to run the query or answer directly
  • MySQL node executes the query and a formatter prepares readable results

What are the benefits?

  • Reduce ad hoc data requests from hours to minutes
  • Streamline question handling by up to 70% with a chat front end
  • Improve accuracy by removing manual copy and paste of SQL
  • Speed up responses by loading schema from a local cache
  • Let non technical users explore data structure without database access
  • Show both the answer and the SQL for full transparency

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with OpenAI and MySQL. See the Tools Required section above for links to create accounts with these services.
  3. Prepare your database: ensure MySQL is reachable from n8n Cloud and that the user has read access to all tables you want to query.
  4. In the n8n credentials manager, create a new MySQL credential. Enter host, port, database, user, and password. Save and test the connection.
  5. Open the MySQL nodes that load tables and schema. In the credential dropdown, pick your new MySQL credential for each node.
  6. Run the manual trigger once to build and save the schema file. Confirm the schema file is created by checking the success output of the file write node.
  7. Create an OpenAI credential in n8n. Click Create new credential, choose OpenAI, paste your API key from the OpenAI dashboard, and save.
  8. Open the OpenAI Chat Model node and select your OpenAI credential. Keep temperature at 0.2 for consistent SQL.
  9. Publish or activate the Chat Trigger. Copy the webhook URL if you plan to embed it in a custom chat UI.
  10. Send a test message like List all tables. Confirm you get an immediate answer without a query when no SQL is needed.
  11. Send a question that needs data, such as Show top customers by invoices. Check that the Extract SQL Query node finds the SQL and that the MySQL node returns rows.
  12. If SQL is not extracted correctly, adjust the regex in the Extract SQL Query node to match your model output style.
  13. If the query fails, verify database permissions, host and port, and that the schema file path matches the file read node.
  14. Optional: tune memory window length and the AI system prompt to align with your data rules and naming standards.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

MySQL

Sign up

MySQL Community Edition (GPL) – Free ($0)

OpenAI

Sign up

Pay-as-you-go: GPT-5 at $1.25 per 1M input tokens and $10 per 1M output tokens

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.