How can i solve this error when i use mysql module? - javascript

I use the node module to connect my local database, then log this error message:
Client does not support authentication protocol requested by server; consider upgrading MySQL client
next is my configuration:
const mysql = require("mysql");
const db = mysql.createPool({
host: "127.0.0.1",
user: "root",
password: "root",
database: "my_db_01"
});
db.query('select 1', (err, results)=>{
if (err) return console.log(err.message);
console.log(results);
});
I can make sure the configuration is correct.

This looks like a frequent issue in the bug reports for the mysql module.
That module hasn't had a commit since 2020. It has 172 open issues. I would consider it abandoned.
Use a different database module. mysql2 seems to be popular.

Related

How to connect to remote phpMyAdmin server using NodeJS + ExpressJS

I'm setting up a GraphQL server however I need to connect to a remote phpMyAdmin server which my friend provided and i am getting the error:
Error: getaddrinfo ENOTFOUND 54.193.13.37/phpmyadmin 54.193.13.37/phpmyadmin:3306
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
here is the code i use to connect
const mysql = require('mysql');
const db = mysql.createConnection({
host: "54.193.13.37/phpmyadmin",
user: "some_user",
password: "some_password",
database:"some_DB",
})
db.connect((err) => {
if (err) throw err;
console.log("Connected!");
});
I've tried removing "http://", adding port, also the IP address 54.193.13.37 is an IP address for his clan website. So, the only way i have access to the database is by 54.193.13.37/phpmyadmin and i dont have any access to any config files or whatsoever, it's just a remote database that he gave me.
Thanks to whoever can help!
just remove the phpmyadmin in the host. Try host: host: "54.193.13.37".
Phpmyadmin is a web interface, but you want to connect directly to the mysql server wich is normaly on Port 3306.

Nodejs Desktop app with electron starting server when app is launched

Am building my app with electron and nodejs together with express server.
What am trying to achieve is create a desktop app with angular2 handling the routing together with express
Now on my local developenment i have to start the server (express) so that i can connect to the database, so i have the following that connects to the database
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'your-password',
database : 'cloudprint'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
THe above is run on a server.js which i start on the commandline through
npm server.js
Now am abit confused on how i can reuse the above in my app, since when a person installs the app he will not require to have to start server.js from commandline,
How should i approach this during production time.

To connect create-react-app reactJs with mysql

I have problem in connecting my create-react-app with mysql using npm.
Its working in nodejs when i tried seperately without npm. But, When i trying to connect using npm install mysql, It throws me an error...
TypeError: http.IncomingMessage is undefined
./node_modules/express/lib/request.js
Here is my code:
var express = require('express');
var mysql = require('mysql');
var connection =mysql.createConnection({
host: 'localhost',
user: 'user',
password: '',
database: 'reactproject'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
This will not work from react-app, since react code will run from client side.
You should use my-sql connection in different app and run in different port and call as api connection.
Thanks.
Adding from an #ashik answer is correct, but if you still want to use MySQL and reactjs simultaneously, you can start by creating your own boilerplate, there you can use MySQL on the server side and create APi/web service without having to separate the APi server and client projects reactjs.

How do I allow the node pg module communicate with my Windows postgresql server?

I have a node server on localhost. In it is the pg module:
//above is express requires
var pg = require('pg');
var client = new pg.Client();
client.connect((err)=>{
if(err) throw err;
client.end((err)=>{
if(err) throw err;
});
})
//below is basic express server stuff
I have, I believe, a postgresql server running on windows. I have pgadmin open, there is a localhost:5432 section, and inside is a "test" database I have created.
My node server currently throws:
error: password authentication fails for user 'myname'
I do not understand how I can, either in postgresql's Windows client, set default username/password that is then added to environment variables, or, in Windows add the proper environment variable key/value pairs that the node pg module expects so it can login, or, tell the node pg module how to login to postgresql, or, whether I am even going down the right path at all.
How can I allow my node pg module to connect to my localhost postgresql server?
EDIT: I tried to follow the instructions in this SO answer, however I would always recieve "access denied" errors no matter where I pointed the db creation to, whether I used cmd or powershell in admin or no, and no matter the permissions I set for files. I'm now primarily interacting with postgresql through the pgadmin III GUI.
EDIT2: I tried modifying the pg initialization by doing:
var connectstring = 'postgres://me:password#localhost/dbname';
var client = new pg.Client(connectstring);
I received the same error, using the same exact login details I was prompted with by pgadmin III.
EDIT3: According to these docs, I should possibly have that list of environment variables? If not, where can I find this information so I can manually set these environment variables? Is this the right path to go down?
Use:
var pg = require('pg');
var config = {
user: 'foo',
database: 'my_db',
password: 'secret',
port: 5432
};
var pool = new pg.Pool(config);

How can I query MySQL with node.js?

I am going to make a client-server application using Node.js, and I want to use a MySQL database for storing some of the data. I need to know how to query a database with server-side JavaScript.
Search the module library for mysql and you will get many modules that will let you do that including mysql, db-mysql, and easy-mysql.
npm install node-mysql
var mysql = require('mysql'),
client = mysql.createClient({
user: 'root',
password: 'root',
});
mysql.query('SELECT * FROM my_table', function(err, results, fields) {
//have fun
client.end();
});
There are other libraries as well that all have slightly different APIs.

Categories