errorError: getaddrinfo ENOTFOUND - mysql

前端 未结 5 1448
温柔的废话
温柔的废话 2021-01-18 01:17

I\'m running a server on c9.io using Node.js and trying to connect to Mysql I guess I get this error:

Error: getaddrinfo ENOTFOUND

相关标签:
5条回答
  • 2021-01-18 01:24

    I was facing this issue. I did fix it simply you have to change your configuration of a connection string like if you are running on local machine try

    host:'localhost' or host:'127.0.0.1'

    ...and set your user name and if you want this to publish your code on server then give the host according to server if in docker container give it name host:'db'.

    0 讨论(0)
  • 2021-01-18 01:26

    I know it's been a while since this was asked but I spent the better part of today trying to fix this. What to check for:

    Within your nodejs source:

    • Try it without the 'port' option unless on your db VM, MySQL is listening on a port besides 3306. I had this option specified but had connection problems until I removed it. And 3306 is already the default value for the option anyway.
    • Try it with the actual host IP in the host 'option'. I was using an actual domain name and, more often than not, node-mysql would throw the same exact "errorError: getaddrinfo ENOTFOUND" When I used the IP address, no more errors. Also, I did not get this error while on Ubuntu instances in AWS. As soon as I switched over to Ubuntu VMs in Azure, I got the problem.
    • Set the "debug" connection option to true which will give you a verbose trace of what's happening during the connection

    On your db VM/Box/Instance:

    • Ensure that MySQL is listening on the right port
    • If you're seeing the error when trying to make concurrent connections using a pool, check that your max_connections and max_user_connections haven't been changed from the default settings in my.conf
    • Monitor "SHOW PROCESSLIST;" in MySQL to see if you see any issues there
    0 讨论(0)
  • 2021-01-18 01:33

    After one hour, and I don't know why, I found the issue.

    In my case I replaced

    host: 'localhost'
    

    by

    host: '127.0.0.1'
    
    0 讨论(0)
  • 2021-01-18 01:34

    I almost spend two hours to understand what is the problem with the mysql database. I am using Windows cmd. I tried multiple ways e.g

    1. npm install mysql2
    2. Changing localhost to 127.0.0.1
    3. Refreshing node

    so in order to resolve this issue

    [Error: getaddrinfo ENOTFOUND 127.0.0.1:3306]
    

    I open XAMPP and start the MySQL database then manually check the credentials and works fine for me.

    var mysql = require('mysql');
    
    var connection = mysql.createConnection({
      host    : 'localhost',
      user    : 'test',
      password: 'test123',
    }); 
    
    0 讨论(0)
  • 2021-01-18 01:43

    This code works for me.

    var mysql = require('mysql');
    
    var con = mysql.createConnection({
        host: '127.0.0.1',
        port: '3306',
        user: 'yourUsername',
        password: '**********'
    });
    
    con.connect(function(err){
        if(err) throw err;
        console.log('connected!');
    });
    

    the host and port name find them in your mysql server. in-place of '127.0.0.1' you can use 'localhost'

    0 讨论(0)
提交回复
热议问题