Connecting a NodeJS container to a MySQL database











up vote
0
down vote

favorite












Before marking this post as a duplicate note that I've already looked at the other related posts here and pretty much everywhere I could without finding a solution to my precise case. I just keep getting confused between docker-compose.yml versions and linking the ports to each other.



Here's the issue:
I've created two containers with Docker, one hosting a NodeJS server, one hosting a MySQL database.



My docker-compose.yml looks like this:



version: '3'
services:

server:
build: ./server/
ports:
- "8080:8080"
depends_on:
- db

db:
build: ./db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: dashboard_nodejs
MYSQL_USER: root
MYSQL_PASSWORD: password

client:
image: angular_app
build: ./front/
ports:
- "4242:80"
depends_on:
- server


When running docker-compose up, all of my apps are built and run without an issue until the server tries to connect to MySQL, exiting with :



Error: connect ECONNREFUSED 172.20.0.2:3306 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)


The dockerfile for my database only contains



FROM mysql

COPY init_db.sql /docker-entrypoint-initdb.d/


and init_db.sql currently is



CREATE DATABASE IF NOT EXISTS dashboard_nodejs;

GRANT ALL PRIVILEGES ON dashboard_nodejs.*
TO 'root'@'%' IDENTIFIED BY 'password'
WITH GRANT OPTION;


Finally, my server attemps to establish the connection with the following parameters:



var con = mySql.createConnection({
host: "db",
user: "root",
password: "password",
database: "dashboard_nodejs"
});


I have no clue why the connection is refused by mysql even with (what looks to me like) the right credentials.
I apologize in advance if this particular case has already been discussed.










