Docker Compose MySql and Python not working - mysql

I am running MySQL on docker compose and I am connecting to MySQL workbench works fine seems like when I connect from python says connection refused.
version: '3.1'
services:
mysql-dev:
image: mysql:8.0.2
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: mydb
ports:
- "3308:3306"
volumes:
- "./my.conf:/etc/mysql/conf.d/config-file.cnf"
- "./data:/var/lib/mysql:rw"
Here is my python code
import mysql
from mysql.connector import connect
# specify database configurations
config = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'password',
'database': 'mydb'
}
connect(**config)
Error :InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)

ALl I had to do is basically change the port to 3308 which solves my issue

Related

Cannot connect to MySQL (python MySql-Connector) when in docker container

I have a Python Flask based app that needs to connect to a database
version: "3.7"
networks:
localdev:
driver: bridge
services:
sysman-db:
image: mysql:8.0
container_name: sysman-db
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "4000:3306"
environment:
MYSQL_ROOT_PASSWORD: xxxxx
volumes:
- ./database/docker:/etc/mysql/conf.d
- ./database/schema.sql:/docker-entrypoint-initdb.d/dump0.sql
networks:
- localdev
sysman:
build:
context: .
dockerfile: Dockerfile.sysman
container_name: sysman
depends_on:
- sysman-db
ports:
- "3030:3030"
networks:
- localdev
links:
- lsm-db
The Flask application connects to the MySql, it has a retry method because of Docker bringing up sysman before the database initialisation completes.
Even when the database is up, I am still getting:
sysman | 2020-02-11 13:55:08 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:18 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:28 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:38 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
The database connect is:
dbConfig = { 'host' : '127.0.0.1', 'port' : 4000, 'user' : user,
'password' : password, 'database' : database }
connPool = mysql.connector.pooling.MySQLConnectionPool(pool_name='myPool',
pool_size=3,
pool_reset_session=True,
**dbConfig)
If I bring up the MySql container on its own then using mysql -u root -p -P 4000 I can connect.
Your sysman container should connect to the database container using sysman-db:3306 (not 4000). Remember that it is communication between 2 services so the published ports don't count.
You are trying to connect the localhost of the flask container which does not run mysqld. 4000 port counts for published port. 2 containers should talk via 3306 port
Try this:
dbConfig = { 'host' : 'sysman-db', 'port' : 3306, 'user' : user,
'password' : password, 'database' : database }

Not able to connect MYSQL from docker

I have downloaded a MYSQL docker image and am trying to connect to MYSQL using the host given in the IPaddress section of Docker inspect (say 172.17.0.2), port 3306 , username :root , and database: MYSQL.
I am trying to run an R script with the following Database connection:
con <- dbConnect(RMySQL::MySQL(),
dbname = “mysql”,
host=“172.17.0.2”,
port=3306,
username = “root”,
password = )
But I am getting an error stating the host does not exist. I even tried using various options of host like “localhost”. But am still failing to connect, with the error message:
Error in .local(drv, …) :
Failed to connect to database: Error: Can’t connect to MySQL server on ‘172.17.0.2’ (0)
Calls: dbConnect -> dbConnect -> .local -> .Call
Please help on this.
Trying to connect using localhost, meaning the localhost of R container, not the DB.
You can connect with the container IP directly but in case of Docker the IP changing frequently if you restart the DB the container the IP will be changed.
Better to use Docker-compose, which will keep both containers in the same network and R container will be able to connect using the name of the container.
Also, verify the logs of MySQL container is it up or you able to connect from MySQL client?
You can try something like
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: my_db
MYSQL_USER: my_user
MYSQL_PASSWORD: my_pass
web:
depends_on:
- db
image: r-base:latest
ports:
- "8000:80"
restart: always
volumes:
db_data: {}
So, in this case, your MySQL connection string will be
dbname = “mydb”, host=“db”, port=3306, username = “root”, password =root_password
Or the other option is to pass host IP to the R container instead of using container IP.
docker run -it --add-host=db:192.168.x.x r_base_image
So now the host will be
dbname = “mydb”, host=“db”
You need to go to Mysql configuration file (my.cnf) and bind the IP address.
something like :
[mysqld]
bind-address = xx.xx.xx.xx
if you dont know the ip , use 0.0.0.0
it means your host mysql can be gloabaly use for all ip addresses.

Connecting a NodeJS container to a MySQL database

