Connecting To MySql Database In Strapi


This article helps you to create a connection to the MySQL database using Strapi. This is an article for the beginners where the basic connection can be created with the existing MySQL database.

I have explained it in step by step way along with providing the code snippet. Also, the framework and its features are also discussed as questions and answers below which will make the users understand actually how the code executes and establishes the connection. 

What is Strapi?

Strapi is an open-source Node.js rich framework for building applications and services. Strapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure. It is designed for building practical and production-ready Node.js applications in a matter of hours instead of weeks. The framework sits on top of Koa. It's ensemble of small modules working together to provide simplicity, maintainability, and structural conventions to Node.js applications.

Can I connect the MYSQL database to Strapi?

Yes, you are capable of connecting the MySQL database to the Strapi using the configuration settings code that helps you connect the Strapi and MySQL database.

Do we need to use any specific MySQL Framework?

Yes, it’s recommended to use the Sails framework for creating the database connection when you use the Strapi codes.

What is the Sails Framework?

Build practical, production-ready Node.js apps in a matter of weeks, not months.

"Sails" is the most popular MVC framework for Node.js, designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with a scalable, service-oriented architecture.

Which ORM is used by SAILS Framework?

Sails come bundled with an ORM called Waterline (implemented in the ORM hook.)

How to connect the MySQL using Strapi in Sails Framework?

Step 1

Install the necessary packages from the NPM [Nugget Package Manager]. Use the following code to install the Sails from the NPM.

$ npm install sails-mysql

Step 2 Configuration of Sails

Create a file name MyConnection.js. This file serves the main configuration to hold all the information to connect to the database.

  1. module.exports.connections = {  
  2.   mysql: {  
  3.     module    : 'sails-mysql',   
  4.     host      : 'localhost'// The host server address, I have used local machine  
  5.     port      : 3306, //Port of the server  
  6.     user      : 'username'//”BhuvanUser” – user name to connect to the database  
  7.     password  : 'password'// “Ert#2123” Credentails for that username   
  8.     database  : 'MySQL Database Name' //”StudentsDB” name of the database  
  10.     // OR (explicit sets take precedence)   
  11.     module    : 'sails-mysql',  
  12.     url       : 'mysql2://USER:PASSWORD@HOST:PORT/DATABASENAME'  
  14.     // Optional   
  15.     charset   : 'utf8',  
  16.     collation : 'utf8_swedish_ci'  
  17.   }  
  18. };  

Step 3

Modify the default model of configuration to config/models.js file.

  1. module.exports.models = {  
  2.   connection: 'mysql'  
  3. };  

Step 4

Execute the code and check if that works fine. Set environment variables that have to override the default database config for the testing of connection.

  1. $ WATERLINE_ADAPTER_TESTS_PASSWORD=yourpass npm test  
  2. Default settings are:  
  4. {  
  5.   host: process.env.WATERLINE_ADAPTER_TESTS_HOST || 'localhost',  
  6.   port: process.env.WATERLINE_ADAPTER_TESTS_PORT || 3306,  
  7.   user: process.env.WATERLINE_ADAPTER_TESTS_USER || 'root',  
  8.   password: process.env.WATERLINE_ADAPTER_TESTS_PASSWORD || '',  
  9.   database: process.env.WATERLINE_ADAPTER_TESTS_DATABASE || 'sails_mysql',  
  10.   pool: true,  
  11.   connectionLimit: 10,  
  12.   waitForConnections: true  
  13. }  

Step 5

When you find the connection is established successfully, then the project can be continued. If not, please check for the domain name, database credentials, and port number verification.

Sometimes, the Firewall in the system may block the port to access or block the system to access the port. So, make sure that the port is available for use and also, it's always advisable to check the code in Administrator access if the connection is not getting established.

Hope this article has helped you.

Up Next
    Ebook Download
    View all
    View all