Node.js accessing WampServer MySQL databases - mysql

I'm really new to node.js and MySQL, and when I try to learn both at once... let's just say I need some help. ;)
I want to use the node-mysql module to dynamically edit a database via node.js. All the basic code is in place.
var http = require('http'),
mysql = require("mysql");
var connection = mysql.createConnection({
user: "root",
password: "",
database: "ballot"
});
http.createServer(function (request, response) {
request.on('end', function () {
connection.query('SELECT * FROM data;', function (error, rows, fields) {
response.writeHead(200, {
"Content-Type": "text/plain",
'Access-Control-Allow-Origin' : '*'
});
response.write(JSON.stringify(rows));
response.end();
});
});
}).listen(8080);
The problem is, I'm listening on port 8080, and localhost is of course port 80. Should I listen on port 80? If so, how do I do so without messing with Wamp? And how can I access the databases I create with PHPmyAdmin?

WAMP gives you a number of things, including MySQL and an apache web server with phpMyAdmin pre-configured.
By default the Apache web server listens on port 80 and the MySQL server listens on port 3306. With WAMP running, these ports will be taken. Your node process will be able to create a server listening on port 8080 as long as you have no other processes listening on port 8080. By default this should be fine and you will be able to access the node http server via http://localhost:8080
A connection with the MySQL database is established on port 3306. You just need to setup your database as you normally would through phpMyAdmin. By default this will be at http://localhost/phpMyAdmin which is running on the apache server on port 80.
Just to clarify, as your terminology seems slightly confused. Localhost in a host name. It's the location of the machine that you wish to talk with. The port number is completely separate and "localhost is of course port 80" doesn't make any sense. You can specify any valid port number for localhost. As I already mentioned, listening on port 8080 means you can access the node server through http://localhost:8080

Related

listen EADDRINUSE: address already in use :::3306 when attempting connection with MySQL server via Node.JS app

I am experiencing problems with MySQL connection since making a few changes, and exhausted all suggestions, found here and the net as well as official troubleshooting docs, I come here in the hope of help.
The problem.
When trying to connect to MySQL DB via Node.JS (VSC)
Error: listen EADDRINUSE: address already in use :::3306
Node works on all Ports as requested, apart from any port that MySQL uses. Also MySQL connection fails if either the port number is edited or a new instance created.
A little history of the problem:
Worked perfectly with Node.JS app + MySQL DB Workbench 8.0 (MWB). Could connect and webpage populated with data from DB with no issues until i hooked it up to AWS Elastic Beanstalk, which I have since delete but problem persists even though I'm back at the beginning.
3306 is the port MySQL listens on. Your NodeJS app should not try to also listen on that port.
You should be specifying port 3306 here:
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "root12345",
});
Not here:
app.listen(PORT, () => {
console.log(`listening to port: ${PORT}`);
});
Your app should probably be listing on port 80 or 443 or whatever is appropriate for whatever your NodeJS app is trying to do. It may not need to listen on any port at all.
Also, you are using local MySQL here, you aren't making a connection to RDS at all, you are making a connection to the MySQL software running on the same server as the NodeJS app.

How to connect mysql in lambda nodejs

How to connect mysql in lambda nodejs
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '1304',
database: 'DemoDB'
})
connection.connect(function (err) {
if (!err) {
console.log("Database connected ... ");
}
else {
console.log("Error connecting database : " + err.message);
}
});
const sql = "CREATE TABLE MESSAGE (message VARCHAR(255))";
connection.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
If this MySQL server is installed locally in your computer then here's a few troubleshooting that you can try:
Make sure your mysql services is running.
Make sure you can connect to the mysql db using that host+port+user+password combination - test connect to it using command line or sql tools like SQLyog etc.
Make sure that the mysql program and port it's using not blocked by firewall - if you have a strict firewall then you possibly need to do firewall exception for mysqld, mysqld-nt and port 3306.
To check what port is your MySQL running on, you have to look for my.ini or my.cnf file (usually located in MySQL folder or MySQL/Data folder). In that file you need to find port=XXXX and there are two of them. Make sure both values of port= are the same.
The idea here is first to make sure your MySQL server is up and running before connecting through node.js because judging from the error message that you've received, it seems like its not a problem from node.js.
AWS Lambda function runs on the cloud (some remote server) whereas you are providing localhost as the Database host which means that your MySQL server is running on your PC so that is why the connection is not establishing. You have to provide the IP of your PC instead of localhost for things to work properly.

ER_ACCESS_DENIED_ERROR: When i try to connect to the database on remote server - Node.js

