An open API service indexing awesome lists of open source software.

https://github.com/davidthorn/node-sequelize


https://github.com/davidthorn/node-sequelize

Last synced: about 1 year ago
JSON representation

Awesome Lists containing this project

README

          

# Node & Sequelize

This project is about configure a Docker container which is linked to a mysql service running.

we will use `sequelize` npm package to manage the our databases.

First we need to create the services so as to be able to use `sequelize`.

# Create docker-compose.yml

```bash
touch docker-compose.yml
```

Due to me not really knowing at this point how I am going to do this, I am referencing the docker documentation website about `docker compose`:

[Overview of Docker Compose](https://docs.docker.com/compose/overview/)

## Create a `mysql` service

```yaml
version: '3'
services:
db:
container_name: db1 # the name of container
image: mysql # the name of the image which will be used to beuild the container
environment:
- MYSQL_ROOT_PASSWORD=12345678 # set the mysql root password here
volumes:
- /tmp/mysql:/var/lib/mysql # create a volume so that the databases do not get reset upon every up command of docker compose
```

## Additional steps.

At this point it you will need to run the following command and add a database user which is `IDENTIFIED` with `mysql_native_password` otherwise the `node-mysql` will encounter a `AUTH_MODE` error.

> Run this command to start the mysql service

```bash
docker-compose up -d
```

You should see the following output:

```bash
Starting mysql ... done
```

## Create your database user

To enter the mysql container run the following command:

```bash
docker exec -it mysql bash
```

Now your bash prompt should change to something like this:

```bash
root@1a244c1608ad:/#
```
Run this command to add a 'node' user with the correct privileges to the database.

```bash
echo "CREATE USER 'ADD_USERNAME_HERE'@'%' IDENTIFIED WITH mysql_native_password BY 'ADD_PASSWORD_HERE';\
GRANT ALL PRIVILEGES on *.* TO 'ADD_USERNAME_HERE'@'%';\
FLUSH PRIVILEGES;\
" > user.sql

mysql -p < user.sql
```

## Create node service

To talk to the mysql container through `http` lets add a `sequelize` service beneath the `db` service.

```yaml

version: '3'
services:

sequelize:
container_name: s1 # the name of the node service
build: . # use docker file in this directory
ports:
- 3000:3000 # link port 3000 on this machine to the port 3000 on the container
links:
- db # link the db service to this container
volumes:
- /home/david/node-sequelize:/app # create a volume so we look straight in the container
```

## Start services

Now that we have configured the docker compose file we can start both the services.

```bash
docker-compose up -d
```

> You should see the following output

```bash
Creating network "node-sequelize_default" with the default driver
Creating mysql ... done
Creating s1 ... done
```

## To be continued....