How to Set Up a MySQL MCP Server With Gemini CLI

It was a Tuesday and someone on the ops team needed a quick number: how many distinct products did we actually sell in March, not counting refunds or test orders? A fifteen-minute Slack thread ensued because the engineer who knew the schema was in a different timezone and the one who was available wasn't sure which join to use. I pulled up Gemini CLI, pointed it at the database through QueryBear, and had the answer in ninety seconds. That Slack thread is why I set this up.
Part of the MySQL MCP Server guide.
What you'll need
- A MySQL database (read replica recommended)
- A QueryBear account
- Gemini CLI installed (
npm i -g @google/gemini-clior via Homebrew)
Why route through a gateway
Passing a raw mysql:// URL to an agent means the agent is working with whatever privileges that user has. A read-only MySQL user helps, but a gateway gives you more: parser-level DML rejection, a table allowlist, column masking, per-query timeouts, and an audit log. The detailed case is in the MySQL MCP Server guide.
Connect QueryBear to your MySQL
- Open the QueryBear dashboard and click "New connection."
- Choose "MySQL" and paste your connection URL. Use a replica and a user with
SELECTprivilege only. - Under "Allowed tables," add the tables that are relevant. Skip anything you wouldn't want quoted in a Slack message.
- Under "Blocked columns," add things like
password_hash,api_key,credit_card_last4. Save.
QueryBear will sit between Gemini and your database from here on.
Install the QueryBear MCP server in Gemini CLI
Edit ~/.gemini/settings.json (create it if it doesn't exist):
{
"mcpServers": {
"querybear": {
"httpUrl": "https://mcp.querybear.com/mcp"
}
}
}That's the full config. Gemini CLI reads settings.json on each invocation, so there's nothing to restart.
Ask your first question
I asked: "How many distinct products did we sell in March?"
Gemini called get_schema, looked at the tables, then wrote this:
SELECT COUNT(DISTINCT oi.product_id) AS distinct_products
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
WHERE o.created_at BETWEEN '2026-03-01' AND '2026-03-31';QueryBear validated the query (allowlisted tables, no DML, columns not blocked), ran it against the replica, and returned a single row. Gemini printed the count inline in the terminal with a short explanation of what the join was doing.
One thing worth knowing about MySQL: the BETWEEN bounds are inclusive on both ends, so '2026-03-31' captures records up to midnight on March 31, not through the end of the day. If your created_at stores time components, use < '2026-04-01' for the upper bound. Gemini didn't hit this edge case in my schema because everything was date-only, but it's worth catching early.
A Gemini-specific tip
Gemini CLI has a large context window, which means you can pipe its output into other tools without worrying about truncation. If you want a CSV instead of a markdown table, try something like:
gemini "Run a QueryBear query: how many distinct products did we sell in March? Output as CSV." | tee march-products.csvThe result goes to the file and to your terminal at the same time. Useful for dropping quick exports into a spreadsheet without opening anything else.