mysql~ MySQLClient

MySQLClient is a client for MySQL database. Internally client uses go-sql-driver/mysql driver.

Constructor

new MySQLClient()

Methods

Connect(host, port, username, password) → {bool}

Connect connects to MySQL database using given credentials. If connection is successful, it returns true. If connection is unsuccessful, it returns false and error. The connection is closed after the function returns.
Parameters:
NameTypeDescription
hoststringThe host of the MySQL database.
portintThe port of the MySQL database.
usernamestringThe username to connect to the MySQL database.
passwordstringThe password to connect to the MySQL database.
Throws:
- The error encountered during connection attempt.
Type
error
Returns:
- The result of the connection attempt.
Type: 
bool
Example
let m = require('nuclei/mysql');
let c = m.MySQLClient();
let result = c.Connect('localhost', 3306, 'root', 'password');

ConnectWithDB(host, port, username, password, dbName) → {bool}

ConnectWithDB connects to MySQL database using given credentials and database name. If connection is successful, it returns true. If connection is unsuccessful, it returns false and error. The connection is closed after the function returns.
Parameters:
NameTypeDescription
hoststringThe host of the MySQL database.
portintThe port of the MySQL database.
usernamestringThe username to connect to the MySQL database.
passwordstringThe password to connect to the MySQL database.
dbNamestringThe name of the database to connect to.
Throws:
- The error encountered during connection attempt.
Type
error
Returns:
- The result of the connection attempt.
Type: 
bool
Example
let m = require('nuclei/mysql');
let c = m.MySQLClient();
let result = c.ConnectWithDB('localhost', 3306, 'root', 'password', 'mydb');

ExecuteQuery(host, port, username, password, dbName, query) → {string}

ExecuteQuery connects to Mysql database using given credentials and database name and executes a query on the db.
Parameters:
NameTypeDescription
hoststringThe host of the MySQL database.
portintThe port of the MySQL database.
usernamestringThe username to connect to the MySQL database.
passwordstringThe password to connect to the MySQL database.
dbNamestringThe name of the database to connect to.
querystringThe query to execute on the database.
Throws:
- The error encountered during query execution.
Type
error
Returns:
- The result of the query execution.
Type: 
string
Example
let m = require('nuclei/mysql');
let c = m.MySQLClient();
let result = c.ExecuteQuery('localhost', 3306, 'root', 'password', 'mydb', 'SELECT * FROM users');

IsMySQL(host, port) → {bool}

IsMySQL checks if the given host is running MySQL database. If the host is running MySQL database, it returns true. If the host is not running MySQL database, it returns false.
Parameters:
NameTypeDescription
hoststringThe host to check.
portintThe port to check.
Throws:
- The error encountered during the check.
Type
error
Returns:
- The result of the check.
Type: 
bool
Example
let m = require('nuclei/mysql');
let c = m.MySQLClient();
let result = c.IsMySQL('localhost', 3306);