How to Install mysql2 Module in NodeJS Application

Posted on

Introduction

This is also another article for installing a specific module in NodeJS application. This article is useful to show how to install a module with the name of mysql2. It is actually important for developing a NodeJS application. It is obvious since there are lots of modules available to support the development of it. But in this context, the specific module with the name of mysql2 is very important in order for an application has the ability to connect to MySQL Database Server. Moreover, it is a NodeJS application with a specific dialect connection which is the MySQL dialect.

Installing Mysql2 Module in NodeJS Application

The following are the steps for installing Mysql2 Module in a NodeJS application :

1. Generally, as in every step in the installation, just access the server or the machine. Basically, it is where the running NodeJS application exist. Whether accessing it directly or remotely. There are lots of reference to be able to do it. Especially if it is accessing remotely using SSH connection. Such as the article with the title of ‘How to Remote CentOS Virtual Server running in a VirtualBox with a NAT Network using SSH’ in this link. Precisely, the example is showing how to remote a virtual server.

2. Next step, access the command line interface in the server or the machine.

3. The third step, just execute the command for install the module. In this context, it is the ‘mysql2’ module. In order to install mysql2 module, just check the article with the title of ‘How to Install a Package using npm tool’ in this link. Another example, it is available with the title of ‘How to Install a Specific Package using npm via Command Line’ in this link. Move on to the next one, it is an article with the title of ‘How to Install Package using npm via Command Line’ exist in this link. Next one is  in this link with the title of ‘How to Install Package Dependencies in Node.js Application using npm’. Last but not least, it is an article with the title of ‘How to Install a Specific Package using npm via Command Line’ in this link.

In all of those articles, the command for installing ‘mysql2’ module is in the following command :.

npm i mysql2

The execution of the above command exist in the following real situation :

[admin@10 db]$ npm i nodemon

added 50 packages, and audited 51 packages in 5s

found 0 vulnerabilities
[admin@10 db]$

Testing Mysql2 Module in NodeJS Application

This part is the last part. It is a part to confirm the existence of the module. Just execute the following command to check whether the express module is available or not :

[admin@10 nodejs]$ npm list
nodejs@ /home/admin/nodejs
├── express@4.17.1
├── mysql@2.18.1
├── mysql2@2.2.5
├── nodemon@2.0.7
└── sequelize@6.5.0

[admin@10 nodejs]$

As it shows in the output above, there is a module with the name of ‘mysql2’ from the previous installation above. But apparently, it is not a command that is available for single execution in a command line interface. The following is the execution of it :

[admin@10 db]$ mysql2
-bash: mysql2: command not found
[admin@10 db]$

It is only available in the script for further execution. Althought it is not available for the execution in the command line interface, it exist as in the previous output command execution.

Leave a Reply