I'm trying to connect to a mysql database using node. It is working perfectly fine when connecting with localhost.
var pool = mysql.createPool({
connectionLimit : 100,
host : 'http://<ip-address here>/phpmyadmin',
user : '*********',
password : '*********',
database : '*********'
});
pool.getConnection(function(err, connection) {
// Use the connection
connection.query( 'SELECT * FROM table', function(err, rows) {
// And done with the connection.
connection.release();
// Don't use the connection here, it has been returned to the pool.
});
});
The above code is not working.
I haven't found any answer on Google as well as here.
Host is only contains IP address, try to remove phpmyadmin.
Related
Can someone explain or point to some tutorial where is explained how to render rows of data from mySql database into react.js component?
I've made small database using mysql workbench and this baza.js file inside my project folder:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'db'
});
connection.connect(function(err) {
if (err) throw err;
connection.query("SELECT * FROM promjer", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
When I run node baza.js in my CMD, everything seems fine, I get everything from that specific table inside CMD terminal so I guess my database is ok and it's connected with app.
What troubles me is how to render that data inside my app?
I know that React by itself can't handle data so i should use Express.js. But I don't get what to do with it. Express should be running on other port so how should I even get data to component in app which is running on port 3000 if express is running on port 9000?
Thanks in advance!
I'm using mssql along with node.
I call a function to connect to the database:
const sql = require('mssql')
async () => {
try {
const pool = await sql.connect(`mssql://${process.env.DATAUSER}:${process.env.DATAPASS}#${process.env.SERVER}`)
} catch(err) {
console.log(err)
}
}
This works perfectly fine when i'm on a localhost but after i've hosted it to heroku and add the env variables. The error I get is (I changed the ip address to 5's):
ConnectionError: Failed to connect to 55.5.5.555:1433 in 15000ms
My process.env.SERVER = 55.5.5.555/SpecificDatabase and If that means anything
I don't see any reference to your specific database or schema in this code. It may be possible that your local implementation of the data server differs from the heroku implementation in respect to the default schema or database to which a user is directed. Therefore, you may need to add a database or schema field to your connection call.
For example, my pool code to connect to the OSU server is:
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'classmysql.engr.oregonstate.edu',
user : 'username',
password : 'password',
database : 'database_name',
multipleStatements: true
});
module.exports.pool = pool;
Though my implementation here was with mysql and not mssql, it may hold true that you need to explicitly name a specific database or schema.
So i have been trying to connect to a MySQL database from my electron app:
<script>
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '*********',
user : '******',
password : '******',
port : '******',
debug : 'true'
});
console.log('trying to connect to DB')
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
connection.end()
</script>
My code gets stuck on connection.connect and after about 1 min it give me:
error connecting: Error: read ECONNRESET
at exports._errnoException (util.js:1024:11)
at TCP.onread (net.js:610:25)
I have no idea what to do, anyone have any ideas?
The chances are that your mssql instance on your lan is not configured to allow connections to anything but your local machine, i.e the machine that mssql is installed.
To allow connections from your LAN you need to edit the following:
edit my.conf and find the bind-address parameter in the [mysqld] section, this will probably be set to localhost or 127.0.0.1, you can either change this to the ip of the machine you want to connect to the database, or use a wildcard and allow your whole local range, e.g 192.168.0.* or 0.0.0.0
You will need to grant access to the database you are trying to connect to
GRANT ALL ON <local database name>.* TO <user>#<iptryingtoconnectfrom> IDENTIFIED BY '<database user password>';
Restart the mysql service and you should be good to go.
I'm running a node.js script to broadcast data to all connected web-app users every 15 seconds. It's running with this command...
nohup node /pushNotifications.js &
And this is the code...
var https = require('https'), fs = require('fs'), app = require("express"), key = fs.readFileSync('apache.key', 'utf8'), cert = fs.readFileSync('apache.crt', 'utf8')
var server = https.createServer({key: key, cert: cert}, app);
server.listen(8080)
var io = require("socket.io").listen(server);
var mysql = require('mysql');
function handler (req, res) {
}
io.on('connection', function (socket) {
setInterval( function() {
var connection = mysql.createConnection({
host: 'db.server.com', user: 'dbuser', password: 'dbpass', database: 'dbname', port: 3306
});
connection.connect(function(err){
});
connection.query('SELECT someColumn FROM someTable ', function(err, rows, fields) {
if (!err) {
socket.emit('notifications', JSON.stringify(rows));
connection.end();
} else {
connection.end();
}
});
}, 15000); //15 seconds
});
It's always worked fine, but recently I've started getting errors in the web app saying "User already has more than 'max_user_connections' active connections", and upon investigation at the DB level using MySQL's "show processlist", I see rapidly spawning/dying connections - all I need to do is kill/restart the pushNotifications.js script and everything is back to normal.
What I'm hoping is that somebody sees something wrong with my code that may be failing to handle a scenario that could lead to processes repeatedly spawning at intervals more regular than every 15 seconds. Appreciate any thoughts at all because I'm out of ideas to diagnose this further.
You're creating a new database connection for each client connection and each interval, which is rather wasteful.
It's much better to create a decently sized connection pool once, and use connections from that:
let pool = mysql.createPool({
connectionLimit : 10, // this may require tweaking depending on # of clients
...
});
io.on('connection', function(socket) {
setInterval(function() {
pool.query('SELECT someColumn FROM someTable ', function(err, rows, fields) {
if (!err) {
socket.emit('notifications', JSON.stringify(rows));
connection.end();
} else {
connection.end();
}
});
}, 15000);
});
Some additional remarks:
once a client disconnects, its associated interval isn't cleared/stopped. At some point that will start causing problems, because its running queries on behalf of a client that isn't there anymore; you should probably use a listener on the disconnect event to call clearInterval to clean up resources when the server detects a client disconnected.
your example code doesn't show if the database query is specific for each client. If it's not, you should move the interval to outside the io.on() block entirely, and use Socket.IO broadcasting to send all connections clients the data (instead of running the exact same query for each client separately)
When i run my node.js script i get this error:
http://i.gyazo.com/4abc4f518db0de3cb36e34a9fa163e22.png
I was reading a similar error and the guy said it was because the script wasn't connecting to the mysql database. I've tried all of the different logins i can think of and it still wont work. I have tried connecting with no credentials and i get the same error. These are the current credentials i am using to log in:
host : 'localhost',
user : 'root',
password : 'mypassword',
database : 'milky',
The code where the error comes from:
mysqlConnection.query('SELECT `value` FROM `info` WHERE `name`=\'maxitems\'', function(err, row, fields) {
if(offer.items_to_receive.length > row[0].value) {
offers.declineOffer({tradeOfferId: offer.tradeofferid});
offer.items_to_receive = [];
mysqlConnection.query('INSERT INTO `messages` (`userid`,`msg`,`from`) VALUES (\''+offer.steamid_other+'\',\'toomuch\',\'System\')', function(err, row, fields) {});
return;