/n8n-tutorials

How to connect n8n to MySQL?

Learn how to connect n8n to MySQL with simple steps, best practices, and tips to ensure a smooth, secure workflow integration.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to connect n8n to MySQL?

The direct answer is: you connect n8n to MySQL by creating a MySQL credential inside n8n and then using the built‑in MySQL node in your workflow. You point the credential to your database host, port, username, password, and database name. After that, you can run real SQL queries (SELECT, INSERT, UPDATE, DELETE) directly from n8n nodes.

 

What you actually do in n8n

 

n8n talks to MySQL using its built‑in MySQL node. n8n does not magically “scan your database”; you explicitly run SQL queries. A credential is simply stored connection info (host, username, etc.) so nodes can connect without exposing passwords in your workflow JSON.

  • MySQL node runs SQL queries.
  • Credentials store DB connection information.
  • Expressions let you insert workflow data into your SQL queries.

 

Step‑by‑step: Creating the connection

 

  • Open n8n, go to Credentials.
  • Create a new credential of type MySQL.
  • Fill the fields:
    • Host: IP or domain (for Docker, this might be the container name).
    • Port: usually 3306.
    • User: your MySQL user.
    • Password: your MySQL password.
    • Database: the DB name you want n8n to use.
  • Click Test. If it passes, the connection is valid.

If the test fails, it’s usually networking: Docker networks, firewalls, or MySQL not allowing external connections. n8n needs permission to access the DB — this is not automatic.

 

Using the MySQL node

 

In any workflow:

  • Add a MySQL node.
  • Select the credential you created.
  • Choose Operation → Execute Query.
  • Write your SQL. Example:
SELECT * FROM users WHERE email = {{$json.email}};  // Using incoming workflow data

n8n will return rows as JSON. Each row becomes an item, which you can send to the next node (Slack, HTTP Request, another DB, etc.).

 

A realistic INSERT example

 

INSERT INTO orders (user_id, total_price, created_at)
VALUES ({{$json.userId}}, {{$json.total}}, NOW());

Expressions (like {{$json.userId}}) pull values from previous nodes. This is how you pass API data into SQL safely.

 

If you're running n8n in Docker

 

The most common connection mistake: using localhost. Inside Docker, localhost means "inside the same container", not your machine. If MySQL is another container, use its container name:

  • MySQL container name: mysql
  • Host in n8n credential: mysql, not localhost

If MySQL is running on the host machine, you usually connect to host.docker.internal (Mac/Windows) or the host IP for Linux.

 

Production considerations (important)

 

  • Do not hardcode passwords in nodes — always use credentials.
  • Enable SSL if the database is outside your private network.
  • Long queries can exceed the workflow timeout; keep heavy logic in the DB or external service.
  • Sanitize your inputs. n8n expressions are inserted raw into SQL; always validate or use known-safe values.
  • Error workflows help monitor DB failures (timeouts, deadlocks, bad credentials).

 

The bottom line

 

To connect n8n to MySQL, you create a MySQL credential with host/user/password/database, test it, and then use the MySQL node to run SQL. From there, you can read or write data as part of any workflow, using expressions to feed real-time values into your queries. This setup is stable for production as long as networking, permissions, and timeouts are correctly configured.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022