The MCP MySQL Server allows AI models to interact with MySQL databases through a standardized interface, enabling you to execute queries, manage database schemas, and perform other database operations directly through your AI assistant.
The easiest way to install the MySQL Database Server is automatically via Smithery:
npx -y @smithery/cli install @enemyrr/mcp-mysql-server --client claude
To install the server manually:
git clone https://github.com/enemyrr/mcp-mysql-server.git
cd mcp-mysql-server
npm install
npm run build
mysql
command
node /absolute/path/to/mcp-mysql-server/build/index.js
Note: Make sure to replace /absolute/path/to/
with the actual path where you cloned the project.
You can configure your database connection using one of these three methods:
DATABASE_URL=mysql://user:password@host:3306/database
DB_HOST=localhost
DB_USER=your_user
DB_PASSWORD=your_password
DB_DATABASE=your_database
use_mcp_tool({
server_name: "mysql",
tool_name: "connect_db",
arguments: {
url: "mysql://user:password@host:3306/database"
// OR
workspace: "/path/to/your/project" // Will use project's .env
// OR
host: "localhost",
user: "your_user",
password: "your_password",
database: "your_database"
}
});
Before using any other tool, you need to establish a connection:
use_mcp_tool({
server_name: "mysql",
tool_name: "connect_db",
arguments: {
url: "mysql://user:password@host:3306/database"
}
});
For retrieving data from the database:
use_mcp_tool({
server_name: "mysql",
tool_name: "query",
arguments: {
sql: "SELECT * FROM users WHERE id = ?",
params: [1]
}
});
For modifying data in the database:
use_mcp_tool({
server_name: "mysql",
tool_name: "execute",
arguments: {
sql: "INSERT INTO users (name, email) VALUES (?, ?)",
params: ["John Doe", "[email protected]"]
}
});
To get a list of all tables in your database:
use_mcp_tool({
server_name: "mysql",
tool_name: "list_tables"
});
To get detailed information about a specific table:
use_mcp_tool({
server_name: "mysql",
tool_name: "describe_table",
arguments: {
table: "users"
}
});
To create a new table with specified fields and indexes:
use_mcp_tool({
server_name: "mysql",
tool_name: "create_table",
arguments: {
table: "users",
fields: [
{
name: "id",
type: "int",
autoIncrement: true,
primary: true
},
{
name: "email",
type: "varchar",
length: 255,
nullable: false
}
],
indexes: [
{
name: "email_idx",
columns: ["email"],
unique: true
}
]
}
});
To add a new column to an existing table:
use_mcp_tool({
server_name: "mysql",
tool_name: "add_column",
arguments: {
table: "users",
field: {
name: "phone",
type: "varchar",
length: 20,
nullable: true
}
}
});
The server includes several built-in security features:
If you encounter errors, the server will return detailed error messages for connection failures, invalid queries or parameters, missing configuration, and more.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"cursor-rules-mcp": {
"command": "npx",
"args": [
"-y",
"cursor-rules-mcp"
]
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.