Meteor and Mysql

Some days ago, I was talking with a coworker about build a Meteor application and connect with an existent mysql database, after some research about this, I didn’t found too much documentation explaining how to do that, just what is available on numtel packages readme, so I decided to share my first experiment with Meteor and MySql. Just to understand and see things working I decided to modify that simple ‘click’ application created after a “meteor create” command, so it’s pretty simple but, I’m sure that it will clarify something for you, even if you are trying it for the first time like me.

#1 – Configure the MySql server

I’m assuming here that you already have MySql server installed somewhere, in my case, I used a virtual box with ubuntu, to simulate connections on remote hosts. Another thing is about node and npm, I’ll not explain here how to install them, to avoid the mix of subjects, but if you need to do that, I recommend you use nvm to install and manage your node installations.   So, let’s go. On your server “where is you MySql server”, install the numtel mysql-live-select npm package, this package will generate events, every time that your select statements result set change.

Now edit the MySql my.cnf file and uncomment the following lines. Usually /etc/mysql/my.cnf

Restart your MySql server

or

And, create an account with replication privileges

 

#2 – Creating the MySql database

Connect on your MySql server and execute the following commands to create your new database and the table that we’ll use.

 

#3 – Connecting your Meteor application

Now, just create a new Meteor application

Remove autopublish and insecure packages

Add the numtel mysql package

Create a new file called settings.json on the root of your project, to keep your database configurations. Do not forget to pass –settings settings.json when you start your application, eg: meteor –settings settings.json

Ok, now your application is able to connect on MySql server,  let me show you my code after customize the default Meteor click application.

In the line 2, you can see an object called MysqlSubscription, this is what you’ll use for subscribe to a publication, nothing else is necessary like Meteor.subscribe. This line is subscribing on the publication at line 52, how you can see, this publication return the result of a SQL query. Subscription

Publication

This is a gif showing the application running. http://cl.ly/image/2F3A3d0R4431/meteor-mysql.gif

Conclusion

I hope this post could give you some idea of how to connect your Meteor app with MySql databases, there are other options like PostgreSQL and Neo4J available for Meteor, I’ll post about that in the future, but for while, I’ll try to do something more realistic with Meteor and MySql to share here.

Sources

https://github.com/allandequeiroz/meteor-mysql

https://github.com/felixge/node-mysql#connection-options

https://github.com/numtel/meteor-mysql

https://github.com/numtel/mysql-live-select

https://github.com/numtel/meteor-mysql-leaderboard/blob/master/leaderboard.html

https://github.com/creationix/nvm

 

Leave a Reply