share|improve this question




























    up vote
    0
    down vote

    favorite












    Before marking this post as a duplicate note that I've already looked at the other related posts here and pretty much everywhere I could without finding a solution to my precise case. I just keep getting confused between docker-compose.yml versions and linking the ports to each other.



    Here's the issue:
    I've created two containers with Docker, one hosting a NodeJS server, one hosting a MySQL database.



    My docker-compose.yml looks like this:



    version: '3'
    services:

    server:
    build: ./server/
    ports:
    - "8080:8080"
    depends_on:
    - db

    db:
    build: ./db
    environment:
    MYSQL_ROOT_PASSWORD: root
    MYSQL_ALLOW_EMPTY_PASSWORD: 1
    MYSQL_DATABASE: dashboard_nodejs
    MYSQL_USER: root
    MYSQL_PASSWORD: password

    client:
    image: angular_app
    build: ./front/
    ports:
    - "4242:80"
    depends_on:
    - server


    When running docker-compose up, all of my apps are built and run without an issue until the server tries to connect to MySQL, exiting with :



    Error: connect ECONNREFUSED 172.20.0.2:3306 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)


    The dockerfile for my database only contains



    FROM mysql

    COPY init_db.sql /docker-entrypoint-initdb.d/


    and init_db.sql currently is



    CREATE DATABASE IF NOT EXISTS dashboard_nodejs;

    GRANT ALL PRIVILEGES ON dashboard_nodejs.*
    TO 'root'@'%' IDENTIFIED BY 'password'
    WITH GRANT OPTION;


    Finally, my server attemps to establish the connection with the following parameters:



    var con = mySql.createConnection({
    host: "db",
    user: "root",
    password: "password",
    database: "dashboard_nodejs"
    });


    I have no clue why the connection is refused by mysql even with (what looks to me like) the right credentials.
    I apologize in advance if this particular case has already been discussed.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Before marking this post as a duplicate note that I've already looked at the other related posts here and pretty much everywhere I could without finding a solution to my precise case. I just keep getting confused between docker-compose.yml versions and linking the ports to each other.



      Here's the issue:
      I've created two containers with Docker, one hosting a NodeJS server, one hosting a MySQL database.



      My docker-compose.yml looks like this:



      version: '3'
      services:

      server:
      build: ./server/
      ports:
      - "8080:8080"
      depends_on:
      - db

      db:
      build: ./db
      environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_DATABASE: dashboard_nodejs
      MYSQL_USER: root
      MYSQL_PASSWORD: password

      client:
      image: angular_app
      build: ./front/
      ports:
      - "4242:80"
      depends_on:
      - server


      When running docker-compose up, all of my apps are built and run without an issue until the server tries to connect to MySQL, exiting with :



      Error: connect ECONNREFUSED 172.20.0.2:3306 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)


      The dockerfile for my database only contains



      FROM mysql

      COPY init_db.sql /docker-entrypoint-initdb.d/


      and init_db.sql currently is



      CREATE DATABASE IF NOT EXISTS dashboard_nodejs;

      GRANT ALL PRIVILEGES ON dashboard_nodejs.*
      TO 'root'@'%' IDENTIFIED BY 'password'
      WITH GRANT OPTION;


      Finally, my server attemps to establish the connection with the following parameters:



      var con = mySql.createConnection({
      host: "db",
      user: "root",
      password: "password",
      database: "dashboard_nodejs"
      });


      I have no clue why the connection is refused by mysql even with (what looks to me like) the right credentials.
      I apologize in advance if this particular case has already been discussed.










      share|improve this question















      Before marking this post as a duplicate note that I've already looked at the other related posts here and pretty much everywhere I could without finding a solution to my precise case. I just keep getting confused between docker-compose.yml versions and linking the ports to each other.



      Here's the issue:
      I've created two containers with Docker, one hosting a NodeJS server, one hosting a MySQL database.



      My docker-compose.yml looks like this:



      version: '3'
      services:

      server:
      build: ./server/
      ports:
      - "8080:8080"
      depends_on:
      - db

      db:
      build: ./db
      environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_DATABASE: dashboard_nodejs
      MYSQL_USER: root
      MYSQL_PASSWORD: password

      client:
      image: angular_app
      build: ./front/
      ports:
      - "4242:80"
      depends_on:
      - server


      When running docker-compose up, all of my apps are built and run without an issue until the server tries to connect to MySQL, exiting with :



      Error: connect ECONNREFUSED 172.20.0.2:3306 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)


      The dockerfile for my database only contains



      FROM mysql

      COPY init_db.sql /docker-entrypoint-initdb.d/


      and init_db.sql currently is



      CREATE DATABASE IF NOT EXISTS dashboard_nodejs;

      GRANT ALL PRIVILEGES ON dashboard_nodejs.*
      TO 'root'@'%' IDENTIFIED BY 'password'
      WITH GRANT OPTION;


      Finally, my server attemps to establish the connection with the following parameters:



      var con = mySql.createConnection({
      host: "db",
      user: "root",
      password: "password",
      database: "dashboard_nodejs"
      });


      I have no clue why the connection is refused by mysql even with (what looks to me like) the right credentials.
      I apologize in advance if this particular case has already been discussed.







      mysql node.js docker






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 19:53

























      asked Nov 11 at 19:47









      ilomax

      148114




      148114
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Add ports: "3306:3306" to the db service.






          share|improve this answer





















          • I've tested that, this is the result: ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint <db>: Error starting userland proxy: Bind for 0.0.0.0:3306: unexpected error Permission denied
            – ilomax
            Nov 11 at 19:58










          • What OS do you use? Can you try to run it priviledged: true Don't do it permanently, just try out, because it is a security risk.
            – lependu
            Nov 11 at 20:03












          • I'm running Windows 10 Pro. Where am I supposed to add that attribute privileged attribute ?
            – ilomax
            Nov 11 at 20:51










          • To the db service right after the build line. Also are You sure, that no previous container uses the 3306 port? check it with docker ps -a
            – lependu
            Nov 11 at 20:53













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252551%2fconnecting-a-nodejs-container-to-a-mysql-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Add ports: "3306:3306" to the db service.






          share|improve this answer





















          • I've tested that, this is the result: ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint <db>: Error starting userland proxy: Bind for 0.0.0.0:3306: unexpected error Permission denied
            – ilomax
            Nov 11 at 19:58










          • What OS do you use? Can you try to run it priviledged: true Don't do it permanently, just try out, because it is a security risk.
            – lependu
            Nov 11 at 20:03












          • I'm running Windows 10 Pro. Where am I supposed to add that attribute privileged attribute ?
            – ilomax
            Nov 11 at 20:51










          • To the db service right after the build line. Also are You sure, that no previous container uses the 3306 port? check it with docker ps -a
            – lependu
            Nov 11 at 20:53

















          up vote
          0
          down vote













          Add ports: "3306:3306" to the db service.






          share|improve this answer





















          • I've tested that, this is the result: ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint <db>: Error starting userland proxy: Bind for 0.0.0.0:3306: unexpected error Permission denied
            – ilomax
            Nov 11 at 19:58










          • What OS do you use? Can you try to run it priviledged: true Don't do it permanently, just try out, because it is a security risk.
            – lependu
            Nov 11 at 20:03












          • I'm running Windows 10 Pro. Where am I supposed to add that attribute privileged attribute ?
            – ilomax
            Nov 11 at 20:51










          • To the db service right after the build line. Also are You sure, that no previous container uses the 3306 port? check it with docker ps -a
            – lependu
            Nov 11 at 20:53















          up vote
          0
          down vote










          up vote
          0
          down vote









          Add ports: "3306:3306" to the db service.






          share|improve this answer












          Add ports: "3306:3306" to the db service.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 19:55









          lependu

          669314




          669314












          • I've tested that, this is the result: ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint <db>: Error starting userland proxy: Bind for 0.0.0.0:3306: unexpected error Permission denied
            – ilomax
            Nov 11 at 19:58










          • What OS do you use? Can you try to run it priviledged: true Don't do it permanently, just try out, because it is a security risk.
            – lependu
            Nov 11 at 20:03












          • I'm running Windows 10 Pro. Where am I supposed to add that attribute privileged attribute ?
            – ilomax
            Nov 11 at 20:51










          • To the db service right after the build line. Also are You sure, that no previous container uses the 3306 port? check it with docker ps -a
            – lependu
            Nov 11 at 20:53




















          • I've tested that, this is the result: ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint <db>: Error starting userland proxy: Bind for 0.0.0.0:3306: unexpected error Permission denied
            – ilomax
            Nov 11 at 19:58










          • What OS do you use? Can you try to run it priviledged: true Don't do it permanently, just try out, because it is a security risk.
            – lependu
            Nov 11 at 20:03












          • I'm running Windows 10 Pro. Where am I supposed to add that attribute privileged attribute ?
            – ilomax
            Nov 11 at 20:51










          • To the db service right after the build line. Also are You sure, that no previous container uses the 3306 port? check it with docker ps -a
            – lependu
            Nov 11 at 20:53


















          I've tested that, this is the result: ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint <db>: Error starting userland proxy: Bind for 0.0.0.0:3306: unexpected error Permission denied
          – ilomax
          Nov 11 at 19:58




          I've tested that, this is the result: ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint <db>: Error starting userland proxy: Bind for 0.0.0.0:3306: unexpected error Permission denied
          – ilomax
          Nov 11 at 19:58












          What OS do you use? Can you try to run it priviledged: true Don't do it permanently, just try out, because it is a security risk.
          – lependu
          Nov 11 at 20:03






          What OS do you use? Can you try to run it priviledged: true Don't do it permanently, just try out, because it is a security risk.
          – lependu
          Nov 11 at 20:03














          I'm running Windows 10 Pro. Where am I supposed to add that attribute privileged attribute ?
          – ilomax
          Nov 11 at 20:51




          I'm running Windows 10 Pro. Where am I supposed to add that attribute privileged attribute ?
          – ilomax
          Nov 11 at 20:51












          To the db service right after the build line. Also are You sure, that no previous container uses the 3306 port? check it with docker ps -a
          – lependu
          Nov 11 at 20:53






          To the db service right after the build line. Also are You sure, that no previous container uses the 3306 port? check it with docker ps -a
          – lependu
          Nov 11 at 20:53




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252551%2fconnecting-a-nodejs-container-to-a-mysql-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues