Skip to main content
Azure Database for MySQL is a fully managed relational database service that provides enterprise-grade reliability and security. It supports JSON-based vector storage for semantic search capabilities in AI applications.

Usage

import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "sk-xx"

config = {
    "vector_store": {
        "provider": "azure_mysql",
        "config": {
            "host": "your-server.mysql.database.azure.com",
            "port": 3306,
            "user": "your_username",
            "password": "your_password",
            "database": "mem0_db",
            "collection_name": "memories",
        }
    }
}

m = Memory.from_config(config)
messages = [
    {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
    {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
    {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
    {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
m.add(messages, user_id="alice", metadata={"category": "movies"})

Using Azure Managed Identity

For production deployments, use Azure Managed Identity instead of passwords:
config = {
    "vector_store": {
        "provider": "azure_mysql",
        "config": {
            "host": "your-server.mysql.database.azure.com",
            "user": "your_username",
            "database": "mem0_db",
            "collection_name": "memories",
            "use_azure_credential": True,  # Uses DefaultAzureCredential
            "ssl_disabled": False
        }
    }
}
When use_azure_credential is enabled, the password is obtained via Azure DefaultAzureCredential (supports Managed Identity, Azure CLI, etc.)

Config

Here are the parameters available for configuring Azure MySQL:
ParameterDescriptionDefault Value
hostMySQL server hostnameRequired
portMySQL server port3306
userDatabase userRequired
passwordDatabase password (optional with Azure credential)None
databaseDatabase nameRequired
collection_nameTable name for storing vectors"mem0"
embedding_model_dimsDimensions of embedding vectors1536
use_azure_credentialUse Azure DefaultAzureCredentialFalse
ssl_caPath to SSL CA certificateNone
ssl_disabledDisable SSL (not recommended)False
minconnMinimum connections in pool1
maxconnMaximum connections in pool5

Setup

Create MySQL Flexible Server using Azure CLI:

# Create resource group
az group create --name mem0-rg --location eastus

# Create MySQL Flexible Server
az mysql flexible-server create \
    --resource-group mem0-rg \
    --name mem0-mysql-server \
    --location eastus \
    --admin-user myadmin \
    --admin-password <YourPassword> \
    --version 8.0.21

# Create database
az mysql flexible-server db create \
    --resource-group mem0-rg \
    --server-name mem0-mysql-server \
    --database-name mem0_db

# Configure firewall
az mysql flexible-server firewall-rule create \
    --resource-group mem0-rg \
    --name mem0-mysql-server \
    --rule-name AllowMyIP \
    --start-ip-address <YourIP> \
    --end-ip-address <YourIP>

Enable Azure AD Authentication:

  1. In Azure Portal, navigate to your MySQL Flexible Server
  2. Go to Security > Authentication and enable Azure AD
  3. Add your application’s managed identity as a MySQL user:
CREATE AADUSER 'your-app-identity' IDENTIFIED BY 'your-client-id';
GRANT ALL PRIVILEGES ON mem0_db.* TO 'your-app-identity'@'%';
FLUSH PRIVILEGES;
For production, use Managed Identity to eliminate password management.