8
n8n 中文网amn8n.com

在短短10秒内自动化PostgreSQL和MySQL设置、创建和删除

中级

这是一个DevOps领域的自动化工作流,包含 15 个节点。主要使用 If, Set, Ssh, ManualTrigger 等节点。 在Linux服务器上自动化PostgreSQL和MySQL数据库管理

前置要求
  • 无特殊前置要求,导入即可使用
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "OnGUB1cHxzP09UTz",
  "meta": {
    "instanceId": "dd69efaf8212c74ad206700d104739d3329588a6f3f8381a46a481f34c9cc281",
    "templateCredsSetupCompleted": true
  },
  "name": "在短短 10 秒内自动化 PostgreSQL 和 MySQL 设置、创建和删除",
  "tags": [],
  "nodes": [
    {
      "id": "f024ece1-0378-4dbb-a1b7-06bf598a04de",
      "name": "开始",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -760,
        480
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "4bd36d6f-a312-4ff2-8b6f-86a6769f2588",
      "name": "设置参数",
      "type": "n8n-nodes-base.set",
      "position": [
        -540,
        480
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "server_host",
              "value": "={{ $json.server_host || '192.168.1.100' }}"
            },
            {
              "name": "server_user",
              "value": "{{ $json.server_user || 'root' }}"
            },
            {
              "name": "server_password",
              "value": "{{ $json.server_password || 'your_password' }}"
            },
            {
              "name": "db_type",
              "value": "={{ $json.db_type || 'postgresql' }}"
            },
            {
              "name": "action",
              "value": "={{ $json.action || 'install' }}"
            },
            {
              "name": "database_name",
              "value": "={{ $json.database_name || 'mydb' }}"
            },
            {
              "name": "db_user",
              "value": "{{ $json.db_user || 'dbuser' }}"
            },
            {
              "name": "db_password",
              "value": "{{ $json.db_password || 'dbpass123' }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "8d23250e-4538-4b64-8fa4-a5cdabadc6ac",
      "name": "数据库类型检查",
      "type": "n8n-nodes-base.if",
      "position": [
        -320,
        480
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.db_type }}",
              "value2": "postgresql"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f49751e8-cc9c-45bc-b677-c11919054be2",
      "name": "PostgreSQL 操作检查",
      "type": "n8n-nodes-base.if",
      "position": [
        -100,
        180
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.action }}",
              "value2": "install"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "bad68834-e9b1-406e-8a25-4a82d845b9eb",
      "name": "MySQL 操作检查",
      "type": "n8n-nodes-base.if",
      "position": [
        -100,
        780
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.action }}",
              "value2": "install"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b4d41f1b-8d44-4725-87d6-72dd5d48d81a",
      "name": "安装 PostgreSQL",
      "type": "n8n-nodes-base.ssh",
      "position": [
        340,
        -20
      ],
      "parameters": {
        "command": "#!/bin/bash\n\n# Install PostgreSQL\necho \"Installing PostgreSQL...\"\napt update -y\napt install -y postgresql postgresql-contrib\n\n# Start and enable PostgreSQL\nsystemctl start postgresql\nsystemctl enable postgresql\n\n# Configure PostgreSQL\nsudo -u postgres psql -c \"ALTER USER postgres PASSWORD '{{ $json.db_password }}';\"\n\n# Configure pg_hba.conf for password authentication\nsed -i \"s/#listen_addresses = 'localhost'/listen_addresses = '*'/g\" /etc/postgresql/*/main/postgresql.conf\necho \"host all all 0.0.0.0/0 md5\" >> /etc/postgresql/*/main/pg_hba.conf\n\n# Restart PostgreSQL\nsystemctl restart postgresql\n\n# Create database and user\nsudo -u postgres createdb {{ $json.database_name }}\nsudo -u postgres psql -c \"CREATE USER {{ $json.db_user }} WITH PASSWORD '{{ $json.db_password }}';\"\nsudo -u postgres psql -c \"GRANT ALL PRIVILEGES ON DATABASE {{ $json.database_name }} TO {{ $json.db_user }};\"\n\necho \"PostgreSQL installation and setup completed!\"\necho \"Database: {{ $json.database_name }}\"\necho \"User: {{ $json.db_user }}\"\necho \"Connection: psql -h {{ $json.server_host }} -U {{ $json.db_user }} -d {{ $json.database_name }}\"",
        "authentication": "privateKey"
      },
      "credentials": {
        "sshPrivateKey": {
          "id": "ilPh8oO4GfSlc0Qy",
          "name": "SSH Password account - test "
        }
      },
      "typeVersion": 1
    },
    {
      "id": "43acaf1f-beec-4d5d-9292-139626d04b05",
      "name": "安装 MySQL",
      "type": "n8n-nodes-base.ssh",
      "position": [
        340,
        580
      ],
      "parameters": {
        "command": "#!/bin/bash\n\n# Install MySQL\necho \"Installing MySQL...\"\napt update -y\n\n# Set MySQL root password non-interactively\ndebconf-set-selections <<< \"mysql-server mysql-server/root_password password {{ $json.db_password }}\"\ndebconf-set-selections <<< \"mysql-server mysql-server/root_password_again password {{ $json.db_password }}\"\n\napt install -y mysql-server\n\n# Start and enable MySQL\nsystemctl start mysql\nsystemctl enable mysql\n\n# Configure MySQL for remote connections\nsed -i \"s/bind-address.*/bind-address = 0.0.0.0/g\" /etc/mysql/mysql.conf.d/mysqld.cnf\n\n# Restart MySQL\nsystemctl restart mysql\n\n# Create database and user\nmysql -u root -p{{ $json.db_password }} -e \"CREATE DATABASE {{ $json.database_name }};\"\nmysql -u root -p{{ $json.db_password }} -e \"CREATE USER '{{ $json.db_user }}'@'%' IDENTIFIED BY '{{ $json.db_password }}';\"\nmysql -u root -p{{ $json.db_password }} -e \"GRANT ALL PRIVILEGES ON {{ $json.database_name }}.* TO '{{ $json.db_user }}'@'%';\"\nmysql -u root -p{{ $json.db_password }} -e \"FLUSH PRIVILEGES;\"\n\necho \"MySQL installation and setup completed!\"\necho \"Database: {{ $json.database_name }}\"\necho \"User: {{ $json.db_user }}\"\necho \"Connection: mysql -h {{ $json.server_host }} -u {{ $json.db_user }} -p{{ $json.db_password }} {{ $json.database_name }}\"",
        "authentication": "privateKey"
      },
      "credentials": {
        "sshPrivateKey": {
          "id": "ilPh8oO4GfSlc0Qy",
          "name": "SSH Password account - test "
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5315c681-2677-441b-8010-f7a742849b83",
      "name": "PostgreSQL 创建检查",
      "type": "n8n-nodes-base.if",
      "position": [
        120,
        280
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.action }}",
              "value2": "create"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "1fe28fb8-1dd9-488d-a7cf-bc47658d745e",
      "name": "MySQL 创建检查",
      "type": "n8n-nodes-base.if",
      "position": [
        120,
        880
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.action }}",
              "value2": "create"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b65b1c99-ec5b-486c-8513-0b20ad54186e",
      "name": "创建 PostgreSQL 数据库",
      "type": "n8n-nodes-base.ssh",
      "position": [
        340,
        180
      ],
      "parameters": {
        "command": "#!/bin/bash\n\n# Create PostgreSQL database\necho \"Creating PostgreSQL database: {{ $json.database_name }}\"\n\nsudo -u postgres createdb {{ $json.database_name }}\nsudo -u postgres psql -c \"CREATE USER {{ $json.db_user }} WITH PASSWORD '{{ $json.db_password }}';\"\nsudo -u postgres psql -c \"GRANT ALL PRIVILEGES ON DATABASE {{ $json.database_name }} TO {{ $json.db_user }};\"\n\necho \"PostgreSQL database created successfully!\"\necho \"Database: {{ $json.database_name }}\"\necho \"User: {{ $json.db_user }}\"",
        "authentication": "privateKey"
      },
      "credentials": {
        "sshPrivateKey": {
          "id": "ilPh8oO4GfSlc0Qy",
          "name": "SSH Password account - test "
        }
      },
      "typeVersion": 1
    },
    {
      "id": "aada1dd3-d236-4fe9-8ced-02d1ea20ef46",
      "name": "创建 MySQL 数据库",
      "type": "n8n-nodes-base.ssh",
      "position": [
        340,
        780
      ],
      "parameters": {
        "command": "#!/bin/bash\n\n# Create MySQL database\necho \"Creating MySQL database: {{ $json.database_name }}\"\n\nmysql -u root -p{{ $json.db_password }} -e \"CREATE DATABASE {{ $json.database_name }};\"\nmysql -u root -p{{ $json.db_password }} -e \"CREATE USER '{{ $json.db_user }}'@'%' IDENTIFIED BY '{{ $json.db_password }}';\"\nmysql -u root -p{{ $json.db_password }} -e \"GRANT ALL PRIVILEGES ON {{ $json.database_name }}.* TO '{{ $json.db_user }}'@'%';\"\nmysql -u root -p{{ $json.db_password }} -e \"FLUSH PRIVILEGES;\"\n\necho \"MySQL database created successfully!\"\necho \"Database: {{ $json.database_name }}\"\necho \"User: {{ $json.db_user }}\"",
        "authentication": "privateKey"
      },
      "credentials": {
        "sshPrivateKey": {
          "id": "ilPh8oO4GfSlc0Qy",
          "name": "SSH Password account - test "
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a77b9367-1eec-40c9-b636-9bd7535ecf7a",
      "name": "删除 PostgreSQL 数据库",
      "type": "n8n-nodes-base.ssh",
      "position": [
        340,
        380
      ],
      "parameters": {
        "command": "#!/bin/bash\n\n# Delete PostgreSQL database\necho \"Deleting PostgreSQL database: {{ $json.database_name }}\"\n\nsudo -u postgres dropdb {{ $json.database_name }}\nsudo -u postgres psql -c \"DROP USER IF EXISTS {{ $json.db_user }};\"\n\necho \"PostgreSQL database deleted successfully!\"\necho \"Database: {{ $json.database_name }} (deleted)\"\necho \"User: {{ $json.db_user }} (deleted)\"",
        "authentication": "privateKey"
      },
      "credentials": {
        "sshPrivateKey": {
          "id": "ilPh8oO4GfSlc0Qy",
          "name": "SSH Password account - test "
        }
      },
      "typeVersion": 1
    },
    {
      "id": "37b5c6e2-94f6-440f-b32d-6454e5178bfc",
      "name": "删除 MySQL 数据库",
      "type": "n8n-nodes-base.ssh",
      "position": [
        340,
        980
      ],
      "parameters": {
        "command": "#!/bin/bash\n\n# Delete MySQL database\necho \"Deleting MySQL database: {{ $json.database_name }}\"\n\nmysql -u root -p{{ $json.db_password }} -e \"DROP DATABASE IF EXISTS {{ $json.database_name }};\"\nmysql -u root -p{{ $json.db_password }} -e \"DROP USER IF EXISTS '{{ $json.db_user }}'@'%';\"\nmysql -u root -p{{ $json.db_password }} -e \"FLUSH PRIVILEGES;\"\n\necho \"MySQL database deleted successfully!\"\necho \"Database: {{ $json.database_name }} (deleted)\"\necho \"User: {{ $json.db_user }} (deleted)\"",
        "authentication": "privateKey"
      },
      "credentials": {
        "sshPrivateKey": {
          "id": "ilPh8oO4GfSlc0Qy",
          "name": "SSH Password account - test "
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e23dbe7f-2bb3-4ba2-848a-e85688a05124",
      "name": "格式化输出",
      "type": "n8n-nodes-base.set",
      "position": [
        560,
        480
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "result",
              "value": "={{ $json.stdout }}"
            },
            {
              "name": "status",
              "value": "success"
            },
            {
              "name": "action_performed",
              "value": "={{ $('Set Parameters').item.json.action }}"
            },
            {
              "name": "database_type",
              "value": "={{ $('Set Parameters').item.json.db_type }}"
            },
            {
              "name": "database_name",
              "value": "={{ $('Set Parameters').item.json.database_name }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "30cb203f-a298-41c9-b897-fc5c33178aa4",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1120,
        -20
      ],
      "parameters": {
        "width": 620,
        "height": 360,
        "content": "## 核心元素"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "a13a1931-c209-4f37-ab8c-bdc3c35b8fd4",
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Install MySQL": {
      "main": [
        [
          {
            "node": "Format Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Parameters": {
      "main": [
        [
          {
            "node": "Database Type Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create MySQL DB": {
      "main": [
        [
          {
            "node": "Format Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete MySQL DB": {
      "main": [
        [
          {
            "node": "Format Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Install PostgreSQL": {
      "main": [
        [
          {
            "node": "Format Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MySQL Action Check": {
      "main": [
        [
          {
            "node": "Install MySQL",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "MySQL Create Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MySQL Create Check": {
      "main": [
        [
          {
            "node": "Create MySQL DB",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Delete MySQL DB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Database Type Check": {
      "main": [
        [
          {
            "node": "PostgreSQL Action Check",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "MySQL Action Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create PostgreSQL DB": {
      "main": [
        [
          {
            "node": "Format Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete PostgreSQL DB": {
      "main": [
        [
          {
            "node": "Format Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PostgreSQL Action Check": {
      "main": [
        [
          {
            "node": "Install PostgreSQL",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "PostgreSQL Create Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PostgreSQL Create Check": {
      "main": [
        [
          {
            "node": "Create PostgreSQL DB",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Delete PostgreSQL DB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 开发运维

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量15
分类1
节点类型5
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Oneclick AI Squad

Oneclick AI Squad

@oneclick-ai

The AI Squad Initiative is a pioneering effort to build, automate and scale AI-powered workflows using n8n.io. Our mission is to help individuals and businesses integrate AI agents seamlessly into their daily operations from automating tasks and enhancing productivity to creating innovative, intelligent solutions. We design modular, reusable AI workflow templates that empower creators, developers and teams to supercharge their automation with minimal effort and maximum impact.

外部链接
在 n8n.io 查看

分享此工作流