I am building an application that has a backend that uses SQL queries to get data from a SQL Server database. However, I need to write a query that truncates and repopulates a table in that database using data from a second database. Here is what my code looks like:
// establishes a connection to serverName and uses DB1 as the database. But how can you access two?
global.config = {
user: 'username',
password: 'password',
server: 'serverName',
database: 'DB1'
};
// run this query. It's already been tested in SQL server and works fine there
let query = "TRUNCATE TABLE [DB1].[dbo].[Shop]; INSERT INTO [DB1].[dbo].[Shop] (Shop, shopDescription, Address, City)" +
" SELECT Shop, Description, Address, City FROM [DB2].[dbo].[ShopTable]"
new sql.ConnectionPool(config).connect().then(pool => {
return pool.request().query(query) }).then(
result => {
console.log(result.recordset)
//result returns as "undefined"
res.setHeader('Access-Control-Allow-Origin', '*')
res.status(200);
sql.close();
}).catch(err => { //error is not thrown
res.status(500).send({ message: err})
sql.close();
});
I get an "undefined" result, and find that no update to the table was made. The issue here isn't exactly clear whether it can't reach the table in DB2, or if perhaps the command doesn't work with the Node.js mssql package?
It appears this was a front end to back end connection issue. I was able to get the query to work without any changes to the database information, so it appears I can access the second database without any issues.
component.ts
this.httpService.patch('update', {} ).subscribe()
index.js
app.patch('/api/update', controllers.manualQueries.update);
Related
I am receiving an error which looks like this (in my functions log)
Access denied for user \'varun_admin\'#\'cloudsqlproxy~84.117.112.32\' (using password: YES)',
sqlMessage:
`\'varun_admin\'#\'cloudsqlproxy~84.117.112.32\' (using password: YES)',`
sqlState: '28000',
fatal: true }
(84.117.112.32) intentionally modified.
I have double checked my username and password, In fact, I made a request from workbench and it went fine.
This is how I am creating/initialising my sql
const mysql = require('mysql')
const config = require('./../../config.js')
const connectionName = config.DB_CONNECTION_NAME
console.log(`Connection name: ${config.DB_CONNECTION_NAME}`)
const configSQL = {
host: config.DB_HOST,
user: config.DB_USER,
password: config.DB_PASSWORD,
database: config.DB_DATABASE
}
// Connection to cloud sql in production
if (!process.env.dev) {
configSQL.socketPath = `/cloudsql/${connectionName}`;
}
//SQL Config
const pool = mysql.createPool(configSQL)
// Checking if it was connected sucessfully or not on server startup
pool.getConnection((err, connection) => {
if (err) {
console.error('error connecting: ' + err)
return
}
console.log('connected as id ' + connection.threadId)
connection.release()
return
})
And the following function would typically make a call to get data
const getEverythingFromTable = tableName => {
return new Promise((resolve, reject) => {
pool.getConnection((error, connection) => {
if (error) return reject(error)
const query = `SELECT * FROM ${tableName}`
connection.query(query, (err, response) => {
connection.destroy()
if (err) return reject(err)
return resolve(response)
})
})
})
}
Any idea what I could be doing wrong?
SQL Logs
Update: 1
These are the environment values I am passing to the Cloud SQL Config
(Please refer to the code snippet above)
Where my cloudSQL config in the UI looks like this
How I am invoking functions/ calling them, the NodeJS code for it is above.
The error you are getting can be caused by an issue with your password or with the SSL encryption that is being used, as mentioned in the Verify how you connect section of the documentation.
I actually tried to see if I could reproduce the issue by I changing my instance configurations to Allow only SSL connections, as suggested by the Enforcing SSL/TLS section of the documentation. However, it didn’t cause the issue for me
This would not usually be a problem since, as mentioned in this post, the connections from Cloud Functions are encrypted by default when you use the cloudsqlproxy, but I had to test it in case something changed.
I also tried changing the configuration in order to restrict the access to my instance even more. However the only thing that failed my connection was disabling the connection through the Public IP and only allowing it through the Private one, and this made so the connection did not even reach the instance.
Since you mentioned you are able to connect with the Workbench, I believe there are 2 possible causes for your issue:
There could be a problem with the encoding of some characters in your
password, that only get mess up when trying to access it from the
env variables. I suggest you try with a very basic password to see
if you get the same result.
There could be an issue with the encryption of the connection from
the Cloud Function. If that is the case, this would be very specific
to your project and the best way to address this issue would be to
open an issue on Google’s Issue Tracker, or to open a support
case, in case you have a support plan.
I hope this helps you.
Make sure that Cloud SQL user varun_admin has the permission to connect from host cloudsqlproxy~84.117.112.32. This could also be %, but I'd rather recommend to permit only what is required to connect (which is a single host only). Also make sure to flush privileges on mySQL, so that the account changes will be applied instantly. Also see Configuring SSL/TLS.
I'm working on a MERN webapp (using MySQL instead of MongoDB) and we are having an issue where at some point after querying to add an entry, the backend is somehow re-querying the last query. In the meantime, between the original entry and the duplicate one, the backend runs continuously to keep the frontend displaying up-to-date data.
I've read on old threads that MySQL for Node may have some issues with memory leaking and thus running sleep queries. I don't believe this is a frontend issue, and the backend isn't running the queries as if they were called from the function that handles the frontend button click (to submit).
I'm a noob to full-stack JS, so I don't really know where to look for an answer to this issue. I'm not sure if we should band-aid the solution and just make sure a query isn't sent without the submit button being clicked within a 1 second time span.
EDIT: Here's code for the query to add the entry (data for a music album).
db.query(insertAlbumQuery, insertAlbumValues, function(err, result) {
if (err) throw err;
console.log("1 Album added.");
});
We don't close the connection to the db after querying, one connection runs for the duration of the app's execution.
Here's the code for the db connection:
let mysql = require("mysql");
var db;
function connectDB() {
if (!db) {
db = connection = mysql.createConnection({
host: "******",
user: "******",
password: "******",
database: "******"
});
connection.connect(function(err) {
if (err) {
return console.error("error: " + err.message);
} else {
console.log("Connected to the MySQL server.");
}
});
}
return db;
}
module.exports = connectDB();
From the main, backend app.js file we have the router post
router.post("/addAlbum", (req, res) => {
const {
Album_title,
Artist,
Release_date,
Category,
Description,
Rotation
} = req.body;
album.add(Album_title, Artist, Release_date, Category, Description, Rotation, null);
});
From the front end we post with
addAlbum = currentState => {
this.setState({ Rotation: +this.state.Rotation });
axios.post("http://localhost:3001/api/addAlbum", this.state);
};
Not sure if this is enough background on the app, but I can add more snippets if needed.
I'm relatively new to Node JS. I'm attempting to query the database, and then simply emit that data to the front end using socket.io, however I've noticed that it is intermittently sending the data / not sending the data to the front end. I'm assuming that the reason behind this is because the query has not yet finished yet, and was wondering how you'd wait for the result to be accessible before continuing?
I'm using npm mysql to access the database within the socket such as below:
var mysql = require('mysql');
var connection = mysql.createConnection(
{
host : 'localhost',
user : 'root',
password : '',
database : 'db'
}
);
Here is my query:
connection.query(queryString, function(err, result, fields){
if(err) throw err;
socket.emit('emitFunction', result);
});
The callback only called after query done so the result should available inside the callback function. Log both err and result to see what happened
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;
I have declared mysql, and my connection in app.js like so:
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '192.168.1.75',
user: 'dev',
password: 'devaccount',
database: 'PugIt'
});
app.set('connection', connection);
And in my User.js for registration I have:
router.route('/register/steam/finish')
.get(function(req, res) {
res.render('user/register_steam');
})
.post(function(req, res) {
var connection = req.app.get('connection');
connection.connect();
// Look For Users
connection.query("SELECT * FROM Users", function(err, rows, fields) {
console.log('We Found Something!');
});
connection.end();
});
When the page first loads and I hit register, it works fine, but if I hit the button a second time I get a 500 error on my server.
But if I manually declare var connection inside each route file, this does not happen.
How come I cannot use req.app.get with MySQL, I used this method when I used to use MongoDB which worked great that way I had one main config in app.js I could alter to change in all route files if I needed. Not sure why I'm getting a 500 error on second POST
I think the connection.connect() and connection.end() on every POST request is causing problems. Drop those two lines and you should be good to go. This way the connection is only established once and all requests can re-use the same connection without constantly trying to tear it down and bring it back up again.
You can also create a pool of mysql connections if you find yourself needing greater concurrency with your database queries.