Docker mysql host not privileged - mysql

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.

Related

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.

Docker Compose - Creating and connecting to mysql container

Having a hard time trying to get to grips with mysql in docker. I have got the container running with docker-compose, but I am not able to connect to the database via any tools such as phpmyadmin, workbench or tableplus.
I have connected directly to the running container and run
mysql -uroot -p
and entered the root password which I have passed, but this fails with this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Here is my docker-compose.yml file:
version: '3'
services:
db:
image: mysql
restart: always
environment:
MYSQL_DATABASE: quotes
MYSQL_USER: quotes
MYSQL_PASSWORD: P#KhzKZp)56sU8n+
MYSQL_ROOT_PASSWORD: +\VrLG*<t5sq[\\shR29u#n~A3^Jp*
ports:
- '3306:3306'
volumes:
- /private/mdbdata/quotes:/etc/mysql/conf.d
expose:
- '3306'
Been on this for days... hope someone can help!
I think your container is looking for a MySQL server on 'localhost', which WILL NOT WORK. 'localhost' to a container is the container itself - NOT the host machine it's running on.
You should be able to access the MySQL server using the host machine IP address.
Finally solved this one ... I had to remove all special characters from the password strings.
I tired adding single and double quotes are around the strings to see if that would allow the special characters, but that still failed. Passwords needed to be alphanumeric.
Have you tried the solution provided here? Basically, if you had a container running previously, you have to explicitly purge it since docker keeps track of root passwds (via volumes) from previously instantiated containers. This happens with a lot of tools, it's not unique to MySQL containers. A simple docker-compose rm -v should suffice, afterwards bring up your container. This basically deletes the old volume from the disk, removing all data from previous container instantiation.
You can also call a docker ps, find your container and execute docker rm -v <CONTAINER_NAME>. Bring up your container afterwards.

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 connect to mysql container from localhost

I'm trying to set up a MySQL container for developing.
So I used docker-compose to set it up.
The container and the mysql looks OK. The thing is that I want to connect to it from a DBeaver client and I can't find how I do it.
Here is my docker-compose.yml:
version: '2'
services:
db:
image: 'mysql:5.7'
volumes:
- '~/dev/dbs-data/mysql:/var/lib/mysql'
restart: 'always'
expose:
- '3306'
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: 'pass'
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'pass'
When I try to connect it from DBeaver I get:
java.sql.SQLException: null, message from server:
"Host '172.18.0.1' is not allowed to connect to this MySQL server"
UPDATE
I'm not trying to connect using the IP 172.18.0.1. I tried to connect using localhost:3306, 127.0.0.1:3306 and with the sub IP docker gave it 0.0.0.0:3306
UPDATE
After having success connecting on my Mac, I tried again with DBeaver on my linux and again:
Tried to connect with other tool, mysql workbench:
As you can see in the official image documention :
MYSQL_ROOT_HOST : By default, MySQL creates the 'root'#'localhost' account. This account can only be connected to from inside the container, requiring the use of the docker exec command as noted under Connect to MySQL from the MySQL Command Line Client. To allow connections from other hosts, set this environment variable. As an example, the value "172.17.0.1", which is the default Docker gateway IP, will allow connections from the Docker host machine.
So you have to set the MYSQL_ROOT_HOST variable with the address 172.18.0.1 that you can see in the error message.
On Docker, run this command to create a MySql container and it will expose the ports to the outside world of docker.
docker run --name <mysql-container-name> -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<root-password> -e MYSQL_USER=root -e MYSQL_PASSWORD=<user-password> -d mysql:latest
Few points to note:
You may see below error when trying to connect with DBeaver:
Public Key Retrieval is not allowed
Solution: When creating a new connection on DBeaver, go to Driver Properties look for allowPublicKeyRetrievel and set it to TRUE. Also, if needed set useSSL to FALSE.
Test your connection from DBeaver or any other clients, and it should be working.
I am new to docker and was experiencing the same issue in Linux, it was an issue with the addresses allowed to accept connection; here is what worked out for me:
Find the MySql configuration file named mysqld.cnf
It would be: /etc/mysql/mysql.conf.d/mysqld.cnfOr if you have your own configuration file.
Edit the bind-address key in it. Set it as: bind-address = 0.0.0.0
This will allow to accept connections from any IP address Restart docker-compose by $ docker-compose down$ docker-compose up
Wait for MySQL to start, it should work fine now.

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