Home >Web Front-end >Front-end Q&A >What should I do if node fails to connect to mysql?

What should I do if node fails to connect to mysql?

藏色散人
藏色散人Original
2023-01-03 15:41:222603browse

Solution to the failure of node to connect to mysql: 1. Execute the "mysql -u root -p" command to enter the bin directory of the MySQL folder; 2. Pass "const con = mysql.createConnection({host: '127.0 .0.1',user: 'root',password: '...',port: '3306',database: "..."});" Just connect to mysql.

What should I do if node fails to connect to mysql?

The operating environment of this tutorial: Windows 10 system, node18.4.0 version, Dell G3 computer.

What should I do if node fails to connect to mysql?

Solution to nodejs failure to connect to mysql:

First check the port number, user name, password, database, and host for errors.

If not, check that your mysql version is not suitable for 8.0 or above, because 8.0 or above uses a new encryption method, and nodejs does not support it yet, so if the version is too high, follow the following method Solution, personal test is effective.

cmd administrator enters the bin directory of the MySQL folder

Enter the command: mysql -u root -p

Enter the password

alter USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRENEVER;
alter USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '修改自己的密码';

nodejs connection mysql

npm install mysql

const mysql = require('mysql');
//创建连接
const con = mysql.createConnection({
        host: '127.0.0.1',
        user: 'root',
        password: '你自己的密码',
        port: '3306',// 自己的端口号,默认为3306
        database: "自己的数据库"
    });
//连接
con.connect(); //这里没报错的话就没有问题了,如果还是报错,那重新安装个低版本的数据库吧,卸载mysql找个靠谱的教程卸载,这玩意挺麻烦的。

Recommended learning: "node.js video tutorial"

The above is the detailed content of What should I do if node fails to connect to mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn