Using MySQL with MCP in Cursor AI
Photo by Aerps.com on Unsplash
The Model Context Protocol (MCP) is a powerful way to connect LLM services with external systems like databases, APIs, and filesystems. In this guide, I’ll show you how to set up MySQL with Cursor AI using MCP, allowing you to create, modify tables, and insert data directly through natural language prompts.
What is MCP?
Model Context Protocol (MCP) is a standardized way for AI assistants to interact with external tools and data sources. It enables LLMs to:
- Connect to databases
- Access APIs
- Read file systems
- Execute commands
- And much more
Prerequisites
Before we begin, make sure you have:
- Docker and Docker Compose installed
- Python 3.x installed
- Cursor AI installed
- Basic knowledge of MySQL
Step 1: Setting Up MySQL with Docker
First, let’s create a MySQL container using Docker Compose. This provides a clean, isolated environment for development.
Create docker-compose.yml
Create a docker-compose.yml
file in your project root:
version: "3.8"
services:
# MySQL Database
mysql:
image: mysql:8.0
container_name: cursor_mysql_db
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_secure_password
MYSQL_DATABASE: your_database_name
MYSQL_USER: your_username
MYSQL_PASSWORD: your_user_password
volumes:
- mysql_data:/var/lib/mysql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "3306:3306"
networks:
- cursor_network
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
timeout: 20s
retries: 10
volumes:
mysql_data:
networks:
cursor_network:
driver: bridge
Start the MySQL Container
Run the following command to start the MySQL container:
docker-compose up -d
This will:
- Pull the MySQL 8.0 image
- Create a container with your specified configuration
- Start the database service in the background
Verify MySQL is Running
Check that the container is running properly:
docker-compose ps
You should see the MySQL container in the “Up” state.
Step 2: Installing the MySQL MCP Server
The MySQL MCP server allows Cursor to communicate with your MySQL database. Install it using pip:
pip install mysql-mcp-server
Step 3: Configuring MCP in Cursor
Create the .cursor Directory
In your project root, create a .cursor
directory:
mkdir .cursor
Create mcp.json Configuration
Inside the .cursor
directory, create an mcp.json
file:
{
"mcpServers": {
"mysql": {
"command": "python3",
"args": ["-m", "mysql_mcp_server.server"],
"env": {
"MYSQL_HOST": "localhost",
"MYSQL_PORT": "3306",
"MYSQL_USER": "your_username",
"MYSQL_PASSWORD": "your_user_password",
"MYSQL_DATABASE": "your_database_name"
}
}
}
}
Important: Replace the placeholder values with your actual MySQL credentials:
your_username
: The MySQL user you createdyour_user_password
: The password for that useryour_database_name
: The database name you specified
Step 4: Starting the MCP Server
Start the MySQL MCP Server
Run the MCP server to establish the connection:
python3 -m mysql_mcp_server.server
Connect to Cursor
When you run the MCP server, Cursor should detect it and prompt you to connect. Click “Yes” to enable the MySQL integration.
If Cursor doesn’t automatically detect the MCP server, you can manually tell the agent to use MCP by saying: “Please use MCP for database operations.”
Step 5: Using MySQL with Cursor
Now you can interact with your MySQL database using natural language prompts in Cursor. Here are some examples:
Creating Tables
"Create a users table with id, name, email, and created_at fields"
Inserting Data
"Insert a new user with name 'John Doe' and email 'john@example.com'"
Querying Data
"Show me all users in the database"
"Find users created in the last 30 days"
Modifying Tables
"Add a phone_number column to the users table"
"Update the email for user with id 1 to 'newemail@example.com'"
Example Workflow
Here’s a complete example of how to use the setup:
-
Start the MySQL container:
docker-compose up -d
-
Start the MCP server:
python3 -m mysql_mcp_server.server
-
In Cursor, ask to create a table:
"Create a products table with id, name, price, and category fields"
-
Insert some data:
"Insert three products: 'Laptop' for $999, 'Mouse' for $25, and 'Keyboard' for $75"
-
Query the data:
"Show me all products with price greater than $50"
Troubleshooting
Common Issues
-
Connection Refused:
- Ensure MySQL container is running:
docker-compose ps
- Check if port 3306 is available
- Verify credentials in
mcp.json
- Ensure MySQL container is running:
-
MCP Server Not Detected:
- Make sure the server is running:
python3 -m mysql_mcp_server.server
- Check that
.cursor/mcp.json
is properly configured - Restart Cursor if needed
- Make sure the server is running:
-
Permission Denied:
- Verify MySQL user has proper permissions
- Check that the database exists
- Ensure correct credentials in configuration
Debugging Commands
# Check if MySQL is accessible
mysql -h localhost -P 3306 -u your_username -p
# Check MCP server logs
python3 -m mysql_mcp_server.server --verbose
# Test database connection
docker exec -it cursor_mysql_db mysql -u your_username -p
Best Practices
Security Considerations
- Use Strong Passwords: Always use secure passwords for MySQL users
- Limit Permissions: Create a dedicated user with only necessary permissions
- Environment Variables: Consider using environment variables for sensitive data
- Network Security: Don’t expose MySQL port to the internet in production
Performance Tips
- Connection Pooling: The MCP server handles connections efficiently
- Query Optimization: Be specific in your prompts for better performance
- Indexing: Ensure proper indexes for frequently queried columns
Alternative MCP Servers
Cursor supports various MCP servers for different use cases:
- PostgreSQL:
postgres-mcp-server
- SQLite:
sqlite-mcp-server
- File System:
filesystem-mcp-server
- Git:
git-mcp-server
Conclusion
Using MCP with MySQL in Cursor opens up powerful possibilities for database management through natural language. This setup allows you to:
- Create and modify database schemas using plain English
- Insert and update data without writing SQL manually
- Query databases using natural language
- Automate database operations as part of your development workflow
The combination of MCP, MySQL, and Cursor creates a seamless development experience where you can focus on your application logic while the AI handles the database interactions.
Remember to always review the generated SQL before executing it in production environments, and ensure your database credentials are properly secured.
Are you using MCP with other databases or tools? Share your experiences in the comments below!
Comments