Reputation: 163
I am trying to connect node.js to MySQL and failed. I have installed MySQL and relevant libraries. How do I resolve this error? Also, if I want to get data to react-native, how should I go about doing it?
const express = require('express');
const mysql = require('mysql');
const connection = mysql.createPool({
host : '*****',//aws db endpoint/MySQL hostname
user : 'administrator', // username of MySQL connection
password : '*****', //pw of MySQL db
database : 'database_3' // name of database to be used
});
const app = express();
app.get('/User', function (req, res) {
connection.getConnection(function (err, connection) {
connection.query('SELECT * FROM User', function (error, results, fields) {
if (error) throw error;
res.send(results)
});
});
});
// Starting our server.
app.listen(3306, () => {
console.log('Go to http://localhost:3306/User');
});
The error msg received :
events.js:174
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3306
at Server.setupListenHandle [as _listen2] (net.js:1279:14)
at listenInCluster (net.js:1327:12)
at Server.listen (net.js:1414:7)
at Function.listen (C:\Users\Irvin\my-new-project\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\Irvin\my-new-project\route.js:39:5)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1306:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Upvotes: 1
Views: 1765
Reputation: 912
Your nodeJs
application is using same port with MySQL
.
// Starting our server.
app.listen(3306, () => {
console.log('Go to http://localhost:3306/User');
});
That app.listen(3306)
is your nodeJs
application listening port,
your current MySQL
listening port is also 3306
,
so your nodeJs
application throw an error
Error: listen EADDRINUSE: address already in use :::3306
Means the port 3306
is already in use .
You should change your nodeJs
application listening port like 8080
or 8090
,
For example :
const express = require('express');
const mysql = require('mysql');
const connection = mysql.createPool({
host : '*****',//aws db endpoint/MySQL hostname
user : 'administrator', // username of MySQL connection
password : '*****', //pw of MySQL db
database : 'database_3' // name of database to be used
});
const app = express();
app.get('/User', function (req, res) {
connection.getConnection(function (err, connection) {
connection.query('SELECT * FROM User', function (error, results, fields) {
if (error) throw error;
res.send(results)
});
});
});
// change this port to 8090
app.listen(8090, () => {
console.log('Go to http://localhost:3306/User');
});
Upvotes: 0
Reputation: 76
Hi since you are running mysql, it runs on port 3306 by default. Since you are tryung to start your express app on the same port, you get this error. You need to start your express application on any other port.
Upvotes: 1
Reputation: 40444
3306 is the default mysql port. Don't use that port for a server if you already have mysql running.
Don't stop the process on that port otherwise mysql will stop running, unless you're running it in another port, which is not your case.
app.listen(3000, () => {
console.log('Go to http://localhost:3306/User');
});
Upvotes: 1
Reputation: 1922
Error: listen EADDRINUSE: address already in use :::3306
This means that the port 3306
is already in use, try changing it to another port or stop the process running on that port.
// Lets change 3306 to 3307
app.listen(3307, () => {
console.log('Go to http://localhost:3307/User');
});
If you want to stop the process on this port here is how to do it on linux
Upvotes: 2