First you should have mysql installed. (I am binary installed version 5.5)
Start the service (without starting the service database is not available):
First enter the installation directory, mine is:
cd /usr/local/mysql
Then:
sudo ./support-files/mysql.server start
Replace start with restart to restart the service, and replace with stop to close the service.
Enter mysql in the terminal:
./bin/mysql -u root -p
Where -u refers to logging in with a user name, my user name is root, -p is logging in with a password, and you are prompted to enter a password after pressing Enter.
When building the table, it is recommended to add character set = utf8 at the end (that is, set the character set encoding format to utf-8), which can support Chinese well.
Just create a new folder and create a new js file in it, for example, I am app.js
Install the module mysql in the current path
npm install mysql
Then write (paste) a small example in the js file, and modify it to see the effect.
example
var mysql = require('mysql'); var conn = mysql.createConnection({ host: 'localhost', user: 'nodejs', password: 'nodejs', database: 'nodejs', port: 3306 }); conn.connect(); var insertSQL = 'insert into t_user(name) values("conan"),("fens.me")'; var selectSQL = 'select * from t_user limit 10'; var deleteSQL = 'delete from t_user'; var updateSQL = 'update t_user set name="conan update" where name="conan"'; //delete conn.query(deleteSQL, function (err0, res0) { if (err0) console.log(err0); console.log("DELETE Return ==> "); console.log(res0); //insert conn.query(insertSQL, function (err1, res1) { if (err1) console.log(err1); console.log("INSERT Return ==> "); console.log(res1); //query conn.query(selectSQL, function (err2, rows) { if (err2) console.log(err2); console.log("SELECT ==> "); for (var i in rows) { console.log(rows[i]); } //update conn.query(updateSQL, function (err3, res3) { if (err3) console.log(err3); console.log("UPDATE Return ==> "); console.log(res3); //query conn.query(selectSQL, function (err4, rows2) { if (err4) console.log(err4); console.log("SELECT ==> "); for (var i in rows2) { console.log(rows2[i]); } }); }); }); }); }); //conn.end();