MySQL

Query and manage MySQL databases

The MySQL tool lets an agent query and manage MySQL databases directly.

Diagram

MySQL

User

Agent

MySQLTool

MySQL

Rows

Overview

MySQL tool allows you to query and manage MySQL databases directly from your AI agents.

Installation

bash
pip install "praisonai[tools]"

Environment Variables

bash
export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_DATABASE=mydb
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password

Quick Start

  1. Simple Usage
    python
    from praisonai_tools import MySQLTool
    
    # Initialize
    mysql = MySQLTool(
        host="localhost",
        database="mydb",
        user="root",
        password="your_password"
    )
    
    # Query
    results = mysql.query("SELECT * FROM users LIMIT 5")
    print(results)
  2. With Configuration

    Use the same tool with an agent — see Usage with Agent below, or pass env vars and options from the sections above.

How It Works

Diagram
MySQLMySQLToolAgentUserMySQLMySQLToolAgentUser“Show top products”query(sql)SELECTrowsresultsfinal answer

Usage with Agent

python
from praisonaiagents import Agent
from praisonai_tools import MySQLTool

mysql = MySQLTool(
    host="localhost",
    database="mydb",
    user="root",
    password="your_password"
)

agent = Agent(
    name="DBAnalyst",
    instructions="You are a database analyst. Use MySQL to query data.",
    tools=[mysql]
)

response = agent.chat("Show me the top 10 products by sales")
print(response)

Available Methods

query(sql)

Execute a SQL query.

python
from praisonai_tools import MySQLTool

mysql = MySQLTool(host="localhost", database="mydb", user="root", password="pass")
results = mysql.query("SELECT * FROM users WHERE active = 1")

execute(sql)

Execute a SQL statement (INSERT, UPDATE, DELETE).

python
mysql.execute("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')")

list_tables()

List all tables in the database.

python
tables = mysql.list_tables()

Docker Setup

bash
docker run -d --name mysql \
    -e MYSQL_ROOT_PASSWORD=praison123 \
    -e MYSQL_DATABASE=praisonai \
    -p 3306:3306 \
    mysql:8

Common Errors

ErrorCauseSolution
mysql-connector not installedMissing dependencyRun pip install mysql-connector-python
Connection refusedDatabase not runningStart MySQL server
Access deniedWrong credentialsCheck username/password

Best Practices

Load credentials from the environment

Read MYSQL_HOST, MYSQL_USER, and MYSQL_PASSWORD from the environment instead of hard-coding them in the tool call.

Use LIMIT on generated queries

Agent-generated SELECTs can return huge result sets. Instruct the agent to add LIMIT so results stay within the context window.

Scope agent access

Give the agent a read-only MySQL user for analytics tasks so generated SQL cannot modify data.

Related Tools