Getting Started with MariaDB using Docker and Node.js

Getting Started with MariaDB using Docker and Node.js hackernoon.com3 months ago in #Dev Love57

It’s no secret that MariaDB has become a popular database solution for developers over the past decade. Why? Well, one could argue that it’s largely because it’s open source and relational. So, for developers, that basically means it’s free, and we get the gist of it. But that really only begins to scratch the surface. What you may not know is that there are two groups actively contributing to MariaDB; Foundation and Corporation. MariaDB Foundation is the custodian of the MariaDB community code and guardian of the MariaDB community. MariaDB Corporation contributes to the community codebase, but also provides superior quality, enterprise grade products that thrusts MariaDB into the forefront of database vendors. MariaDB Corporation even offers columnar and HTAP based solutions, but I digress. With that in mind, I’ve written this short walkthrough to provide a launchpad for you to get started using MariaDB with Docker and Node.js, within a matter of minutes, so you can check things out for yourself. Requirements Before jumping into code, you’re going to need to make sure you have a few things on your machine. MariaDB client Node.js (and NPM – Node Package Manager) Docker Using a MariaDB Docker Container To pull the MariaDB Server image and spin up a container simply open a terminal window and run the following. $ docker run -p 3306:3306 -d –name mariadb -eMARIADB_ROOT_PASSWORD=Password123! mariadb/server:10.4 The previous command will spin up a MariaDB Server container that you can connect to and communicate with using the MariaDB client. Note: While you can certainly use a variety of other SQL clients, for the sake of keeping things simple and uniform, I’ve only included samples using the official MariaDB client. Connect to your MariaDB instance by executing the following command in a terminal window. $ mariadb –host -P 3306 –user root -pPassword123! You should see something like the following, which means you’ve successfully connected to the MariaDB instance! Next, create a new database. Then create a new table. CREATE TABLE demo.people (name VARCHAR(50)); Finally, insert a couple records. INSERT INTO demo.people VALUES (‘rob’), (‘tracy’), (‘sam’), (‘duke’); Connecting to MariaDB with Node.js Now that you’ve downloaded, installed, and stood up a MariaDB database, you’re ready to put it to use within a new Node.js app. To start, pick a new directory, and create a new Javascript file to be used as the main entry point for the Node server. For simplicity, I used “server.js“. Then, within a terminal that the directory location, execute the following. Feel free to fill out all of the prompts, or you can just hit the enter key through all of the options. Either way, you’ll end up with a package.json file being generated next to server.js. Note: You now have a runnable Node app, albeit a pretty uninteresting one. So, let’s continue to spice it up! Install the Express package which will be used as a lightweight web framework by the Node app. Install the MariaDB Node.js connector, which will be used to connect to and communicate with your MariaDB instance. Now it’s time to add code to connect to MariaDB.  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply