Deno and MySQL Connection Tutorial

To Connect Deno with MySQL Server, follow these steps:

Step 1: Install and Configure MySQL

You need to have MySQL installed in your system. If you are using Mac, you can follow this article to install MySQL.

Once MySQL is installed, login to MySQL shell using the following command.

mysql -u root -p

At the time of writing this tutorial, Deno drive for MySQL does not support password-based authentication, so to test it out, leave the password blank

Create a new database.

create database codeforgeek_test;

Select the database.

use codeforgeek;

Create a new table.

    CREATE TABLE users (
        id int(11) NOT NULL AUTO_INCREMENT,
        name varchar(100) NOT NULL,
        created_at timestamp not null default current_timestamp,
        PRIMARY KEY (id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Add data in the table.

insert into users(name) VALUES ('Shahid')

Step 2: Connect to MySQL using Deno

Assuming you have deno installed in your machine. if not, check out the official installation guide.

If you are interested to learn about the Deno concepts and how Deno works, check out this tutorial.

Here is our program to connect to MySQL using Deno.

import { Client } from "https://deno.land/x/mysql/mod.ts";

const client = await new Client().connect({
  hostname: "127.0.0.1",
  username: "root",
  db: "codeforgeek_test",
  poolSize: 3, // connection limit
  password: "",
});

const users = await client.query(`select * from users`);
console.log(users);

await client.close();

Run the program using the following command.

deno run --allow-net app.ts

This should print the data from the table.

Deno MySQL Connection

Deno is currently in the development mode and so are third-party deno modules. I do not recommend it for production at all.

Pankaj Kumar
Pankaj Kumar
Articles: 208