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.
Related
I am running PhpMyAdmin using Docker on my local machine with docker-compose. On remote server i'm using mysql user who can only access from localhost which is why i need ssh tunneling.
version: '3.1'
services:
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1
volumes:
- /usr/local/etc/php/php.ini:/php-make/upload.ini
- ./config.inc.php:/etc/phpmyadmin/config.inc.php
networks:
- host
networks:
host:
Since i'm using host network, docker container should be aware of local port forwarding (not really sure about this tho, but i couldn't find much information online on how host network actually works).
SSH config
host remote-server-name
HostName remote-server-ip
User user
IdentityFile path-to-ssh-key
ForwardAgent yes
LocalForward 3306 127.0.0.1:3306
After i do ssh to remote server there should be a tunnel on my local machine on port 3306 that is pointing to 3306 on remote server. Here is netstat -tulpn to confirm that:
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 17506/ssh
Server choice configuration for PhpMyAdmin (phpmyadmin.config.inc)
$cfg['Servers'][$i]['verbose'] = 'remote-server-name';
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['port'] = '3306';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['AllowNoPassword'] = false;
$cfg['LoginCookieValidity'] = 24*60*60*30;
After i choose remote-server-name in server choice i get the following message
mysqli::real_connect(): (HY000/2002): Connection refused
which means mysql user in not allowed to access from given ip address (in this case my public ip) and i guess that's because docker container is not using ssh tunneling from my local machine even if i'm using host network (which again i'm not sure what it actually does).
Anyone got any ideas what i'm doing wrong?
You need to change your service configuration to say
services:
phpmyadmin:
network_mode: host
# and not networks:
The configuration you have creates a Compose network that happens to be named host, but it's not "the host network".
You may be able to use a different approach to connect to the ssh tunnel; also see From inside of a Docker container, how do I connect to the localhost of the machine?. In particular, if you're on a MacOS or Windows host, host networking just doesn't work (you connect to the "host network" of a hidden Linux VM) and you'll need to use the special host.docker.internal host name instead of localhost. For this you don't need any special networks: or network_mode: option at all.
You might need to change the settings of the ssh tunnel listener for this to work. The 127.0.0.1:3306 setting binds to the host's localhost interface, but at least on native Linux the request will actually arrive from the docker0 interface. Setting the tunnel listener to listen on 0.0.0.0:3306 will solve this problem but also will allow others on the network to connect to the forwarded database. There's not a trivial solution for this.
I've set up a docker container running a mysql instance on a remote computer I have. In the past this hasn't been an issue but for some reason I can't get it to work now. I am unsure what the issue might be. I am using docker compose and I can't seem to connect through mysql work bench on a different computer even those the container is running. Here are my details:
docker-compose.yaml
version: '3.7'
services:
api:
image: api
restart: unless-stopped
container_name: api
build: ./node/
ports:
- 3008:3008
mysql:
image: mysql
restart: unless-stopped
container_name: mysql
environment:
MYSQL_DATABASE: pitapaldb
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
build: ./database/
ports:
- 3306:3306
networks:
default:
external:
name: my-net
database/Dockerfile
FROM mysql
COPY init.sql /docker-entrypoint-initdb.d
database/init.sql
CREATE DATABASE mydb;
USE mydb;
SET SQL_SAFE_UPDATES = 0;
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password';
flush privileges;
CREATE TABLE carts (
id int PRIMARY KEY AUTO_INCREMENT,
lat float,
lon float,
address varchar(255),
status boolean,
city_id int
);
container is definitely running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
784cf75183f4 mysql "docker-entrypoint.s…" 2 minutes ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
But when I try to connect via workbench I get 'unable to connect'. I've tried both username user and root with password password. The IP address I use definitely should work because I have other services operating from it with no issue:
#LoF10 Here is a quick list of things to check:
Can you connect within the docker network on the machine running the MySQL docker? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it --network my-net mysql:5.7 mysql -h mysql -uroot -ppassword
If not, there may be a problem with your MySQL config, MySQL data, or the initialization of the container. These are what #JorgeCampos is suggesting you verify. Since you are pulling directly from MySQL's Docker Hub entry, the config should be set properly to allow remote connections. If good, proceed. FYI, you will know you've connected successfully if you see mysql> on the terminal. To exit: \q.
Can you connect on exposed port on the localhost of the machine running the MySQL docker? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it --network host mysql:5.7 mysql -h 127.0.0.1 -uroot -ppassword
Make sure to use the IP used above and NOT localhost. This is b/c the MySQL client has special handling of the 'localhost' keyword by looking for mysqld locally. Using the 127.0.0.1 forces MySQL to connect via a proper socket connection. If you are not able to connect, then there is a problem with mapping the container's port to your host. If good, proceed.
Assuming both machines are on the same network and the machine that has MySQL Workbench also has docker, can you connect using the IP of the machine running MySQL container e.g. 10.0.0.4? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it mysql:5.7 mysql -h 10.0.0.4 -uroot -ppassword
If not, you may want to verify if you can:
Ping the 10.0.0.4 machine
If there are any firewall rules that prevent its proper exposure to the network. This happens commonly with Windows' default Firewall...
If on AWS, there are a number of reasons why you might not be able to reach if it has been properly assigned a Public Port e.g. Security Groups, Route Tables, Internet Gateway, etc.
Once you are able to proceed from 3 above, then you should be able to connect using MySQL Workbench as you've described.
Hope those help. Any more detailed recommendation will require you sharing more about your local networking setup (OS, Physical/Virtual, how you are determining IP's, etc).
I'm working with node, docker, mysql and sequelize and am trying to connect sequelize to mysql container running on docker. It will only connect through port 3306 despite me having changed the "ports" to 3308-3308. When i look up the running containers i get the following for the mysql database:
ticketgo_database_1 docker-entrypoint.sh mysqld Up 3306/tcp,
0.0.0.0:3308->3308/tcp
Which explains why it can only connect to port 3306 but I need to change the connection port from 3306 since that port is busy on my computer. How can i do that?
Mysql container:
database:
image: mysql
environment:
MYSQL_DATABASE: "ticketgo"
MYSQL_ROOT_PASSWORD: "pass"
volumes:
- "./sql:/docker-entrypoint-initdb.d"
ports:
- "3308:3308"
I guess your app is managed by docker-compose as well. There is no need to change which port is MySQL listening in its own container. Leave squelize connecting to databade:3306 and either do not specify port mapping in MySQL docker compose config or specify: 3308:3306 which means that port 3308 on host will be mapped to the 3306 container port. This does not mean that MySQL will listen to the 3308. It will be continuing listening in its container 3306, and a new port 3308 on the host will be mapped to it.
Only specify a port mapping if you need to access MySQL from outside docker-compose services (from another app on your host or a MySQL GUI for example)
In Port section, change second Port to any you want "3308:3309";
Or You can do this in she'll by -p 3309:3308
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.
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