0

this is a code for connecting to mysql for creating a database from node.js but it doesn't work! Why this line can not be executed?

 console.log("database created");

there is my code:

const express = require('express');
const mysql = require('mysql');

const app = express();


const connection = mysql.createConnection({
    host: '127.0.0.1',
    port: 5000,
    user: 'root',
    password: 'password'

});



connection.query('CREATE DATABASE IF NOT EXISTS test', function (err) {
    if (err) throw err;
    console.log("database created");
    connection.query('USE test', function (err) {
        if (err) throw err;
        connection.query('CREATE TABLE IF NOT EXISTS users('
            + 'id INT NOT NULL AUTO_INCREMENT,'
            + 'PRIMARY KEY(id),'
            + 'name VARCHAR(30)'
            + ')', function (err) {
            if (err) throw err;
        });
    });
});

// connection.end();


app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
    console.log("send file index.html");

});


app.post('/users', function (req, res) {
    connection.query('INSERT INTO users SET ?', req.body,
        function (err, result) {
            if (err) throw err;
            res.send('User added to database with ID: ' + result.insertId);
        }
    );
});



const server = app.listen(5000, "127.0.0.1", function () {

    const host = server.address().address;
    const port = server.address().port;

    console.log("app listening at http://%s:%s", host, port)

});
4
  • Try with providing database name in mysql configuration like this : const connection = mysql.createConnection({ host: '127.0.0.1', port: 5000, user: 'root', password: 'password', database: 'dbname' }); Commented Nov 1, 2017 at 8:21
  • @AmitSingh it doesn't work Commented Nov 1, 2017 at 8:36
  • @MehdiAmirafshar check your mysql port if you have not changed, Default port of mysql is 3306 Commented Nov 1, 2017 at 8:40
  • @MehdiAmirafshar yes please do check because it is working properly with default port no. Commented Nov 1, 2017 at 8:43

2 Answers 2

2

I have tried your code but with default port no of mysql. The database is created successfully.

Here is th result

Sign up to request clarification or add additional context in comments.

2 Comments

I get this error Error: connect ECONNREFUSED 127.0.0.1:3306 at Object.exports._errnoException (util.js:1020:11) at exports._exceptionWithHostPort (util.js:1043:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
No i have not changed any other lines. First you check port no on which mysql is running. On linux systems you can useing command netstat -tln. Simile problem can be seen in another question [stackoverflow.com/questions/42899916/…
0

First of all, is MySQL running? If so, it might be worth tailing the MySQL error logs and seeing whether the connection ever reaches MySQL, if it does these error messages might be give some indication of what is happening.

Also try connecting to MySQL the same way that node would on the terminal i.e. on linux:

mysql -u user -p password -h 127.0.0.1

Sorry for writing this as an answer rather than a comment - I don't have enough reputation points.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.