I have simple phpmyadmin, mysql containers.
I create new folder with backend in nodejs. At now, I just can't connect to database through backend.
All three containers (phpmyadmin, mysql, api) works and launch. But container api don't connect with database.
This is my main db.js file for connection, which is always throw error.
const mysql = require("mysql");
const db = mysql.createConnection({
host: "localhost",
user: "db_user",
password: "db_user_pass",
database: "any-game",
port: 8899,
});
db.connect((err) => {
if (err) {
throw new Error("Something went wrong!"); // Get error always in this line because can't connect to db
}
});
module.exports = db;
Url for phpmyadmin is localhost:8899. Phpmyadmin work as I expect. Without any error. I can log in to db through login and password in docker-compose. But can't connect with backend.
And docker-compose.yml file
version: "3"
services:
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin1
environment:
- PMA_ARBITRARY=1
- PMA_HOST=db
- PMA_PORT=3306
restart: always
links:
- db
ports:
- 8899:80
depends_on:
- db
api:
build: ./api
container_name: api1
command: npm run start
restart: unless-stopped
ports:
- "3005:3005"
environment:
- PORT=3005
depends_on:
- phpmyadmin
db:
image: mysql
container_name: db
environment:
- MYSQL_ROOT_PASSWORD=my_secret_password
- MYSQL_DATABASE=guess-game
- MYSQL_USER=db_user
- MYSQL_PASSWORD=db_user_pass
restart: always
ports:
- 6033:3306
I tried to write like this:
const db = mysql.createConnection({
host: "localhost",
user: "db_user",
password: "db_user_pass",
database: "any-game",
});
Like this
const db = mysql.createConnection({
host: "localhost",
user: "db_user",
password: "db_user_pass",
database: "any-game",
port: 80,
});
Or
const db = mysql.createConnection({
host: "localhost",
user: "db_user",
password: "db_user_pass",
database: "any-game",
port: 6033, // DB port
});
Even
const db = mysql.createConnection({
host: "127.0.0.1",
user: "db_user",
password: "db_user_pass",
database: "any-game",
});
Please help
