Home > OS >  Connect to mySQL in Laravel 9 installed using Docker
Connect to mySQL in Laravel 9 installed using Docker

Time:04-22

Recently, I installed Laravel 9 by following the steps below:

  1. First, I downloaded and installed Docker Desktop and after running it,

  2. I ran the following command in the directory where I wanted to install my Laravel project:

    curl -s "https://laravel.build/example-app" | bash

  3. After Laravel was successfully installed, I ran the following lines of code to activate sail:

    ./vendor/bin/sail up

    cd example-app

Now when I check Docker to see the services that are being run for example-app, I see:

example-app_mysql_1 mysql/mysql-server:8.0

But when I click on the "OPEN IN BROWSER" button to see a page similar to phpmyadmin, I get one of the following errors:

Error 1: This error is thrown when I enter the URL as http://localhost:3306:

localhost sent an invalid response. ERR_INVALID_HTTP_RESPONSE

Error 2: And this error is thrown when I enter the URL as http://localhost:3306:

localhost sent an invalid response. ERR_SSL_PROTOCOL_ERROR

As I need to create a new database with the same name defined in my .env file (shown below), I need to have access to mysql wizard. Besides, in the developing phase, sometimes, I may need to manipulate the database manually (with using migrations and other artisan commands).

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=example_app
DB_USERNAME=sail
DB_PASSWORD=password

How can I solve this?

CodePudding user response:

mysql is not available through the browser. You must either install phpmyadmin or connect to mysql as a wizard through softwares such as tableplus or sequel pro or etc.

But in any case you can install phpmyadmin by adding the following code to docker-compose.

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
        - 8080:80
    environment:
        MYSQL_USERNAME: "${DB_USERNAME}"
        MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
        PMA_HOST: mysql
    networks:
        - sail

Do not forget after adding this code use sail build --no-cache to rebuild services

  • Related