Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Docker Container not connecting to local mysql

I have an issue connecting to mysql running in the local machine in my DockerFile i have mentioned

FROM php:7
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo mbstring pdo_mysql
WORKDIR /home
COPY . /home
RUN composer install --ignore-platform-reqs

CMD php artisan serve --host=0.0.0.0 --port=8081
EXPOSE 8081

and this in my .env configuration

DB_HOST=localhost
DB_DATABASE=databasename
DB_USERNAME=root
DB_PASSWORD=testpassword

I have very less clue about where it is failing. Do i need to install mysql for Docker container also?

like image 991
Divyesh pal Avatar asked Dec 13 '22 11:12

Divyesh pal


2 Answers

A much simpler solution (for Mac OSX & Docker for Windows) is to replace the host address from localhost to host.docker.internal

DB_HOST=host.docker.internal
DB_DATABASE=databasename
DB_USERNAME=root
DB_PASSWORD=testpassword

Basically the DNS namehost.docker.internal will resolves to the internal IP address used by the host.

NB: If you have changed your address to host.docker.internal but you still receive connection refused error, it’s most probably because MySQL is currently configured to only listen to the local network.

To resolve that, please update the value of the bind_address to 0.0.0.0 in your my.cnf configuration file.

like image 60
eliarms Avatar answered Dec 17 '22 23:12

eliarms


you are trying to connect to mysql in localhost, which is (surprisingly) the reference to the local host. since its a relative address, inside the container it is being resolved as the container own address, and no mysql is awaiting you there... so to solve it - just give it your real host ip instead of localhost or 127.0.0.1.

step 1 - fix .env file:

DB_HOST=<your_host_ip> #run `ifconfig` and look for your ip on `docker0` network
DB_DATABASE=databasename
DB_USERNAME=laravel_server #not root, since we are going to allow this user remote access.
DB_PASSWORD=testpassword

step 2 - create dedicated user:

open your mysql: mysql -u root -p, give your root password, and run the following:

CREATE USER 'laravel_server'@'%' IDENTIFIED BY 'testpassword';
GRANT ALL PRIVILEGES ON databasename.* TO 'laravel_server'@'%'; 
FLUSH PRIVILEGES;

we created the user and gave it the permissions.

step 3 - open mysql to remote access:

we have to make it listening on all interfaces and not just localhost and therefore we run:

sudo sed 's/.*bind-address.*/bind-address=0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf

(you will be prompted for password. this command is just replacing the line in mysql configuration file)

step 4 - updating:

  1. in the project directory: php artisan config:cache
  2. service mysql restart

then docker build and run a new container again. it should work for you.

like image 44
Efrat Levitan Avatar answered Dec 17 '22 22:12

Efrat Levitan