Here is how I am trying to connect. But it shows:
Host 'host.docker.internal' is not allowed to connect
YAML:
environment:
- MYSQL_DSN=mysql:host=192.168.0.1;port=3307;dbname=dbname
Your MySQL user does not allow external connections.
Update your user to allow external connections using "GRANT" or create a new user using "CREATE USER": https://linuxize.com/post/how-to-create-mysql-user-accounts-and-grant-privileges/
To accept all connections, use % wildcard as a host part.
create mysql data as a container: named: mysql-data
create docker container for mysql server to use mysql DB created in step 1. this is your inside docker conatiner
mysql client should be there on the host machine
for above 2 steps use yaml file as:
services:
mysql-data:
image: image_path/mysql-data:latest
container_name: mysql-data
my-container-name:
image: mysql/mysql-server:5.6
container_name: my-container-name
env_file:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_USER=username
- MYSQL_PASSWORD=password
ports:
- 9999:3306
volumes_from:
- mysql-data
login to mysql DB using command:
mysql -h <your-host-ip> -P9999 -uusername -ppassword
Related
I have a project with a mysql database in a container. I use docker-compose to set my project up. And I want to run the mysql command to inspect te database.
So I did, and get:
docker-compose run --rm database mysql
Creating myproject_database_run ... done
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
However when I tried this it works:
docker exec -it myproject_database_1 mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Can anybody explain me this?
My docker-compose file:
version: "3.7"
services:
database:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "127.0.0.1:3306:3306"
env_file: .env
volumes:
- type: volume
source: db_data
target: /var/lib/mysql
- type: bind
source: ./my.cnf
target: /etc/my.cnf
read_only: true
volumes:
db_data:
testing_images:
docker-compose run creates a new container. That's perfectly normal, but if your mysql client is configured to connect via a Unix socket, the new container will have a new filesystem and won't be able to see the main database container's /var/run directory.
When you use docker-compose run, you need to specify a TCP connection, using the setup described in Networking in Compose in the Docker documentation. For example,
docker-compose run --rm database \
mysql -h database
Since you publish ports: out of the container, you should be able to reach it from the host, without Docker. The trick here is that the mysql command-line client interprets localhost as a magic term to use a Unix socket and not a normal host name, so you need to specifically use the IP address 127.0.0.1 instead.
# From the same host, without anything Docker-specific
mysql -h 127.0.0.1
Try adding MYSQL_ROOT_PASSWORD in the environment.
environment:
MYSQL_ROOT_PASSWORD: root
This is from one of my working compose file
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
db_mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
## map volume
volumes:
db_mysql:
## in network, we can define any name under networks:
networks:
app-network:
driver: bridge
FYI: Official MySQL docker image - Docker Hub
I'm trying to access my mysql database from workbench through SSH tunnel, mysql is in a container (working with docker-compose).
Here is my mysql container config in docker-compose :
mysql:
image: mysql:5.7
container_name: mysql
expose:
- 3306
ports:
- '3306:3306'
volumes:
- ./mysql:/var/lib/mysql
environment:
- "MYSQL_ROOT_PASSWORD=#####"
- "MYSQL_DATABASE=#####"
- "MYSQL_USER=#####"
- "MYSQL_PASSWORD=#####"
restart: always
But I'm getting a "Failed to Connect to MySQL" error, just like if my ports were not well reforwarding from SSH to MySQL container (and so, not hitting the good door).
Please note I did not allowed remote access from mysql, as I want to only keep SSH/localhost access possible.
Do you have any idea? Thanks
You are already exposing port 3306, so you should be able to access it from your system with below command -
mysql -u <mysql_user> -p -h localhost -p 3306
If not accessible, try if you are able to access it inside the docker session by logging in to mysql container in interactive mode.
I have the following docker compose:
version: '3.1'
services:
db:
image: mysql:5.5
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=some_root_pw
- MYSQL_DATABASE=some_db
- MYSQL_USER=db_user
- MYSQL_PASSWORD=user_pw
ports:
- 8306:3306
volumes:
- ./db:/var/lib/mysql/some_db
I am accessing the container with the command:
docker-compose exec db /bin/sh
Which drops me at the shell as expected. However, I am unable to access mysql with the information from the docker-compose file.
mysql -u root -p
Prompts for the password, then rejects for bad username or password. I have tried with both root and user. Is there a way I can troubleshoot what is happening with user creation?
NOTE: intentionally older version of MySQL.
I'm trying to connect a phpmyadmin-container to a mysql-container to view the databases. phpmyadmin web interface return error Cannot log in to the MySQL server with mysqli_real_connect(): (HY000/2002): No route to host.
What am I doing wrong? Thanks.
I use this operations:
docker-compose down && docker-compose up -d
Type to web browser http://localhost:8080
I use login as user root, password test
Get error described above
My docker-compose.yml
version: '2'
services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: test
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db
ports:
- "8080:80"
See official guide:
You need to specify some environment in order to link to an external mysql container:
Environment variables summary
PMA_ARBITRARY - when set to 1 connection to the arbitrary server will be allowed
PMA_HOST - define address/host name of the MySQL server
PMA_VERBOSE - define verbose name of the MySQL server
PMA_PORT - define port of the MySQL server
PMA_HOSTS - define comma separated list of address/host names of the MySQL servers
PMA_VERBOSES - define comma separated list of verbose names of the MySQL servers
PMA_PORTS - define comma separated list of ports of the MySQL servers
PMA_USER and PMA_PASSWORD - define username to use for config authentication method
PMA_ABSOLUTE_URI - define user-facing URI
For you, the mysql name is db, so you need to do next:
version: '2'
services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: test
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8080:80"
environment:
PMA_HOSTS: db
Also, --link depracated, just delete it, docker-compose will set network for you automatically to let the containers see each other with dns help.
I have a docker compose file which creates one container to run my app (Ruby on Rails) and another to run a mysql server.
version: "3"
volumes:
test-db:
external: false
services:
db:
image: mysql:5.7
env_file: .env
environment:
MYSQL_DATABASE: test_development
MYSQL_ROOT_PASSWORD: foobar
MYSQL_USER: root
MYSQL_PASSWORD: foobar
volumes:
- test-db:/var/lib/mysql
ports:
- "3306:3306"
app:
build:
./
image: test_app:latest
env_file: .env
command: "rails server -b 0.0.0.0"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
tty: true
stdin_open: true
I exec into the db container, and can access the mysql cli as expected.
$ docker exec -it test_db_1 mysql -pfoobar
Welcome to the MySQL monitor. Commands end with ; or \g.
However, I am met with the following when I attempt to access the server through the app container.
$ docker exec -it test_app_1 mysql -hdb -pfoobar
ERROR 1130 (HY000): Host '192.168.144.3' is not allowed to connect to this MySQL server
Why is the container running the mysql server unable to receive requests from the test app container?
When I run docker ps, I see the following:
COMMAND PORTS
"rails server -b 0.0…" 0.0.0.0:3000->3000/tcp
"docker-entrypoint.s…" 0.0.0.0:3306->3306/tcp, 33060/tcp
By default when you start MySQL container, it creates automatically the root user which you are using it to access the same container test_db_1 based on environment variables passed, this root user is granted/allowed to connect from localhost only.
But if you want to access MySQL from the other container, you should create a user that is granted to connect to the database from a remote host (either username#% or username#container-name) - as each container has a different IP inside the docker default network,
Note: you can do that by logging into MySQL in the container, and create a user, in your case it will be something like: grant all on <your-database>#`%` to <yourusername>#`%` identified by '<password>'
portsoption expose the ports to your host machine but I don't think it does between the same between containers. Try using expose: 3306 on the db configuration. By the way, I'd like to point out that even though you are using depends_on option that doesn't guarantee your database will be up when you start your application only that the db container will be ready so keep that in mind.