I am using node.js to connect to Mysql database. I succeded in making the connection. However, several attempts to insert data have failed.
Can somoeone please look at the code of the js file executed in node ?
This is the js file to execute by node.js:
let mysql = require("mysql")
connection = mysql.createConnection({
host : 'localhost',
user : 'daniel',
password : '',
database : 'company',
});
connection.connect()
connection.query('INSERT INTO Employees (emp_no,emp_name,salary,reports_to) VALUES (1, "Manos", 1500, "Director")');
And this is the error I get:
node mysqlconnection3.js
/root/nodejs/mysqlconnection3.js:1
(function (exports, require, module, __filename, __dirname) { \// Read the docs at https://www.npmjs.com/package/mysql.
^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Related
Let me start off by saying that I am a complete beginner with javascript. I am trying to read up on it and learn it. I have a MySQL database hosted with GoDaddy and am attempting to connect to it with a discord bot. Here is the code below:
const Discord = require('discord.js');
const client = new Discord.Client();
var mysql = require('mysql');
var con = mysql.createConnection({
host : 'redacted',
port : 'redacted'
user : 'redacted',
password : 'redacted',
database : 'redacted'
});
con.connect(error => {
if(error) throw error;
msg.channel.send("Connection to database successful!");
});
client.login('redacted');
When I run node index.js, the error is:
user : 'redacted',
^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
I don't understand why "user" is an unexpected identifier. Can anyone steer my in the right direction?
Thank you for your time.
You have left off the comma after the redacted port.
You forgot the comma , in the previous line of code
port : 'redacted',
I'm a complete newbie to Node.js, but I do have some experience with mysql and some other languages.
I am trying to establish a database connection between Node.js and MySQL, however, my terminal keeps on giving me errors. It failed, so I tried to debug it by using "error.stack" as stated from the documentation, that failed also so I'm totally lost now. Can someone please help me to figure out the bug.
Also, can anyone also tell me how to tell Node.js to print out the reason of why it failed to connect to the database like the function "mysql_error()" from PHP.
My original Node.js code:
var MySQL = require('mysql');
var DatabaseConnection = MySQL.createConnection({
host: '10.0.0.25',
user: 'Admin',
password: 'Password'
});
DatabaseConnection.connect(function(error) {
if(!error) {
console.log("Database is connected");
return;
} else {
console.log("Database is not connected: ");
console.log(error.stack);
}
});
My result:
Database is not connected:
Error: connect ECONNREFUSED 10.0.0.25:3306
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
--------------------
at Protocol._enqueue (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/Users/marc/Documents/JSON_MySQL_Database_Test/Database_Test.js:8:20)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
I created a node application that is run by a docker-compose. The application needs a mysql server which is also started but when I get to the point in my node application to access the server I get the following error:
events.js:136
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:1031:13)
at _exceptionWithHostPort (util.js:1052:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)
--------------------
at Protocol._enqueue (/usr/src/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/usr/src/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/usr/src/node_modules/mysql/lib/Connection.js:130:18)
at Connection._implyConnect (/usr/src/node_modules/mysql/lib/Connection.js:461:10)
at Connection.query (/usr/src/node_modules/mysql/lib/Connection.js:206:8)
at Object.<anonymous> (/usr/src/configs/passport.js:15:12)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Module.require (module.js:585:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/usr/src/app.js:47:1)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
The corresponding node code is essentially this:
var mysql = require('mysql');
var dbconfig = require('./database');
var connection = mysql.createConnection(dbconfig.connection);
And database.js looks like this:
module.exports = {
'connection': {
'host': '127.0.0.1',
'user': 'user',
'password': 'password'
},
'database': 'website_user',
'users_table': 'users'
};
All of this is run lokally on my MacBook. When I open a second terminal window and execute:
mysql -h 127.0.0.1 -u user -p
I can successfully connect to this server. The user specified there is identical to the user that is passed to my connection establishment in the node app. I've messed around with this for probably 4 hours now and have no idea what I'm doing wrong. What do I have to change in order for this to work?
Let me know if there is anything I need to provide in order to help me with my problem. Thanks!
You are trying to connect to 127.0.0.1:3306 from the node container - but that's the node container itself. If your database service is named mysql you should be able to use a config like this (assuming docker-compose.yml version 2 or above):
module.exports = {
'connection': {
'host': 'mysql',
'user': 'user',
'password': 'password'
},
'database': 'website_user',
'users_table': 'users'
};
I'm trying to create a link between node.js and mysql. I get an error as connect ECONNREFUSED. how to solve ths?
CODE:
var mysql = require('mysql');
var conn = mysql.createConnection({
host : "192.164.0.114",
user : "supriya",
password : "root123#",
database: "student"
}
);
var queryString = "SELECT * FROM studata";
conn.query("someurlhere",queryString, function(error, results)
{
if(error)
{
throw error;
}
else {
console.log(results);
}
})
conn.end();
Error is:
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
--------------------
at Protocol._enqueue (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/supriya/node_modules/mysql/lib/Connection.js:109:18)
at Connection._implyConnect (/home/supriya/node_modules/mysql/lib/Connection.js:387:10)
at Connection.query (/home/supriya/node_modules/mysql/lib/Connection.js:174:8)
at Object.<anonymous> (/home/supriya/node_js/example2.js:19:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
I had the same problem. I invoked node server.js, which says
It's Started on PORT 3000 Problem with MySQLError: connect
ECONNREFUSED
By first line, I assumed, intially, it starts mysql server. However, that is not the case. it starts node server, which tries to connect to mysql server and node server says ECONNREFUSED. Clearly, mysql server was not running. I found this, when I logged in, as root, in mysql workbench and tried to execute simple query. There in the corner it said, "..at port 3306, it is not connected" or sth like that.
So, conclusion is starting server.js on some port starts node server and not mysql server.
At the minute I'm just trying to connect to the database but I am getting a connection refused error.The database is from a wordpress site that was set up and I am trying to connect to that through Node using the node mysql driver - https://www.npmjs.org/package/mysql
The way the user's have looked at the database before was through phpmyadmin and im not sure what to use as the hostname for a database like this. i have tried using the server name given to me - cust-mysql-123-14 but that just gives me a address not found error obviously. The only other information i have about the server is this address;
linweb14.atlas.pipex.net
When i connect with that it then throws up this error;
error connecting: Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
--------------------
at Protocol._enqueue (/Users/tp/Desktop/angular-seed/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/Users/tp/Desktop/angular-seed/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Users/tp/Desktop/angular-seed/node_modules/mysql/lib/Connection.js:108:18)
at Object.<anonymous> (/Users/tp/Desktop/angular-seed/app/server.js:29:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Heres the database bit of code in my .js file (replaced connection details);
var mysql = require('mysql');
var connection = mysql.createConnection(
{
host : 'hidden',
user : 'hidden',
password : 'hiddden',
database : 'hidden',
port : 3000
}
);
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
Any ideas or advice on where I'm going wrong?