I've got two docker services, one running a simple node server and the other a mysql (mariadb actually) database server.
All instances of a socket file mentioned anywhere in /etc/mysql/ say
/var/run/mysqld/mysqld.sock
This will be important soon.
My node server is running some Sequelize code that is trying to connect to the MySQL server.
Whenever I try and connect via Sequelize, I get:
{"statusCode":500,"error":"Internal Server Error","message":"connect ENOENT /var/run/mysqld/mysqld.sock"}
However, if I log into the Node docker container I can successfully connect to MySQL on the other docker container using the mysql CLI client.
I think I understand that the mysql client is using a tcp connection, while Sequelize is using a socket connection. But, when Sequelize is throwing that error, it is showing the correct socket path, as far as I know. Here is my Sequelize config:
const options = {
host: "mysql",
dialect: "mysql",
dialectOptions: {
socketPath: "/var/run/mysqld/mysqld.sock"
}
};
let sequelize = new Sequelize("ibbr_dev", "devuser", "password", options);
The MySQL socket file is not available in your Node container, it is only available in the MySQL container as it is a file. Rather than setting up unix socket based connection, you should use a TCP connection (skipping the dialectOptions).
Related
I am experiencing problems with MySQL connection since making a few changes, and exhausted all suggestions, found here and the net as well as official troubleshooting docs, I come here in the hope of help.
The problem.
When trying to connect to MySQL DB via Node.JS (VSC)
Error: listen EADDRINUSE: address already in use :::3306
Node works on all Ports as requested, apart from any port that MySQL uses. Also MySQL connection fails if either the port number is edited or a new instance created.
A little history of the problem:
Worked perfectly with Node.JS app + MySQL DB Workbench 8.0 (MWB). Could connect and webpage populated with data from DB with no issues until i hooked it up to AWS Elastic Beanstalk, which I have since delete but problem persists even though I'm back at the beginning.
3306 is the port MySQL listens on. Your NodeJS app should not try to also listen on that port.
You should be specifying port 3306 here:
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "root12345",
});
Not here:
app.listen(PORT, () => {
console.log(`listening to port: ${PORT}`);
});
Your app should probably be listing on port 80 or 443 or whatever is appropriate for whatever your NodeJS app is trying to do. It may not need to listen on any port at all.
Also, you are using local MySQL here, you aren't making a connection to RDS at all, you are making a connection to the MySQL software running on the same server as the NodeJS app.
I've been using the official MySQL NPM package found here: https://www.npmjs.com/package/#mysql/xdevapi.
However, I can't seem to make a connection to the server. Here's the current error message I get:
Error: The server connection is not using the X Protocol.
Make sure you are connecting to the correct port and using a MySQL 5.7.12 (or higher) server intance.
Here is the code that generates that issue:
const db = mysqlx.getSession('root#localhost:33060/schemaname').then(session => {
console.log('SESSION STARTED!!');
});
This is just a test database without a password so I don't think the password is the issue. Also, I've made sure I'm using the right port and the MySQL version is 8.x.x so I don't think that is the issue. I created a database using the app Dbngin and I verified I could connect to the database by running the following command in my terminal: mysql -u root -h 127.0.0.1 --port=33060 -p which worked. I'm also running this on my Mac.
Update:
I've also tried passing a config object without much luck:
const config = {
user: 'root',
password: '',
host: 'localhost',
port: 33060,
schema: 'schemaname'
};
const db = mysqlx.getSession(config).then(session => {
console.log('SESSION STARTED!!');
});
Unfortunately, this code produces the same error above.
I tried it out myself and got the same error because you are using the wrong port. It could be you changed the default port, but the default port is: 3306 and not 33060 although I have to use port 33060 while my port is 3306.
X-protocal requires you to multiply your port by 10 I see here: https://dev.mysql.com/doc/mysql-port-reference/en/mysql-ports-reference-tables.html. So if your original port is 33060 I guess it should be 330600.
You could try this command SHOW VARIABLES LIKE 'mysqlx_port';
I'm finished with my project and I'm trying to deploy it to AWS. I have an ec2 instance as my webserver with the following configuration details:
NodeJS using port 5000
PM2 (keeping server alive at all times)
NGINX as web server reading from my build file
MySQL within ec2 instance as my database. (using port 3306)
My problem is I'm having trouble establishing a connection from my local machine to my AWS ec2 instance that has the MYSQL db inside of it. I opened up MYSQL workbench and I can connect to it just fine there but when I try and establish a connection string to the DB from node.js it gives me an error.
I was able to successfully connect to the DB within MYSQL workbench but how can I connect to it now from nodejs connection string?
What I already tried was the following:
1) In AWS security group opening up TCP Rule for all incoming traffic at port 5000
2) In AWS security group opening up MYSQL/Aurora Rule for all incoming traffic at port 3306
3) Granting all privileges on . to user and flushing and restarting mysql server.
Error it gives me in the console.
`{ Error: connect ECONNREFUSED 14.54.xxx.xx:3306
at Object._errnoException (util.js:1019:11)
at _exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1175:14)
--------------------
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '14.54.xxx.xxx',
port: 3306,
fatal: true }`
Here is my code trying to establish the connection:
```var mysql = require("mysql");
// Local Works just fine
// var connection = mysql.createConnection({
// host: "localhost",
// user: "root",
// password: "xxx",
// database: "devdb",
// charset: "utf8mb4"
// });
// Production Connection to AWS MYSQL instance (stuck on)
var connection = mysql.createConnection({
host: "14.54.xxx.xxx",
port: "3306",
user: "jordan",
password: "xxx",
database: "productiondb"
charset: "utf8mb4"
});
// Test the db connection
connection.connect(function(err) {
if (err) {
console.log(err);
} else {
console.log("Connected!");
}
});
module.exports = connection;
```
I expect to be able to successfully connect to the db instance from my NodeJS
Make sure again, I think your security groups have something wrong, maybe your server listening internally so It's happening. Go your EC2 security group and select Inbound and add rules as type=mysql, proto=tcp, port range=3306, source=0.0.0.0/0,::/0 (allow all)
There are a couple of reasons due to which this might be happening -
Configure MySQL database
#start MySQL server sudo service mysqld start
#run configuration sudo mysql_secure_installation
In the prompt, follow the following steps:
Enter current password for the root account: press Enter key
Set root password? Y
New password: yourpassword
Re-enter new password: yourpassword
Remove anonymous users? Y
Disallow root login remotely? n
Remove test database and access to it? Y
Reload privilege tables now? Y
If you are using RDS then you will have to provide NAT access to the VPC which holds your database. For more info please refer here
Actually I think I just figured it out.
So the default mysql configuration file has the ip binded to 127.0.0.1. However, I changed the binding to my ec2 public ip and also changed the default "mysql" to "jordan" and I saved the configuration file, restarted the mysql service and it now works.
Thank you for the suggestions I'll write this down in my documentation to check for in the future.
I am getting error SequelizeConnectionError: connect ETIMEDOUT when trying to connect to a remote mysql db with sequelize.
Connection can be established successfully when I try to connect to my local mysql db.
I'm using sequelize's default db connection code new Sequelize(...) contained within models/index.js, with the following config (filled up with the correct values):
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
I tried connecting to the remote db with a simple php script and it worked (so we can rule out issues on the remote db server side)
Any ideas?
For me, I have to:
1) Define exact information of my database at the local server (Xamp/ Mamp). It means I must have the existing database to connect, user name and password is a privileged account in your database.
2) Xamp/ Mamp must be online (of course). Default ports will be taken by this local server, so try a different port for mysql 8889 instead of 3306.
And this is what I tried:
sequelize = new Sequelize('wohui', 'root', 'root', {
dialect: 'mysql',
host: 'localhost',
port: 8889
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch((err) => {
console.log('Unable to connect to the database:', err);
});
I had the same problem, in my case it happened because I forgot to open the connection to mysql port 3306 in the inbound rules at my cloud provider
This error usually appears when the connection to the sql server is not established. Some things to take care of are :
Ensure mysql server is running in the host you are trying to connect to.
Ensure the host ip is correct.
Ensure that the port entered is correct.
Ensure that the firewall rules are defined correctly.
There could be couple of reasons for this, Listing out a few I have faced,
Remote root access not granted by the mysql server.
The configs are filtered by the environment, Which ideally is done by using the NODE_ENV variable, can you try running your server locally with prod config. In my case, I would do something like NODE_ENV=production node server.js. Assuming server.js is the start file. You can try logging the value's before new Sequelize(...), that might give a better idea as to what's going in.
I was facing the same issue. While in my case I gave a wrong port number (I didn't update my port number for the production database)
I had the same issue and my problem was in the ports. MySql had ports 3306 and in the config, i wrote port 3000 :D
Thanks for helped
I am trying to do the following in the NodeJS application which is running on MAC OSX: (Docker REST API's are used to interact with docker.
API Reference: https://docs.docker.com/engine/reference/api/docker_remote_api_v1.23/)
Create mysql docker container, mapping its port 3306 to one of the host port. This is the docker container configuration POST body:
{
'Env': ['MYSQL_ROOT_PASSWORD=root'],
'Image': 'test-mysql',
'ExposedPorts': {
'3306/tcp': {}
},
'HostConfig': {
'PortBindings': { '3306/tcp': [{}]}
}
Start this newly created mysql docker container.
Fetch the docker container port 3306 host mapping from the host config, by inspecting the container. (Assume the mapping maps mysql container port 3306 to port 32789 of the host)
Create a mysql connection using the following mysql npm module https://www.npmjs.com/package/mysql.
Connection config is:
{
host: dockerHostIp, // 192.168.99.100 -- docker host IP
port: portMappingFromStep3, //32789
user: mySQLUser,
password: mySQLUserPassword,
database: 'mysql'
}
Running all this step sequentially throws an error while setting up mysql connection. It produces following output:
{
[Error: connect ECONNREFUSED 192.168.99.100:32789]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.99.100',
port: 32800,
fatal: true
}
But, if I provide a timeout of around 7-8 seconds before establishing up the mysql connection, connection establishes successfully.
Not sure, what is happening over here. I am not able to understand, why without timeout mysql connection is failing. I think it is something related to port mapping. Port mapping is taking some time to setup and providing timeout of roughly 7seconds, is covering up that mapping time.
I am trying to set up an end to end test environment where multiple mysql containers will be running in parallel. Waiting time of 7-8 seconds for the connection setup is a big overhead.
Any help to unravel this mystery will be very helpful.
Thanks