I have issue when i try to connect to the database on remote server.
My code:
const mysql = require('mysql');
const database = mysql.createPool({
host: 'localhost',
user: 'user',
password: 'pass',
database: 'db'
});
database.getConnection(function (err, connection) {
if (!err) {
console.log('Database is connected ...');
} else {
console.log('Error connecting database ...');
}
});
The credentials for connection in code is faked. With the right credentials I have, I login successfully on phpMyAdmin on remote server, on datebase that I want to connect. Credentials is good.
When I run script, return this error:
view error
Also, when I input credentials for connection with my local database, everything work perfect.
As pointed out by Luuk, you need to replace the localhost with the actual IP address of the remote database server and the port on which the database server is running.
For example -
const database = mysql.createPool({
host: '123.234.121.234',
port : '3306',
user: 'user',
password: 'pass',
database: 'db'
});
Also, make sure the port is whitelisted and can be accessed over the network. Heres a tiny little diagram for explanation.
phpmyadmin runs on the same machine as your MySQL server, so it can connect to the server using the generic host name localhost.
I guess, from your question, that your nodejs program runs on some other machine (your personal machine, maybe?). That takes some special-purpose setup to do.
You must use the server's actual hostname in your host: property, not localhost.
MySQL login credentials aren't just username/password. They are host/username/password. You may need to create a new set of credentials for remote access so your nodejs program can get in. Read this: https://webmasters.stackexchange.com/questions/2242/how-to-create-separate-users-in-phpmyadmin-each-one-cant-see-others-databases
If your MySQL server runs on a rented server at some cloud or hosting service, you may need to open up a firewall to allow your machine to connect. If you're on a hosting service, ask their customer support krewe about that. On a cloud service, you want to open port 3306. Look up how to do that in their documentation. (It may be a gnarly configuration task).
Your easiest way of troubleshooting this is to use some MyQSL client program (like MySQL Workbench or HeidiSQL) on your own machine. when you get that to connect, you can use the same credentials in your createPool() call.

Sequelize Connect ETIMEOUT when connecting to remote mysql db

I am getting error SequelizeConnectionError: connect ETIMEDOUT when trying to connect to a remote mysql db with sequelize.
Connection can be established successfully when I try to connect to my local mysql db.
I'm using sequelize's default db connection code new Sequelize(...) contained within models/index.js, with the following config (filled up with the correct values):
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
I tried connecting to the remote db with a simple php script and it worked (so we can rule out issues on the remote db server side)
Any ideas?
For me, I have to:
1) Define exact information of my database at the local server (Xamp/ Mamp). It means I must have the existing database to connect, user name and password is a privileged account in your database.
2) Xamp/ Mamp must be online (of course). Default ports will be taken by this local server, so try a different port for mysql 8889 instead of 3306.
And this is what I tried:
sequelize = new Sequelize('wohui', 'root', 'root', {
dialect: 'mysql',
host: 'localhost',
port: 8889
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch((err) => {
console.log('Unable to connect to the database:', err);
});
I had the same problem, in my case it happened because I forgot to open the connection to mysql port 3306 in the inbound rules at my cloud provider
This error usually appears when the connection to the sql server is not established. Some things to take care of are :
Ensure mysql server is running in the host you are trying to connect to.
Ensure the host ip is correct.
Ensure that the port entered is correct.
Ensure that the firewall rules are defined correctly.
There could be couple of reasons for this, Listing out a few I have faced,
Remote root access not granted by the mysql server.
The configs are filtered by the environment, Which ideally is done by using the NODE_ENV variable, can you try running your server locally with prod config. In my case, I would do something like NODE_ENV=production node server.js. Assuming server.js is the start file. You can try logging the value's before new Sequelize(...), that might give a better idea as to what's going in.
I was facing the same issue. While in my case I gave a wrong port number (I didn't update my port number for the production database)
I had the same issue and my problem was in the ports. MySql had ports 3306 and in the config, i wrote port 3000 :D
Thanks for helped

Node.js establish connection to remote mysql server using tunnel-ssh

I am using the tunnel-ssh module to establish connection to the remote mysql database using node.js. The documentation is poor and I am not able to establish a connection. Here is the code snippet:
var tunnel = require('tunnel-ssh')
var server = tunnel.tunnel(config.sshData, function(err, result) {console.log('connected'});
Here is my sshData object.
config.sshData = {host : 'serverxyz.web-hosting.com', username : 'xyz', password : 'xyz',
srcPort: 3307, dstPort : 21098}
The dstPort is 21098 as suggested by the namecheap documentation.
However I am getting timeout error and whenever I add this snippet:
server.on('error', function(err) {});
I get the error server.on is not a function. The remote connection is working fine on putty and SQLyog. Any procedure on how to establish successful connection would be of great help. Thanks!
Update
Got the database working by using the correct ports specified and by directly using ssh2 module with the code example given here
There is a misunderstanding with the namecheap documentation. 21098 is the ssh port, not the port the database is listening on. In order to use a non-standard ssh port, you will need to explicitly specify the port value like:
config.sshData = {
host: 'serverxyz.web-hosting.com',
port: 21098,
username: 'xyz',
password: 'xyz',
dstPort: 3306
};
Then you should be able to connect to localhost:3306 to access your remote database.