|
| 1 | +# MySQL |
| 2 | + |
| 3 | +The [`logfire.instrument_mysql()`][logfire.Logfire.instrument_mysql] method can be used to instrument the [MySQL Connector/Python][mysql-connector] database driver with **Logfire**, creating a span for every query. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install `logfire` with the `mysql` extra: |
| 8 | + |
| 9 | +{{ install_logfire(extras=['mysql']) }} |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +Let's setup a MySQL database using Docker and run a Python script that connects to the database using MySQL connector to |
| 14 | +demonstrate how to use **Logfire** with MySQL. |
| 15 | + |
| 16 | +### Setup a MySQL Database Using Docker |
| 17 | + |
| 18 | +First, we need to initialize a MySQL database. This can be easily done using Docker with the following command: |
| 19 | + |
| 20 | +```bash |
| 21 | +docker run --name mysql \ |
| 22 | + -e MYSQL_ROOT_PASSWORD=secret \ |
| 23 | + -e MYSQL_DATABASE=database \ |
| 24 | + -e MYSQL_USER=user \ |
| 25 | + -e MYSQL_PASSWORD=secret \ |
| 26 | + -p 3306:3306 -d mysql |
| 27 | +``` |
| 28 | + |
| 29 | +This command accomplishes the following: |
| 30 | + |
| 31 | +- `--name mysql`: gives the container a name of "mysql". |
| 32 | +- `-e MYSQL_ROOT_PASSWORD=secret` sets the root password to "secret". |
| 33 | +- `-e MYSQL_DATABASE=database` creates a new database named "database". |
| 34 | +- `-e MYSQL_USER=user` creates a new user named "user". |
| 35 | +- `-e MYSQL_PASSWORD=secret` sets the password for the new user to "secret". |
| 36 | +- `-p 3306:3306` maps port 3306 inside Docker as port 3306 on the host machine. |
| 37 | +- `-d mysql` runs the container in the background and prints the container ID. The image is "mysql". |
| 38 | + |
| 39 | +### Run the Python script |
| 40 | + |
| 41 | +The following Python script connects to the MySQL database and executes some SQL queries: |
| 42 | + |
| 43 | +```py |
| 44 | +import logfire |
| 45 | +import mysql.connector |
| 46 | + |
| 47 | +logfire.configure() |
| 48 | + |
| 49 | +# To instrument the whole module: |
| 50 | +logfire.instrument_mysql() |
| 51 | + |
| 52 | +connection = mysql.connector.connect( |
| 53 | + host="localhost", |
| 54 | + user="user", |
| 55 | + password="secret", |
| 56 | + database="database", |
| 57 | + port=3306, |
| 58 | + use_pure=True, |
| 59 | +) |
| 60 | + |
| 61 | +# Or instrument just the connection: |
| 62 | +# connection = logfire.instrument_mysql(connection) |
| 63 | + |
| 64 | +with logfire.span('Create table and insert data'), connection.cursor() as cursor: |
| 65 | + cursor.execute( |
| 66 | + 'CREATE TABLE IF NOT EXISTS test (id INT AUTO_INCREMENT PRIMARY KEY, num integer, data varchar(255));' |
| 67 | + ) |
| 68 | + |
| 69 | + # Insert some data |
| 70 | + cursor.execute('INSERT INTO test (num, data) VALUES (%s, %s)', (100, 'abc')) |
| 71 | + cursor.execute('INSERT INTO test (num, data) VALUES (%s, %s)', (200, 'def')) |
| 72 | + |
| 73 | + # Query the data |
| 74 | + cursor.execute('SELECT * FROM test') |
| 75 | + results = cursor.fetchall() # Fetch all rows |
| 76 | + for row in results: |
| 77 | + print(row) # Print each row |
| 78 | +``` |
| 79 | + |
| 80 | +[`logfire.instrument_mysql()`][logfire.Logfire.instrument_mysql] uses the |
| 81 | +**OpenTelemetry MySQL Instrumentation** package, |
| 82 | +which you can find more information about [here][opentelemetry-mysql]. |
| 83 | + |
| 84 | +[opentelemetry-mysql]: https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/mysql/mysql.html |
| 85 | +[mysql]: https://www.mysql.com/ |
| 86 | +[mysql-connector]: https://dev.mysql.com/doc/connector-python/en/ |
0 commit comments