> Every page of the ganymede website is listed in https://eugo.io/docs/ganymede/llms.txt.

# MySQL via asyncmy

> MySQL via asyncmy: Work with data in a MySQL database. A Python demo notebook from ganymede.

Web page: https://eugo.io/docs/ganymede/demo_notebooks/data_storage/databases/mysql/asyncmy/
Notebook: https://eugo.io/docs/ganymede/demo_notebooks/data_storage/databases/mysql/asyncmy/asyncmy.ipynb (source: https://github.com/eugo-inc/ganymede/blob/main/demo_notebooks/data_storage/databases/mysql/asyncmy.ipynb)

Description:
- Work with data in a MySQL database

Resources:
- [GitHub] https://github.com/long2ice/asyncmy
- [Examples] https://github.com/long2ice/asyncmy/tree/dev/examples

## Environment variables

This notebook reads its settings from environment variables. Set them before starting Jupyter:

- `MYSQL_HOST`: hostname of your MySQL server
- `MYSQL_PORT`: optional, defaults to `3306`
- `MYSQL_USER`: MySQL user name
- `MYSQL_PASSWORD`: password for that user

## Example 1

Read data asynchronously from a mysql database

```python
from asyncmy import connect
from asyncmy.cursors import DictCursor
import asyncio
import os

userpwd = os.environ["MYSQL_PASSWORD"]


async def run():
    conn = await connect(
        host=os.environ["MYSQL_HOST"],
        user=os.environ["MYSQL_USER"],
        password=userpwd,
        port=int(os.environ.get("MYSQL_PORT", "3306")),
    )

    async with conn.cursor(DictCursor) as cur:
        print("Connected")
        await cur.execute("create database if not exists test_db")
        await cur.execute("""use test_db""")
        await cur.execute(
            """CREATE TABLE if not exists another_test_table (
                `id`       int primary key auto_increment,
                `first`    varchar(100),
                `last`     varchar(200)
            )
            """
        )

        await cur.execute("INSERT INTO another_test_table (first, last) VALUES (%s, %s)", ("test_first", "test_lat"))
        res = await cur.execute("SELECT * FROM another_test_table;")
        print(cur.fetchone())


if __name__ == '__main__':
    asyncio.run(run())
```
