Jump to content
Sign in to follow this  
Narcis

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;
  console.log("Connected!");
});

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

- node demo_db_connection.js

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

  code: 'ER_NOT_SUPPORTED_AUTH_MODE',
  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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...