Laravel Connection with docker mysql - mysql

I am connecting my Laravel application with docker-composer.yml
I pull MySQL image and PHPMyAdmin image through CLI and connect MySQL and PHPMyAdmin in a container in the same network and I am trying to connect my Laravel application in localhost.
Laravel ENV:
'DB_CONNECTION=mysql'
'DB_HOST=mysql'
'DB_PORT=3306'
'DB_DATABASE=select_elect'
'DB_USERNAME=root
'DB_PASSWORD=root'
Container Info:
Error:
php_network_getaddresses: getaddrinfo failed: No such host is known.
Please guide me where I am doing a mistake.

If you want to connect not from a container, but from a host - then you need to add docker-compose for MySQL service first:
ports:
- "3306:3306"
e.g.
version: '3.7'
services:
db:
restart: unless-stopped
image: mysql:latest
ports:
- "3306:3306"
CLI:
docker run -p 3306:3306 mysql:latest
This will make the MySQL service and port accessible from outside the docker network.
But if you trying to connect from inside same docker network, check if your service has a name mysql, because you use values: 'DB_HOST=mysql'

Related

error 1045 when connecting to mysql on a docker container running at remote machine

I use dockerfile to create a mysql container on my remote machine like this:
version: '3'
services:
nginx:
image: nginx:latest
ports:
- 3000:3000
volumes:
- /root/nginx/html:/usr/share/nginx/html
- /root/nginx/nginx.conf:/etc/nginx/nginx.conf
privileged: true
mysql:
image: mysql:5.7.27
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=xxx
app:
image: app:latest
ports:
- 8686:8686
depends_on:
- mysql
everything runs well yesterday.This morning,I change some tables with SQLyog(It worked minutes before).and then I restart my back-end service and find it cannot connect to the database.
I doubt mysql has restriction on connection of same user so I restart my sqlyog and find it cannot connect to mysql too.
strangely, I can connect to the dabase on my remote docker.So the password is right.I check the port and don't know what else can i do.
app crashedsqlyog access denieddocker logs

FastAPI-mysql docker. mysql return (2003, "Can't connect to MySQL server on '127.0.0.1')

this is my docker-compose-yml file. first i tried to run the db service it work fined and docker running successfully. but when I run the app service, in the terminal it says connected to database but I get an error as (2003, "Can't connect to MySQL server on '127.0.0.1')
version: '4'
services:
app:
build: .
ports:
- "8000:8000"
env_file:
- .env
depends_on:
- db
links:
- db
db:
environment:
- MYSQL_ROOT_PASSWORD=root
-
image: mysql
ports:
- "3307:3307"
env_file:
- .env
enter image description here
What environment variable is the FastAPI app using to connect to the MySQL host?
If you're using docker-compose networking, services need to use the container name in order to reach each other (i.e. mysql://db:3307/db_name_here) not localhost.
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
More information on networking can be found in their docs here.
Also worth noting, since you're using links you can set aliases as well like so:
version: "3.9"
services:
web:
build: .
links:
- "db:database"
db:
image: postgres
Links Docs Source

Django container cannot find its mysql container

I have built my django docker with hostname server_default and with --network=server_default and ran mysql with same network(mysql container has ran before django server) when I check my mysql container everything is ok but when I run my django server it fails with error :
"Can't connect to MySQL server on 'server_default' ([Errno -2] Name or service not known)"
I attached to my server container and I couldn't connect to mysql container.
server_default is a bridge type.
my run commands :
sudo docker run -d -p 8000:8000 --network=server_default scotech-server
sudo docker run -d --network=server_default scotech-db
I couldn't do it with docker individually but with docker-compose.yml and 2 build context I could connect them together : it seems a bad solution :(
version: '3'
services:
scotech-db:
build:
context: ./scotech-mysql-docker
expose:
- 3306
web:
build:
context: ./server
depends_on:
- scotech-db
restart: on-failure
ports:
- "8002:8000"

"Host '192.XXX.XXX.X' is not allowed to connect to this MySQL server" error when attempting to access mysql Docker container from another container

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.

Docker django mysql.sock in different location

I'm trying to containerize my django file, and I keep running into the issue:(2006, ’Can\‘t connect to local MySQL server through socket \‘/var/run/mysqld/mysqld.sock\’ (2 “No such file or directory”)
I found out later mysql.sock is in this location:/tmp/mysql.sock instead of /var/run/mysqld/mysqld.sock, how do I change the location for docker to see /tmp/mysql.sock
Here is my docker-composr.yml:
version: '3'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: somepassword
adminer:
image: adminer
restart: always
ports:
- 8080:8080
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
I have followed the instructions on the mysql docker website to link mysql instance to a container
EDIT: I read another stack overflow similar to this, I changed my django code to 'HOST': '127.0.0.1' in DATABASES now I get : (2006, 'Can\'t connect to MySQL server on \'127.0.0.1\' (111 "Connection refused")')
Your host should be db. When using docker-compose, you address different servers by their service name.
So, in settings.py, you should have:
DATABASES = {
'default': {
'HOST': 'db',
...
}
}
If you want to connect to your containerized MySQL server both inside and outside of the container, you'll first need to make sure the port is mapped on the host machine:
services:
db:
image: mysql
ports:
- "3306:3306"
...
That will allow you to access MySQL using localhost or 127.0.0.1 directly on your host machine.
If you want to be able to run Django in both the web container and also on the host, you'll need to override the DATABASES setting depending upon the scenario. The web container will need to use a HOST value of db, whereas your local machine will need a value of localhost.