简体   繁体   English

使用Node.js连接到MySQL数据库

[英]Connect to MySQL database with Node.js

I'm experiencing an issue when trying to connect to a MySQL database with Node.JS in my React Native application. 在我的React Native应用程序中尝试使用Node.JS连接到MySQL数据库时遇到问题。

var mysql = require('mysql');

var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    port: 8889,
    database: "blabla"
}); 

con.connect(function(err) {
    if (err) console.log(err);
});

I can access to phpmyadmin without any problem (same user and password) but when I execute the JS file the terminal seems to load but stay stuck like this: 我可以毫无问题地访问phpmyadmin(相同的用户名和密码),但是当我执行JS文件时,终端似乎已加载,但像这样保持卡住:

在此处输入图片说明

I don't think that there is a link with React but I really have no idea about it. 我不认为与React有联系,但我真的对此一无所知。

Thanks in advance for your help 在此先感谢您的帮助

According to the mysql docs: https://www.npmjs.com/package/mysql you need to add con.end(); 根据mysql文档: https : con.end();您需要添加con.end(); after you are finished with the connection. 完成连接后。

I just understood that I didn't exit the script, but it's working (sorry). 我只是知道我没有退出该脚本,但是它正在运行(抱歉)。

The line to end the script for Node.js beginners like me: 对于像我这样的Node.js初学者结束脚本的行:

process.exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM