MySQL is a very widely used database, so to continue my tests with sails.js, I will add it to my project.
The explanation of how to configure a docker container for the sails.js part is here.
The code to this example can be found here.
First, I will add to my docker-compose a service to build the mysql image. I will not, make any volume mapping, since this is just for a simple test and I don’t care about keeping the data.
Dockerfile and Docker Compose
The Dockerfile for mysql will be on a folder called mysql, and its contents are:
FROM mysql:8.0.13
the compose configuration:
mysql:
build: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
ports:
- '3306:3306'
networks:
- sails
With this, I will have a mysql running locally on port 3306.
Project Dependencies and Configuration
The first thing I need to do, is to add the sails-mysql dependency to my project. To do this:
npm install sails-mysql --save
Now, we need to configure the datasource on the project. To do that, in the file config/connections.js, you will find a commented out example like this:
// someMysqlServer: {
// adapter: 'sails-mysql',
// host: 'YOUR_MYSQL_SERVER_HOSTNAME_OR_IP_ADDRESS',
// user: 'YOUR_MYSQL_USER', //optional
// password: 'YOUR_MYSQL_PASSWORD', //optional
// database: 'YOUR_MYSQL_DB' //optional
// },
Uncomment the lines, and set the values according to your setup. The password, should be the same one you have set on your docker compose.
For the host, you can use your host machine address.
With this, you should have all you need to start storing data into a mysql database with your sails.js app.
Top comments (0)