Future<MySqlConnection> dbConnectSystem() async {
final conn = await MySqlConnection.connect(ConnectionSettings(
host: 'my_ip_adress',
port: 3306,
user: 'my_user_name',
password: 'my_password',
db: 'my_db'));
return conn;
}
I rented a hosting via natro and I'm trying to add and remove my data.
Related
I have a problem in Nodejs when trying to make a connection with my db via getConnection method. I declared my db variable via require which is a createPool promise(as shown below). when i try to make a connection with that pool via getConnection it skips that code block.
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
const db = pool.promise(); // this is exported
my connection method:
console.log('start');
const items = [this.idplace,this.idactivity,this.iduser,this.date,this.hour,this.party_size];
db.getConnection(function(err, conn) {
if(err) {
console.log('error when connecting to db:', err);
db.releaseConnection(conn);
throw err;
}
console.log('ok');
conn.execute(`INSERT INTO reservation(place,activity,customer,date,hour,party_size)VALUES(?,?,?,'?','?',?);`, items);
db.releaseConnection(conn);
});
console.log('end');
and the log is:
start
end
(no sign of the logs inside db.getConnection)
What do you think is the reason of that?
when i try to access workbench database i get this error
SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 60500
this is code
Future connectToDb() async {
// var rng = Random();
final conn = await MySqlConnection.connect(ConnectionSettings(
host: 'localhost',
port: 3306,
user: 'root',
db: 'sql_workbench',
password: '12345678a#',
),
).then((value) {
print("value");
});
}
I have my backend with this connection pool in mysql, now I try to migrate to dabase oracle can someone help me with configuration (to connect my api to oracle DB) thank you very much.
.env
APP_PORT=3000
DB_PORT=8889
DB_HOST=localhost
DB_USER=root
DB_PASS=root
MYSQL_DB=test
**dabatase.js**
const { createPool } = require('mysql');
const pool = createPool({
port: process.env.DB_PORT,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.MYSQL_DB,
connectionLimit: 10
});
module.exports = pool;
Node.js is installed in the following version on my Ubuntu 18/04 machine:
node -v
v14.16.0
The following modules are installed:
sudo npm list -g --depth=0
/usr/lib
├── mariadb#2.5.3
└── npm#6.14.11
The required package
The relevant code in my app file maria.js looks like this:
const mariadb = require('mariadb');
let conn = mariadb.createConnection({
host: 'localhost',
database: 'db1',
user:'dbuser1',
password: 'dbpwd',
port: 3306
});
conn.connect(function(err){
if(err){
console.log('Database connection error');
}else{
console.log('Database connection successful');
}
});
The required package 'mariadb' is satisfied and createConnection() does not return an error.
However, getting to the connect() statement, NodeJS returns the following:
conn.connect(function(err){
^
TypeError: conn.connect is not a function
at Object.<anonymous> (/home/juerg/bin/node/maria.js:12:6)
So looks like that createConnection() returns an empty JS object. Any other Connection class method won't work either, and I can't discover an error in this short code fragment.
mariadb.createConnection returns a promise so you would have to await for it to return the connection, like:
let conn = await mariadb.createConnection({
host: 'localhost',
database: 'db1',
user:'dbuser1',
password: 'dbpwd',
port: 3306
});
Also there might not be a need to call conn.connect() separately since it is already done in createConnection: Source Code
Or if you do not want to make your function async then you can do something like:
mariadb.createConnection({
host: 'localhost',
database: 'db1',
user:'dbuser1',
password: 'dbpwd',
port: 3306
}).then((conn) => {
// Do here what you want to do with MySQL connection...
});
As per the MariaDB connector documentation there are two available API's, Promise and Callback, Promise is the default API which you require and use in the following manner.
const mariadb = require("mariadb");
mariadb.createConnection({
host: 'localhost',
database: 'database',
user:'username',
password: 'password',
port: 3306
}).then(conn => {
// Execute a query/do whatever you need with the connection object here.
});
Alternatively, you can use the Callback API (which to note, is for compatibility with the mysql and mysql2 modules) by modifiyig the require statement and then connecting as follows:
const mariadb = require("mariadb/callback");
const conn = mariadb.createConnection({
host: 'localhost',
database: 'database',
user:'username',
password: 'password',
port: 3306
});
conn.connect(err => {
if (err) return console.log("Failed to connect");
console.log(`Successfully connected to mariadb server: ${conn.serverVersion()}`);
});
// Execute a query/do whatever you need with the connection object here.
Attempting to connect database from within the microservice, failing to the connect the database
Error: connect ECONNREFUSED 172.18.0.2:3306
service/index.js
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
console.log("Listening at 8080");
var mysql = require('mysql');
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Database Connected!");
});
docker-compose.yml
version: '3'
services:
database:
build:
./database
ports:
- "6603:3306"
image: "test-mysql"
container_name: "test-mysql"
service:
build:
./service
ports:
- "8080:8080"
depends_on:
- database
image: "test-nodejs"
container_name: "test-nodejs"
restart: on-failure
I've attempted connecting to database with different settings.
1) Without port
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
});
2) specified port 3306
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
port: 3306
});
3) specified port 6603
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password",
port: 6603
});
database/Dockerfile
FROM mysql
ENV MYSQL_DATABASE=test
ENV MYSQL_ROOT_PASSWORD=password
EXPOSE 6603:3306
COPY ./schema.sql /docker-entrypoint-initdb.d/
Basically how my node.js microservice can discover the database service?
Edit
I suspected that database wasn't ready by the time nodejs kicks in, so I added some delay before connecting to database and error changed
Updated Code
setTimeout(function(){
var mysql = require('mysql');
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Database Connected!");
});
}, 20 * 1000);
output
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Probably you are using a version of MySQL that doesnt support the login you are trying. Try with mysql v5.7:
docker run -d -p 6603:3306 --name mysql-container -e MYSQL_ROOT_PASSWORD=password mysql:5.7
You miss order:
First connect to database, then listen the port.
var http = require('http');
var mysql = require('mysql');
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
} );
con.connect(function(err) {
if (err) throw err;
console.log("Database Connected!");
});
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
console.log("Listening at 8080");
the database port does not get translated as it's within the container. just use 3306 in your app