Database query doesn't return anything - mysql

I'm trying to connect my flutter app to the database using the mysql1 dart package. Everything seems right, I get no errors on the connection but I can't query my database. I'm not sure if my query reaches the database and is ignored or maybe I'm not reading the results right. I did as much debugging as my skills allow for and I'm out of ideas.
MySqlConnection.connect(ConnectionSettings(
host: 'localhost',
port: 3306,
user: 'root',
password: 'Haffenstadt!',
db: 'dbco'))
.then((conn) {
conn.query('SELECT * FROM User').then((results) {
print(results.isEmpty);
print(List.from(results));
});
conn.close();
});
My console:
flutter: true
flutter: []

Related

ER_ACCESS_DENIED_ERROR when connecting to MySQL using process.env.* values

I'm facing the following issue with https://github.com/sidorares/node-mysql2 (also tried with other libs):
await mysql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
})
throws Access denied for user 'user'#'ec2-44-201-80-189.compute-1.amazonaws.com'
while
await mysql.createConnection({
host: 'xxx.xxx.xxx.xxx',
port: '3306',
user: 'user',
password: 'password',
database: 'database'
})
works fine.
Please note that 'xxx.xxx.xxx.xxx' is the IP of the remote MySQL server, but in the first case it looks like the library is instead trying to connect to the NodeJS server (Heroku. But same is happening when I run the app locally, the IP doesn't match what I supplied). Just before the call, I console.log the process.env.* values read from env file and they are all correct.
Any clue on what I'm doing wrong?
thank you.

Can't add new command when connection is in closed state (express.js + mysql2 + mysql2/promise + clearDB + heruko)

i am facing thi error whenever i make a second call to the api i created using mysql2+expressjs+clearDB(on HERUKO)+heruko deployment server
this is my code ....
const [rows,fields]= await db.execute(MYQUERY,[myarguments])
i am calling the mysql server like this with asyn/await functionality which mysql2 provides
import mysql from 'mysql2/promise'
after that i create the the connection
let db = await mysql.createconnection({
host: 'localhost',
user: "root",
password: "asdzxcasdzxc123123",
database: "doctorapp"
})
export { db }
Any sollution to this error...???
i was facing this error in mysql2/promise
Reason for this error
The actual problem is with mysql2/promise .
when you create your connection with mysql database with mysql2 npm package
this thing is causing the error...
Sollution to this error
i solved this error by creating my connection with mysql.creatPool(yourCredentials)
creat your db config file like this
import mysql from 'mysql2'
let db = mysql.createPool({
host: 'Enter your host here',
user: "enter user name here",
password: "enter you password",
database: "enter the database name",
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
})
export { db }
Use of db in your contollers or router should be like this
const promise = db.promise()
const select_user_query=`
select *
from users
`
const [rows,fields]= await promise.execute(select_user_query)
return res.status(200).json({users:rows})
One thing to note===>
you have to create
const promise= db.promise()
in all the contollers ... so it will automatically handles all the required connnection release or other things required to make the connection working perfectly

Connect to RDS MySQL programatically (NodeJs, Java) - ER_ACCESS_DENIED_ERROR: Access denied for user

I am trying to connect to my RDS (AWS MySQL database) using NodeJs mysql library.
const mysql = require('mysql');
var connection;
connection = mysql.createConnection({
host: 'xxx.us-east-1.rds.amazonaws.com',
port: 3306,
user: "username",
password: "MyPassword",
database: "DbName",
});
I was also trying mysql2 and sequelize. All of them got me the same result:
{
error: ER_ACCESS_DENIED_ERROR: Access denied for user 'username'#'myhost,
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000'
}
However, this remote database is set to public and I am able to connect to it with mysql command:
mysql -u username -pMyPassowrd -h xxx.us-east-1.rds.amazonaws.com
I am also able to connect to it with MySQL Workbench.
Also, the problem is not in NodeJs, because I am able to connect to my local database:
connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'test',
});
In conclusion, my AWS database is publicly available and I can connect to it, just not using NodeJs for some reason.
I have not found a useful answer yet. Did anyone encounter this problem ?
UPDATE:
Trying to connect in Java with simple Connection returns the same result as well:
final Connection con = DriverManager.getConnection("jdbc:mysql:/xxx.us-east-1.rds.amazonaws.com:3306/DbName", "username", "MyPassword");
UPDATE 2:
I was missing one slash for Java code. I needed to use jdbc:mysql:// instead of jdbc:mysql:/. But NodeJs implementation is still one big mystery.
The answer was rather simple, just add an ssl option with true value
connection = mysql.createConnection({
host: 'xxx.us-east-1.rds.amazonaws.com',
port: 3306,
user: "username",
password: "MyPassword",
database: "DbName",
ssl: true //this does the trick
});

Sequelize ORM: How to connect to MYSQL on localhost, using the 'host' property?

I'm trying to connect to my local MYSQL(ran by XAMPP) through Sequelize:
const sequelize = new Sequelize(process.env.MYSQL_DB, 'root', '', {
host: process.env.CLEARDB_DATABASE_URL,
dialect: 'mysql',
logging: false
});
The process.env.CLEARDB_DATABASE_URL variable is set to "localhost", just like the documentation says(i'm using this specific variable because i want to deploy to Heroku later)
I'm getting the following error:
getaddrinfo ENOTFOUND localhost'; localhost';:3306
The process.env.MYSQL_DB variable is just the name of my database.
It all worked when i was using the "short way" to connect:
var sequelize = new Sequelize(process.env.MYSQL_DB , "root", "", {
dialect:'mysql',
logging: false
});
Can someone tell me what might be wrong with my setup?
EDIT: if anybody is interested, the problem was that i put a semicolon after the variable deceleration in my .env file.......

sequelize mysql ssl SequelizeConnectionError: ER_HANDSHAKE_ERROR: Bad handshake

I try to connect to an Azure MySQL DB with ssl.
new Sequelize('db', 'user', 'pw',
{
host: '...',
port: '3306',
dialect: 'mysql',
dialectOptions: {
insecureAuth: true,
ssl: {
ca: fs.readFileSync('ca.pem')
}
}
}
However I get ... `
SequelizeConnectionError: ER_HANDSHAKE_ERROR: Bad handshake
I have already tried:
https://github.com/sequelize/sequelize/issues/578
`
Ensure that your credentials are in the format of user#servername; this is required for Azure Database for MySQL.
A little late to the party but just came across this myself. This thread helped me solve it. If it's an option you could also just downgrade your version of Node but that's more of a bandaid than a fix.