# MySQL

- URL: https://betteranalyst.com/docs/connectors/mysql
- Updated: 2026-11-12
- Author: Better Analyst

Connect MySQL to Better Analyst with a read-only user. Covers setup steps, firewall configuration, SSL, Data Context, and troubleshooting tips.

Connect your MySQL database to analyze data, run queries, and create visualizations using natural language.

## Prerequisites

- MySQL 5.7 or higher
- Database credentials (username, password)
- Network access from Better Analyst servers
- A user with SELECT permissions

## Connection Details

You'll need the following information:

| Field | Description | Example |
| - | - | - |
| Host | Server hostname or IP | `db.example.com` |
| Port | MySQL port (default 3306) | `3306` |
| Database | Database name | `analytics` |
| Username | Database user | `readonly_user` |
| Password | User password | `********` |

## Setup Steps

### 1. Prepare Your Database

Create a read-only user for Better Analyst (recommended):

```sql
CREATE USER 'formulabot'@'%' IDENTIFIED BY 'your-secure-password';
GRANT SELECT ON your_database.* TO 'formulabot'@'%';
FLUSH PRIVILEGES;
```

### 2. Configure Firewall Access

Allow connections from Better Analyst's IP ranges:

```
74.220.50.0/24
74.220.58.0/24
```

### 3. Connect in Better Analyst

1. Go to **Connectors** in the sidebar
2. Click **Add Connector**
3. Select **MySQL**
4. Enter your connection details
5. Click **Test Connection**
6. If successful, click **Save**

### 4. Configure Optional Settings

- **Data Context** - Add definitions and documents for better query understanding
- **Data Access Controls** - Restrict which tables/columns are accessible

## Security Best Practices

Never use root or admin credentials. Create a dedicated read-only user for Better Analyst.

### Use Read-Only Access

Only grant SELECT permissions to prevent accidental data modification.

### Use SSL/TLS

Enable SSL for encrypted connections:

1. Check "Use SSL" in connection settings
2. Upload your CA certificate if required

### Restrict Table Access

Use Data Access Controls to limit which tables Better Analyst can query.

## Example Questions

Once connected, ask questions like:

- "How many orders were placed last month?"
- "Show the top 10 customers by revenue"
- "What's the average order value by product category?"
- "Create a chart of daily signups for the past 30 days"

Better Analyst will automatically generate the appropriate SQL queries.

## Data Context

Use Data Context for better results:

1. Go to **Connectors** > your MySQL connector
2. Open **Data Context**
3. Use **Definitions** for tables and columns, or **Uploaded Docs** for reference files
4. The AI will use this context when generating queries

Example knowledge base entries:

- `orders` table: "Contains all customer orders with timestamps and totals"
- `customer_id` column: "Foreign key to the customers table"

## Troubleshooting

### Connection Refused

- Verify the host and port are correct
- Check firewall rules allow the connection
- Ensure MySQL is accepting remote connections

### Access Denied

- Verify username and password
- Check user has SELECT permissions
- Confirm user can connect from remote hosts

### SSL/TLS Errors

- Verify SSL is configured on the MySQL server
- Check the CA certificate is correct
- Try disabling SSL verification for testing

### Timeout Errors

- Large queries may time out
- Ask for aggregated data instead of raw rows
- Consider adding indexes to frequently queried columns

## Next Steps

- [Connect PostgreSQL](https://betteranalyst.com/docs/connectors/postgresql)
- [Connect Supabase](https://betteranalyst.com/docs/connectors/supabase)
- [Learn about Data Access Controls](https://betteranalyst.com/docs/connectors/overview)
