Skip to main content

My experience with NodeJS and MariaDB on Android through Termux

The first thing I did was check the official documentation, Getting Started With the Node.js Connector

I modified my server.js code and tested and first error I encountered was, "Error: (conn=20, no: 1698, SQLState: 28000) Access denied for user 'u0_a270'@'localhost'"

At some point in troubleshooting I also got this error, "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/data/data/com.termux/files/usr/tmp/mysqld.sock'"

Eventually I figured out that the problem was with the mariadb user that was created on install and how no password is required for mysql in Termux. So I just created a new user for my dev work as follows

MariaDB [db1] > CREATE USER 'user1'@localhost IDENTIFIED BY 'password1';

MariaDB [db1] > GRANT ALL PRIVILEGES ON *.* TO 'user1'@localhost IDENTIFIED BY 'password1';

Then my testing nodejs code as follows

const mariadb = require('mariadb');
const pool = mariadb.createPool({
     host: 'localhost',
     user:'user1',
     password: 'password1',
     database: 'db1',
     connectionLimit: 5
});

var http = require('http');

http.createServer(async function (req, res) {
  res.write('Device listing : ');
 
  rows = await queryDB();
 
  rows.forEach(function (row) {
  res.write(row.id + ' ' + row.name + '; '); });
 
  res.end();
}).listen(3000);

async function queryDB() {
  let conn;
  try {
conn = await pool.getConnection();
rows = await conn.query("SELECT * from devices");
  } catch (err) {
throw err;
  } finally {
if (conn) { conn.end(); return rows; }
  }
}

Man I was so happy to see this work after troubleshooting for a few hours. Hopefully this helps someone trying to get started with the same thing.

MariaDB example

Comments

Popular posts from this blog

Faith

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for faith. We say we want to meet God but we already do in some ways. We meet God in his creation. We meet God every time we meet someone. We meet God every time we meet the animals. We meet God every time we meet nature. God is all around. Faith is when we can see this. This is God's world. We are God's work. We are testimony of his greatness. Everyday that goes by I think about God. Everyday I thank God for his mercy. I take my problems to God. I pray to God. God is at the center of my life. Nothing happens without the will of God and therefore I would be wise to submit to the will of God. God is in the everyday moments. God loves when we love. What is the simplest definition of faith? I think it is found in the phrase, let go and let God. I asked my friend the same question. My friend says that at its simplest, faith is trust in what you cannot see or prove. But...

Flowers

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for the flowers. I bought a blue daze flowering plant for my mom when I went out to walk one day. It adds some colour to the nature in the yard. It is beautiful. I was told that blue flowers are rare. My friend says that blue is relatively rare in nature due to chemical and physical challenges in producing this color in living organisms. Nothing exists completely on its own. Plants depend on insects and other creatures, while those creatures depend on plants and their flowers. There is an interconnectedness in creation that reminds me that God is intentional. Flowers are beautiful but so is everything else if we really consider things with an open mind. What is beauty? What makes some things more beautiful than others? They say that beauty is in the eyes of the beholder. Beauty is subjective. My friend says that what I am talking about is the perceptual mind where meaning ...

Softness

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for softness. The gentleness of prayer. The calmness of the heart. The patience of the mind. The soft sounds of nature. The cool breezes of shade. The slow movement of sand in the hand. The kindness of strangers. The soothing voices of Quran. The warm embrace of the azan. I move through life with softness in my heart. I move through life with joy. I move through life wishing good for others. There is a peace that comes with being soft. The angels surround those who are soft. Soft like the winds of heaven. Soft like the clouds in the sky. Easy like Friday morning. May I never lose this softness that allows me to see beauty and goodness in everything and everyone. The world tries to harden me but I refuse. I walk upon the earth softly. I love without fear. My every breath is a prayer of gratitude. Let my heart remain like grains of sand in the hands of God that are softly he...