Using the MySQL client for Deno

MySQL and MariaDB#

In this lesson, we will take a closer look at MySQL. This database system emerged around the same time as PostgreSQL, and while PostgreSQL is known for its advanced features, MySQL is known for its popularity, and is used as the default by projects like WordPress.

MariaDB is a community fork of MySQL which was created after the Oracle acquisition of MySQL in 2009. It was intended to be a drop in replacement for MySQL, although it diverges in some cases. If you prefer to use MariaDB, you should not face any troubles and can follow the same steps beneath.

We will start this lesson by choosing deno_mysql, originally created bymanyuanrong but transferred to Deno Drivers in the attempt to unify database client interfaces between databases. deno_mysql also supports MariaDB, so if this is your preferred fork, you have the freedom of choice.

You can find the library here.


For this step, we need two things: a MySQL (or MariaDB) database, and a Deno script. This script will create a connection from our code to our database which will allow us to save and fetch data.

I will be using Docker for the database, but you can also install it locally by following the steps forMySQL orMariaDB.

The default username for MySQL is root and the password is root, however, the Deno client does not yet support authentication for version 8 or newer, so we will disable password login for our purposes. You can follow the issue here if you want more information.

Using Docker#

To start the database using Docker, you can run this command. You can leave the terminal session open for the rest of this step, and if you get any permission errors you can prefix the command with sudo.

  • --rm will clean up the container after you kill it. You can read more about it here.

  • -p is short for --port. It links a port on the container with a port on your local system, allowing you to connect to it.

  • -e is short for --env. It is used to pass in an env variable to the container.

If you receive any errors regarding port allocation, it probably means that you are already using that port for something else. Try to change the first port to something else e.g. 3307:3306

Writing the connection#

For MySQL we create a new file called mysql.ts, to host our script in. We start by importing the client.


This page is a preview of Build and deploy a REST API with Deno

Start a new discussion. All notification go to the author.