Home » n8n» How to connect MySQL to n8n — Docker tutorial

How to connect MySQL to n8n?

To connect MySQL to n8n, run a MySQL server in Docker, start n8n in the same Docker network, then create MySQL credentials in the n8n UI and use the MySQL node to run queries. This guide shows the exact Docker Compose, commands, and a sample query for a beginner.

How to connect MySQL to n8n – What You Need

n8n running in Docker. A MySQL server in Docker. Docker and Docker Compose installed. Basic comfort with the terminal. Port 3306 free locally or mapped in Docker.

Step-by-step setup

Step 1: Create a Docker Compose file

Save the following as docker-compose.yml. It starts MySQL and n8n on one network so n8n can reach the database by service name.

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: examplepass
      MYSQL_DATABASE: n8n_demo
    volumes:
      - mysql-data:/var/lib/mysql
    ports:
      - '3306:3306'

  n8n:
    image: n8nio/n8n:latest
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=admin
      - N8N_BASIC_AUTH_PASSWORD=adminpass
    ports:
      - '5678:5678'
    depends_on:
      - mysql
    tty: true

volumes:
  mysql-data:

Step 2: Start the services

Run this command in the folder with docker-compose.yml.

docker compose up -d

Check both containers are healthy.

docker compose ps

Step 3: Initialize the database and add sample data

Run these commands to create a table and insert a row.

docker exec -i $(docker compose ps -q mysql) 
  mysql -u root -pexamplepass -e "CREATE DATABASE IF NOT EXISTS n8n_demo; USE n8n_demo; CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); INSERT INTO users (name) VALUES ('Alice');"

# Or run a simple mysql client inside the container
docker exec -it $(docker compose ps -q mysql) mysql -u root -pexamplepass n8n_demo
# Then inside the mysql shell:
# SELECT * FROM users;

Step 4: Open n8n and create MySQL credentials

Open http://localhost:5678 and log in with the basic auth user and password from the compose file. Create credentials in n8n:

  • Type: MySQL
  • Host: mysql
  • Port: 3306
  • Database: n8n_demo
  • User: root
  • Password: examplepass

Use the service name mysql because both services share the same Docker network.

Step 5: Use the MySQL node in a workflow

Create a new workflow. Add the MySQL node. Select the credentials you created. Use a simple query to read data.

SELECT * FROM users;

Run the node. You should see the row inserted earlier.


Update

To update images, pull and restart. Example:

docker compose pull
docker compose up -d --remove-orphans

Test the workflow after restart to confirm queries still work.


Security

Do not use root or simple passwords in production. Create a dedicated MySQL user with limited privileges for n8n.

docker exec -it $(docker compose ps -q mysql) mysql -u root -pexamplepass -e "CREATE USER 'n8n_user'@'%' IDENTIFIED BY 'strongpass'; GRANT SELECT, INSERT, UPDATE, DELETE ON n8n_demo.* TO 'n8n_user'@'%'; FLUSH PRIVILEGES;"

Restrict network access and avoid exposing MySQL to the public internet. Use TLS and secrets or environment files for passwords.


Done

You now have MySQL connected to n8n for queries and automation. The MySQL node can run selects, inserts, updates, and deletes. Use credentials in n8n to switch environments without changing workflows.

If you want n8n itself to use MySQL for its internal database instead of SQLite, add DB_TYPE and DB_MYSQL_* environment variables to the n8n service and restart.

Neil
Written by Neil

Neil is a true n8n geek who lives and breathes workflow automation. He dives deep into nodes, triggers, webhooks, custom logic, and self-hosting setups, sharing everything he learns about n8n on AutomationCompare.com. As part of a broader team of automation specialists, Neil focuses purely on mastering n8n and helping others unlock its full potential.

Keep Reading

Scroll to Top