Before marking this post as a duplicate note that I've already looked at the other related posts here and pretty much everywhere I could without finding a solution to my precise case. I just keep getting confused between docker-compose.yml versions and linking the ports to each other.
Here's the issue:
I've created two containers with Docker, one hosting a NodeJS server, one hosting a MySQL database.
My docker-compose.yml looks like this:
version: '3'
services:
server:
build: ./server/
ports:
- "8080:8080"
depends_on:
- db
db:
build: ./db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: dashboard_nodejs
MYSQL_USER: root
MYSQL_PASSWORD: password
client:
image: angular_app
build: ./front/
ports:
- "4242:80"
depends_on:
- server
When running docker-compose up, all of my apps are built and run without an issue until the server tries to connect to MySQL, exiting with :
Error: connect ECONNREFUSED 172.20.0.2:3306 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
The dockerfile for my database only contains
FROM mysql
COPY init_db.sql /docker-entrypoint-initdb.d/
and init_db.sql currently is
CREATE DATABASE IF NOT EXISTS dashboard_nodejs;
GRANT ALL PRIVILEGES ON dashboard_nodejs.*
TO 'root'#'%' IDENTIFIED BY 'password'
WITH GRANT OPTION;
Finally, my server attemps to establish the connection with the following parameters:
var con = mySql.createConnection({
host: "db",
user: "root",
password: "password",
database: "dashboard_nodejs"
});
I have no clue why the connection is refused by mysql even with (what looks to me like) the right credentials.
I apologize in advance if this particular case has already been discussed.
Add ports: "3306:3306" to the db service.

Can't start application within docker using linked mysql database

I'm using docker to run my nodejs app with few databases, and one of them is mysql.
I found mysql image on the docker hub and use it in my docker-compose.yml
app:
build: .
volumes:
- ./:/var/www/app/
working_dir: /var/www/app/
command: node app.js
ports:
- "3000:3000"
links:
- mongo
- elasticsearch
- mysql
mysql:
image: mysql
environment:
MYSQL_DATABASE: testdb
mongo:
image: mongo
elasticsearch:
image: elasticsearch
Everything builds and application uses mysql conenction config that seems like this:
mysql: {
host: 'localhost',
user: 'root',
password: '',
database: 'testdb'
}
Application trying to start and stops on the the mysql connection error.
Error: connect ECONNREFUSED 127.0.0.1:3306
I'm wondering if docker mysql image could be linked to the app container via compose file. Please explain how to link mysql container properly.
Thanks in advance!
My own answer
When you're using any type of linking other containers(external_link or link) you may set in your application config file the process environment variables according to the name of the container
process.env.<container_name>_PORT_<container_port>
SailsJS to Mongo Example:
I have a container with name mongo_dev which is connected in my docker compose file as
external_links:
- mongo_dev
so in my application config this environment variable used like this:
mongo: {
module : 'sails-mongo',
host : process.env.MONGO_DEV_PORT_27017_TCP_ADDR || 'localhost',
port : process.env.MONGO_DEV_PORT_27017_TCP_PORT || '27017',
user : null,
password : null,
database : 'database_name'
}
The linked mysql container will be your mysql host
mysql: { host: 'mysql', user: 'root', password: '', database: 'testdb' }
You will see all you linked hosts are named in /etc/hosts like:
172.17.0.2 composetest_mysql_1 fffb39e63df6
172.17.0.2 mysql fffb39e63df6 composetest_mysql_1
172.17.0.2 mysql_1 fffb39e63df6 composetest_mysql_1
Use Connection String as Follows:
mysql://username:password#mysql:3306/demo

Docker mysql host not privileged

I am trying to configure a nodejs container to connect to a mysql database.
My code looks like this:
var pool = mysql.createPool({
host : 'mysql',
port : '3306',
user : 'root',
password : '...',
database : 'general',
connectionLimit : 50,
queueLimit : 5000
});
I am using the standard mysql container.
I am using fig for starting the containers. fig.yml looks something like:
node:
build: node
ports:
- "9000:9000"
- "9001:9001"
links:
- mysql:mysql
command: node server/Main.js
mysql:
image: mysql
volumes:
- /data/test:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ...
Every time i try to connect i get the following error:
failed to connect to database: Error: ER_HOST_NOT_PRIVILEGED: Host '172.17.0.142' is not allowed to connect to this MySQL server
Any idea what I am doing wrong? I have played around with wordpress and it seems to connect to the same mysql db without any problems.
Thx!
Edit
So, I have found the answer in the end. The problem was indeed a privilege issue. I ran the following command:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password'
And I was able to access the database.
I use the drakedroid/mysql-with-data (https://registry.hub.docker.com/u/drakedroid/mysql-with-data/) image. It extends the default image, but adds more functionality.