> ## Documentation Index
> Fetch the complete documentation index at: https://docs.langchain.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Mariadb integration

> Integrate with the Mariadb vector store using LangChain Python.

LangChain's MariaDB integration (langchain-mariadb) provides vector capabilities for working with MariaDB version 11.7.1 and above, distributed under the MIT license. Users can use the provided implementations as-is or customize them for specific needs.
Key features include:

* Built-in vector similarity search
* Support for cosine and euclidean distance metrics
* Robust metadata filtering options
* Performance optimization through connection pooling
* Configurable table and column settings

## Setup

Launch a MariaDB Docker container with:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
!docker run --name mariadb-container -e MARIADB_ROOT_PASSWORD=langchain -e MARIADB_DATABASE=langchain -p 3306:3306 -d mariadb:11.7
```

### Installing the package

The package uses SQLAlchemy but works best with the MariaDB connector, which requires C/C++ components:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# Debian, Ubuntu
!sudo apt install libmariadb3 libmariadb-dev

# CentOS, RHEL, Rocky Linux
!sudo yum install MariaDB-shared MariaDB-devel

# Install Python connector
!pip install -U mariadb
```

Then install `langchain-mariadb` package

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -U langchain-mariadb

```

VectorStore works along with an LLM model, here using `langchain-openai` as example.

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install langchain-openai
export OPENAI_API_KEY=...

```

## Initialization

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_core.documents import Document
from langchain_mariadb import MariaDBStore
from langchain_openai import OpenAIEmbeddings

# connection string
url = f"mariadb+mariadbconnector://myuser:mypassword@localhost/langchain"

# Initialize vector store
vectorstore = MariaDBStore(
    embeddings=OpenAIEmbeddings(),
    embedding_length=1536,
    datasource=url,
    collection_name="my_docs",
)
```

## Manage vector store

### Adding data

You can add data as documents with metadata:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docs = [
    Document(
        page_content="there are cats in the pond",
        metadata={"id": 1, "location": "pond", "topic": "animals"},
    ),
    Document(
        page_content="ducks are also found in the pond",
        metadata={"id": 2, "location": "pond", "topic": "animals"},
    ),
    # More documents...
]
vectorstore.add_documents(docs)
```

Or as plain text with optional metadata:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
texts = [
    "a sculpture exhibit is also at the museum",
    "a new coffee shop opened on Main Street",
]
metadatas = [
    {"id": 6, "location": "museum", "topic": "art"},
    {"id": 7, "location": "Main Street", "topic": "food"},
]

vectorstore.add_texts(texts=texts, metadatas=metadatas)
```

## Query vector store

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# Basic similarity search
results = vectorstore.similarity_search("Hello", k=2)

# Search with metadata filtering
results = vectorstore.similarity_search("Hello", filter={"category": "greeting"})
```

### Filter options

The system supports various filtering operations on metadata:

* Equality: \$eq
* Inequality: \$ne
* Comparisons: $lt, $lte, $gt, $gte
* List operations: $in, $nin
* Text matching: $like, $nlike
* Logical operations: $and, $or, \$not

Example:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# Search with simple filter
results = vectorstore.similarity_search(
    "kitty", k=10, filter={"id": {"$in": [1, 5, 2, 9]}}
)

# Search with multiple conditions (AND)
results = vectorstore.similarity_search(
    "ducks",
    k=10,
    filter={"id": {"$in": [1, 5, 2, 9]}, "location": {"$in": ["pond", "market"]}},
)
```

***

## API reference

See the [langchain-mariadb API documentation](https://mariadb.com/docs/connectors/other/langchain-mariadb/api-reference) for more detail.

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/vectorstores/mariadb.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
