Our application will require customers to view information from a centralized source, and we will need to capture information that they have entered into our database. Usually, customers can either view your product/services via an application that they install on their machine or they can use a browser to visit our website. Node.js is a good choice for building web applications, which is what we’ll be building throughout this book, due to its central processing unit (CPU)-bound limitations and ease of context switching between frontend development (what is displayed to the end user) and backend development (what the end user does not see but still invokes) owing to Node.js being JavaScript. We will need to install the following applications/programs in order to get started:
- A DBMS (we will be installing MySQL)
- Node.js runtime
- Sequelize and Express
Installing MySQL
This next section will go over the installation process for MySQL on three different operating system distributions: Microsoft Windows, macOS, and Linux. MySQL was chosen due to the ease of installation (no need to mess with configurations or access-control lists (ACLs)). Do not let those points discourage you from using a different database. For the most part, Sequelize will be able to gracefully translate from one DBMS to another, and the majority of this book will use common/standard Structured Query Language (SQL) methods.
Windows
The MySQL installer for Microsoft Windows can be found here:
https://dev.mysql.com/downloads/mysql/5.7.html
Note
The default Uniform Resource Locator (URL) for downloading Windows’ MySQL installer is currently at version 8.0.26
. This book uses version 5.7
, but other versions of MySQL should still work appropriately as long as the Node.js MySQL driver is compatible with that version.
Once we are finished downloading and opening the installer application, you will be greeted with the Choosing a Setup Type screen. We will want to select the Developer Default and Install all products options, as illustrated in the following screenshot:
Figure 1.1 – Windows MySQL Installer: Choosing a Setup Type
If you have Python or Visual Studio installed on your computer, you may be greeted with a Check Requirements step (see Figure 1.2). If you are using Visual Studio as your integrated development environment (IDE), then you may install the necessary products, but it is not a requirement. Throughout your projects, you may come across a utility that is written in Python that interacts with your database (for example, most data science-related libraries/frameworks). By selecting the Connector/Python option shown in the following screenshot, we can avoid potential headaches in the future:
Figure 1.2 – Windows MySQL Installer: Check Requirements
The next section should be the Download step. The main products that we will be required for the contents of this book are listed here:
- MySQL Server
- MySQL Workbench (for a graphical user interface (GUI) to our database)
- MySQL Shell
You can see the aforementioned products in the following screenshot:
Figure 1.3 – Windows MySQL Installer: Download
Note
If you are new to MySQL, it may be a good idea to download the MySQL Documentation and Samples and Examples packages.
After we have finished downloading our packages, we will be entering our configuration details for each applicable selected product (for example, MySQL Server and Samples and Examples). For the majority of the configuration settings, we will be using the default values; however, there will be some steps that will require your intervention. You can see an overview of this in the following screenshot:
Figure 1.4 – Windows MySQL Installer: Type and Networking
From the MySQL Server configuration wizard, we will want the following settings (as shown in Figure 1.4):
- Config Type: Development Computer
- TCP/IP: Checked
- Port:
3306
- Open Windows Firewall port for network access: Optional
The next part of the MySQL Server configuration step is to declare your MySQL root password and user accounts. Make sure to keep this information in a safe place in case you run into administration issues throughout your projects. If you forget the MySQL root password, there are several methods for resetting the password, as explained here: https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/resetting-permissions-windows.html.
For setting up a MySQL user account with a role, you will be greeted with the following Accounts and Roles screen:
Figure 1.5 – Windows MySQL Installer: Accounts and Roles
Within the MySQL User Accounts section, you will need to click on the Add User button (near the right side of the window, as shown in Figure 1.5) and type in a username and password that you will memorize for when we initialize our Node.js application. When you are finished adding the appropriate root password and MySQL user account(s), we can proceed to the next step.
Next, the installation process will offer a Configure MySQL Server as a Windows Service option, as illustrated in the following screenshot. Windows Service is a process control system (PCS) that will also orchestrate background processes (in the Unix/Linux world, these are referred to as daemons):
Figure 1.6 – Windows MySQL Installer: Windows Service
We will want to ensure the following parameters are configured (as shown in Figure 1.6):
- Configure MySQL Server as a Windows Service: Checked
- Start the MySQL Server at System Startup: Checked
- Standard System Account selected under the Run Windows Service as... section
Click on Next > to apply our configurations for the MySQL server. If you selected additional packages to install earlier, you may be prompted with additional screens asking for more configuration settings and parameters.
Note
If you selected the MySQL Router package from the previous section, the installation process will ask you for information on how you would like to set up a cluster environment. It is not recommended to install this package unless you are a database administrator or you are setting up a production environment. Simply uncheck the Bootstrap MySQL Router for use with InnoDB cluster option and click Finish to proceed without installing MySQL under a cluster environment.
If the Samples and Examples package was selected for installation, we will be prompted with a screen that will allow us to enter our MySQL username and password. You may use your root credentials for the username and password input fields and click on the Next > button to continue. An overview of the screen is provided in the following screenshot:
Figure 1.7 – Windows MySQL Installer: Connect To Server
macOS
There are a couple of ways to install MySQL on a macOS machine. The first way is to download and install MySQL from a Disk iMaGe (DMG) file, while another method is by using a package manager such as Homebrew. We will explore both options.
Installing from disk image
You can find the appropriate disk image from the following URL: https://dev.mysql.com/downloads/mysql/ (x86 for Intel CPUs and Advanced RISC Machine (ARM) for M1 CPUs).
Note
If you cannot find version 5.7 for MySQL, you will find the appropriate DMG file from MySQL’s archive link: https://downloads.mysql.com/archives/community.
However, the macOS installation packages may not be available to download for the most recent 5.7 versions. At the time of writing this book, versions 5.7.34, 5.7.33, and 5.7.32 are not available as a DMG package (5.7.31 is available to download). Any applicable 5.7 version should be compatible with this book’s instructions and installation procedures.
If you are asked about installing a preference panel throughout the installation process, we recommend you do so. Otherwise, we will need to consult the Installing a MySQL Launch Daemon page, located at https://dev.mysql.com/doc/refman/5.7/en/macos-installation-launchd.html.
After downloading and opening the DMG file, we will want to open the package (pkg) file, which will start our installation process. Depending on your macOS version, you may be prompted with a “[package name]” can’t be opened because Apple cannot check it for malicious software screen, as shown here:
Figure 1.8 – Apple cannot identify the package for maliciousness
If this is the case for you, go to Apple | Security & Privacy, and the window should have an Open Anyway button next to “mysql….pkg” was blocked from use because it is not from an identified developer., as shown in the following screenshot:
Figure 1.9 – Bypassing unidentified package installations
Once the installation package opens again, you may be prompted with another alert from Apple. Click on Open to continue with the installation process. After continuing and reading the software license agreement (SLA), you may select the default installation location. Clicking on Install may prompt for your administrative password, as illustrated in the following screenshot:
Figure 1.10 – MySQL installation asking for administrative permission
Once the MySQL installer finishes, an alert dialog will appear with a temporary password. An example is shown in the following screenshot. Make sure to take note of the temporary password for when we log in to the MySQL server:
Figure 1.11 – MySQL installation providing a temporary root password
Installing from Homebrew
Using Homebrew over traditional package installers can help keep your packages up to date without manual intervention, along with validating package installations and binaries. To install MySQL through Homebrew, we will need to install Homebrew on our local machine. Within the terminal (located in Applications > Utilities), simply type in the following:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Note
It is always a good idea to double-check an external script’s contents before running commands from it. A web page can redirect to anywhere, including malicious scripts that could lead to data breaches or something more nefarious.
When installing Homebrew, you may come across the following message:
==> Checking for `sudo` access (which may request your password)
You can see an illustration of this in the following screenshot:
Figure 1.12 – Installing Homebrew on macOS
You can either enter in your password here or before installing Homebrew, run sudo <anything>
(for example, sudo ls
), enter in your password, and then run the installation command. The user must have administrator access before continuing.
For this book, we will install MySQL version 5.7. Other versions of MySQL should be compatible with the book’s code base, as previously noted. To install version 5.7 explicitly, run the following command:
brew install mysql@5.7
There may be additional steps and commands to run in order to set up your instance properly, as illustrated in the following screenshot. The book’s contents will not require library/header files for compilation, nor for pkg-config
to be configured. As a general rule, it is recommended to run mysql_secure_installation
and go through the prompts for adding a root password, but it is not a requirement:
Figure 1.13 – Installing MySQL with Homebrew on macOS
Next, we will need a way to manage our MySQL service. There are two options available to us, as outlined here:
In order to start the MySQL service, we need to run the following command:
brew services start mysql@5.7
If you prefer a GUI version of managing your services, there is an application called brew-services-menubar
that can be installed via Homebrew’s Cask extension, as shown in the following code snippet:
brew install --cask brewservicesmenubar
Note
If you prefer to use a GUI when interfacing/querying databases, there is a free application called Sequel Pro that is available for downloading here:
http://www.sequelpro.com/
Linux
There are numerous distributions of Linux; for this book, we will be using Ubuntu (any Debian distribution should be applicable with the same commands). If you are using a different distribution, please refer to this page for instructions on how to install MySQL for your operating system: https://dev.mysql.com/doc/refman/5.7/en/linux-installation.html.
Within the terminal, run the following commands (these are also shown in the screenshot that follows):
sudo apt-get update
sudo apt install mysql-server
Figure 1.14 – Installing MySQL Server on Ubuntu
After MySQL has finished its installation, we will need to initialize a database to store all of our model’s schemas and information. Some ORMs and DBMSs will refer to databases as “schemas” (not to be confused with a model’s schema, which is referred to as “attributes” in Sequelize specifically).
Creating a database
Now that we have finished installing the MySQL DBMS engine on our local machine, we can start creating a database with some tables. Before creating tables, we will need to go over the various types of MySQL engines. Luckily for us, the following is applicable to all operating systems in the same way.
By default, MySQL will create an InnoDB database type (or, in MySQL terms, engine). Database engines are associated with the database’s table on MySQL (and not the entire database itself). This is useful when you know the trade-offs between a read-heavy table with no constraints (for example, news articles) and a write-heavy table (for example, a chatroom). For the sake of brevity, we will go over the main three database engines, as follows:
- InnoDB: A database engine with transactional queries and FK support. Transactional queries are useful for executing a query, or several queries, with atomicity. We will go into further details about transactions and FKs in a later chapter.
- MyISAM: If the majority of your database’s operations are read-related and you do not require any data constraints, this would be a preferred database engine to use.
- HEAP: The data stored within these tables is contained within the machine’s memory. This database engine is useful if you had to query against temporary data quickly. MySQL will not manage memory allocations for you, so it is important to remember to delete tables when they are no longer in use (and that the data fits into the machine’s available memory).
Note
You can always check your local MySQL server’s default engine type by entering the following command within a MySQL client: SELECT @@default_storage_engine;
.
You may skip this section and use the Sequelize command-line interface’s (CLI’s) (installation instructions are given later within this chapter) db:create
command, as long as the applicable MySQL user has the appropriate permissions. For the intent of becoming familiar with the terminal, we will create the database using command lines, as shown in the next screenshot.
Log in to the MySQL server with the following command (you may be prompted to enter in a password, or the additional -p
parameter is required to enter in a password):
mysql --user=root
We can create our database by executing the following SQL command within the MySQL client Command Prompt:
CREATE DATABASE airline;
For Windows users
Most of these commands are executable via the Command Prompt or PowerShell applications. These applications can be accessed from the Start menu (for example, Start > All Programs > Accessories > Windows PowerShell).
Figure 1.15 – Creating a database
If you are using a Windows machine, you may use any terminal application of your choice (Command Prompt, PowerShell, and so on), or you can use MySQL Workbench, as shown in the following screenshot, which we installed in the previous section:
Figure 1.16 – MySQL Workbench: Creating a database
Note
To execute a query using MySQL Workbench, there is a thunderbolt icon within the query’s toolbar (the icon is usually next to the save icon). Your query’s results will appear at the bottom of your screen in the Output section.
Installing Node.js
At the time of writing this book, the long-term support (LTS) version of Node.js is 16. Throughout this book, we will be using this version of Node.js, but the code base should still execute without issues using other releases. All of the corresponding operating system installations of Node.js can be found here: https://nodejs.org/en/download/.
Note
If the LTS version of Node.js is no longer version 16 and you want to use the same version as this book, you can download previous Node.js versions here: https://nodejs.org/en/download/releases/.
For managing multiple Node.js versions on one machine, there is an application called Node Version Manager (NVM) that can handle and maintain several versions of Node.js on the same machine. For more information, you can visit their repository at https://github.com/nvm-sh/nvm.
Windows
After we are done downloading and opening the Node.js Windows installer, we will be prompted with the following screen:
Figure 1.17 – Windows Node.js installer: Destination Folder
Clicking on Next will bring us to the Custom Setup step of the installation. Ensure that you are installing/configuring the following:
- Node.js runtime
- npm package manager
- Add to PATH
You can see an overview of this screen here:
Figure 1.18 – Windows Node.js installer: Custom Setup
After the Custom Setup step, we will be brought to a Tools for Native Modules section. By default, the checkbox for installing the necessary tools is unchecked. For development purposes, we will want to make sure that the automatic installation option is checked, as depicted in the following screenshot:
Figure 1.19 – Windows Node.js installer: Tools for Native Modules
Selecting the automatic tool installation will prompt a PowerShell window to appear, as illustrated in the next screenshot, showing you the status of installation progress for Chocolatey, .NET packages, Python dependencies, and so on.
Figure 1.20 – Windows Node.js installation: additional tools
Note
Chocolatey is a package manager for Microsoft’s Windows operating system. If you are familiar with the macOS environment, this would be similar to Homebrew or Apt on a Debian Linux distribution. For more information on Chocolatey, please refer to the following link: https://chocolatey.org/.
macOS
You can install Node.js for macOS via its package image, which is located at https://nodejs.org/en/download/, or you can install it with Homebrew by running the following command:
brew install node@16
To confirm that your machine is using the correct “node” binary, we can always check the version by running the following command:
node -v
Linux
For Ubuntu/Debian Linux distributions, we can use a specific repository to install Node.js 14, as illustrated in the following code snippet:
sudo apt update
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
After the repository has been added, we can install Node.js and check the version, like so:
sudo apt -y install nodejs
node -v
So far, we have finished installing MySQL as our DBMS, applicable package managers, and the Node.js runtime library; we can now begin to scaffold our project and install the necessary Node.js packages for Sequelize and Express.