How to use k6 to connect to mysql database

How to use k6 to connect to mysql database and execute sql

When I use k6 to do functional testing, I need to do this, and check the http response with mysql result

Stock k6 can’t connect to MySQL (or any other DB) yet. We plan to add such features in the future, but for now the only way to do something like this is with an xk6 extension (more info). A basic extension with SQL support already exists here: GitHub - grafana/xk6-sql: k6 extension to load test RDBMSs (PostgreSQL, MySQL, MS SQL and SQLite3)

Thanks for your reply, I will try it

Hi, I just test the xk6 with sqlite and worked perfect. My question is how I can call a MySQL database?
The examples said:
const db = sql.open(‘type’, ‘database’);

So I tried:
const db = sql.open(‘mysql’, ‘mysql’);

So the type = mysql and the database also is mysql, but I dont know how to set the username, password, and host. Any help will be apreciated.

From skimming the code, the second argument for MySQL should be something like this (though I can’t test it myself right now, sorry): username:password@tcp(127.0.0.1:3306)/database_name

1 Like

Hey guys, there’s a MySQL example in the tests/ directory, which has an example connection string. But @ned is correct :slight_smile:

2 Likes