2017年3月23日 星期四

[Node.js][mysql] How to install mysql(mariaDB)

Install mysql(mariaDB) package
sudo pacman -S mysql


Add one new user was called “mysql”.
sudo useradd mysql


Change mysql’s owner
sudo chown mysql:mysql /var/lib/mysql/ -Rf

You need to initialize the MariaDB data directory prior to starting the service.
sudo mysql_install_db –user=mysql –basedir=/usr –datadir=/var/lib/mysql


Mysql(mariaDB) will create mysqld.sock under “/var/run/mysqld”, so I need to prepare right folder and right permission of folder for mysql(mariaDB).
sudo mkdir /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld/


Start mysql(mariaDB) now
sudo systemctl start mysqld


Start mysql(mariaDB) after booting.
sudo systemctl start mysqld


[Node.js][Simple Example] How to connect to mysql

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : '< MySQL username >',
  password : '< MySQL password >',
  database : '<your database name>'
});

connection.connect();

connection.query('SELECT * from < table name >', function(err, rows, fields) {
  if (!err)
    console.log('The solution is: ', rows);
  else
    console.log('Error while performing Query.');
});

connection.end();

Reference:

0 意見:

張貼留言