Friday, 5 May 2017

Implementing a mysql database to socket chat

First off, mysql is completely new to me, so please hold that in mind, when reading my question.

I have finished building a socket chat, that runs on a node server, and now I want to connect a database to it, so I can store users and their conversations. I have read various tutorials and guides, but all assume you know just a little bit.

In command prompt, inside my project's folder, I have installed mysql, which is now located in the node-modules folder. Afterwards, I have manually added it as a dependency in my JSON file.

I have then set up a connection in the server.js file:

var mysql = require('mysql');
var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: ''
});

connection.connect(function(err) {
        if (err) {
                console.error('error connecting: ' + err.stack);
        return;
        } 

        console.log('connected as id ' + connection.threadId);
});

So where do I go from here? I thought I would be able to create a database using this command:mysql -u root -e 'CREATE DATABASE node' but that doesn't work.



via JonasSH

No comments:

Post a Comment