4

So I am working with Loopback (Node.js framework) for the first time and I tried to set up a Mysql model. I've installed Loopback globally, and also installed the mysql connector with npm. After that, I tried to add a datasource with the mysql connector. I tried to use my MAMP localhost database.

Unfortunatly I get an error when I try to connect with loopback again typing the node . command. The error: Error: connect ECONNREFUSED 127.0.0.1:3306.

So I did some research and many people answered on different node questions saying you have to add the socket of MAMP to your config in order to get a connection. So I tried to add that but that doesn't seem to work or I am not doing it right, because, I can't find the proper way of doing it. Here is my datasource:

  "db": {
    "host": "127.0.0.1",
    "port": "3306",
    "url": "",
    "database": "meetups",
    "password": "root",
    "name": "root",
    "user": "root",
    "connector": "mysql"
  }

And I tried to add "socketPath": "/var/run/mysqld/mysqld.sock" and "socket": "/var/run/mysqld/mysqld.sock" but that doesn't seem to work. Is there anybody out there with exp. in node.js / loopback?

Giesburts
  • 6,879
  • 15
  • 48
  • 85

2 Answers2

5

After researching the Loopback MySQL docs I have found out that you can use additional parameters supported by node-mysql, that's located here.

At the Connection options I found socketPath. So that's actually the parameter for socket when using MAMP. After using that, and also removing the url parameter, the connection worked with this:

  "db": {
    "host": "127.0.0.1",
    "port": 3306,
    "database": "meetups",
    "password": "root",
    "name": "db",
    "user": "root",
    "connector": "mysql",
    "socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"
  }

The only problem I got after was something with the database tables. I had to autoimigrate them for some reason, don't exactly knew what that was but I found the solution over here. I tried the Grunt automigrate task and now my loopback backend with MySQL works fine so far.

Giesburts
  • 6,879
  • 15
  • 48
  • 85
0

This is my working datasource mysql setup:

"voipnow": { //should equal the name param
    "host": "localhost",
    "port": 3306,
    "database": "databasename",
    "username": "root",
    "password": "root",
    "name": "voipnow",
    "connector": "mysql",
    "namingStrategy": "underscore"
  },

The name should match the declared datasource name

Mark Ryan Orosa
  • 847
  • 1
  • 6
  • 21
  • Is this with MAMP? Still the same error unfortunatly. Has something to do with that Socket of MAMP I guess but I still don't know how to implement that in my datasource file. – Giesburts Oct 28 '17 at 08:02
  • 1
    See my answer below for more info, fixed it :) – Giesburts Oct 28 '17 at 09:51