简体   繁体   中英

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.

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:

在此处输入图片说明

I don't think that there is a link with React but I really have no idea about it.

Thanks in advance for your help

According to the mysql docs: https://www.npmjs.com/package/mysql you need to add 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:

process.exit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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