Jump to content

Node.js. Connect to MySQL


Recommended Posts

I try to do the Node.js MySQL tutorial. https://www.w3schools.com/nodejs/nodejs_mysql.asp

- I have downloaded MySQL from here: https://dev.mysql.com/downloads/mysql/

- I clicked on the file and installed it.

- Now I have it in my Mac System Preferences > MySQL

- I put that in Terminal: npm install mysql

- In demo_db_connection.js I put

var mysql = require('/Users/myName/node_modules/mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "myPassword"

con.connect(function(err) {
  if (err) throw err;

- Then in Termimal: cd (folder where I have the file)

- node demo_db_connection.js

- It gives me a long error. At the end: 

  errno: 1251,
  sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client',
  sqlState: '08004',
  fatal: true

I remember the password but I am not sure if I get the user right. I tried: localhost, myName, root. Could that be the cause of the error? How to solve it?

In require I put the address where I have the node modules. I am not sure if that is right?

Edited by Narcis
Link to comment
Share on other sites

Node's mysql module doesn't support Mysql server 8 authentication just yet. So we need to change the server so that it uses the old version of auth.

If you can execute an SQL query (not in node), then you can run the following

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

Replacing, 'password' with your own password of choice. (Assuming you're still user root on localhost domain.)

Then run

flush privileges;

And try node after that.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...