How to Connect MySQL to VS Code

Galaxy Glossary

How do I connect MySQL to VS Code quickly and securely?

Use the SQLTools extension or MySQL Shell for VS Code to open, query, and manage MySQL databases directly from Visual Studio Code.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

Why connect MySQL to VS Code?

Keeping code and data in one editor speeds up debugging, simplifies sharing, and lets you use VS Code shortcuts, Git integration, and Galaxy’s AI copilot while working on SQL.

Which extension should I install?

Search the VS Code marketplace for SQLTools (most popular) or MySQL Shell for VS Code. Click Install and reload the window.

How do I configure a new MySQL connection?

Open the Command Palette (⇧⌘P / Ctrl+Shift+P) → SQLTools: Add New Connection. Select MySQL, then fill in the form or paste a URL. Save to settings.json to reuse later.

SQLTools settings.json method

Add a block like:
{ "sqltools.connections": [ { "driver": "MySQL", "name": "LocalShop", "server": "127.0.0.1", "port": 3306, "database": "ecommerce", "username": "dev", "password": "secret", "connectionTimeout": 30, "previewLimit": 50 } ]}

Command Palette quick-connect method

Use a one-line URL:
mysql://dev:secret@127.0.0.1:3306/ecommerce?ssl=false. SQLTools parses the string and stores the details automatically.

How do I run queries after connecting?

Press ⌘E/Ctrl+E to open the connection explorer, double-click LocalShop, then write SQL in a .sql file and hit ⌘Enter/Ctrl+Enter. Results appear in a grid below your code.

How can I manage multiple MySQL servers?

Create extra connection blocks, group them in folders, and assign colors. SQLTools lets you switch contexts without editing code, perfect for staging vs. production.

Best practices for secure connections

Store passwords in VS Code’s Secret Storage, enable SSL with ?ssl=true, and grant the DB user read-only rights. Commit settings.json to Git without credentials by using environment variables.

Why How to Connect MySQL to VS Code is important

How to Connect MySQL to VS Code Example Usage


-- Retrieve total spent by each customer last month
SELECT c.id,
       c.name,
       SUM(o.total_amount) AS monthly_spend
FROM Customers AS c
JOIN Orders AS o ON o.customer_id = c.id
WHERE o.order_date >= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '1 month'
  AND o.order_date <  DATE_TRUNC('month', CURRENT_DATE)
GROUP BY c.id, c.name
ORDER BY monthly_spend DESC;

How to Connect MySQL to VS Code Syntax


mysql://<user>:<password>@<host>:<port>/<database>?ssl=<true|false>

SQLTools settings.json options:
{
  "driver": "MySQL",          // Required: DB type
  "name": "<ConnectionName>", // Display name
  "server": "<host>",         // Hostname or IP
  "port": <3306>,              // Default 3306
  "database": "<db>",         // Default schema
  "username": "<user>",       // MySQL user
  "password": "<password>",   // MySQL password
  "connectionTimeout": 30,     // Seconds to wait
  "previewLimit": 50           // Rows shown in results grid
}

Example ecommerce context URL:
mysql://reporter:My$Pwd@db.prod.local:3306/ecommerce?ssl=true

Common Mistakes

Frequently Asked Questions (FAQs)

Can I use SSH tunneling in SQLTools?

Yes. Set sshHost, sshUser, and sshPort fields in your connection block, or append ?ssh=true in the URL to open the database only through the tunnel.

Does Galaxy’s AI copilot work inside VS Code?

Galaxy integrates through the extension marketplace. Once installed, it suggests queries, explains errors, and refactors SQL directly in your VS Code editor.

How do I export query results to CSV?

After running the query, click the download icon in the results pane or execute SQLTools: Save Results as CSV from the Command Palette.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo