@
@
@Twitt2019-09-28 20:55:30
Docker
@Twitt, 2019-09-28 20:55:30

Where is my db that I deployed in docker-compose actually?

I decided to raise MySQL in the docker, I decided that it was a matter of a couple of minutes.
There is such docker-compose:

version: '3.3'
services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: 'db'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'user'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3306:3306'
    expose:
      # Opens port 3306 on the container
      - '3306'
      # Where our data will be persisted
    volumes:
      - my-db:/var/lib/mysql
# Names our volume
volumes:
  my-db:

Made docker-compose up. After that, I decided to specify these configurations for the database in Laravel in the .env file:
DB_HOST=127.0.0.1
DB_PORT=3306
When I do php artisan migrate, it writes to me
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

But the strangest thing is that when I look in the database, there are no tables in the database at all. How does Laravel understand that the table is already there if I look through the same phpmyadmin and it's empty?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry, 2019-09-29
_

In the terminal, from the folder with the project, execute docker-compose exec db bash
This will take you to the container with the database.
Then execute mysql -uroot -p
This will take you to mysql.
Then execute SHOW DATABASES;
So you will see the list of bases in your mysql.
Then execute USE имя_нужной_тебе_базы;
So you will choose the base.
Then execute SHOW TABLES;
So you will see a list of tables that are in the database you selected.
This will help you understand that Lara really connected to mysql in docker.
Further, if you want to delete the database and create again.
Run DROP DATABASE имя_нужной_тебе_базы;
This will delete the database and all the data in it, it will be impossible to restore them, google this command and make sure that you understand exactly what you are doing.
Then run CREATE DATABASE имя_базы_которую_ты_удалил;
This will create a new empty base.
Now you can run the migrations.

D
Dmitry, 2019-09-28
@q2digger

The base of this container should have been saved in volume: /var/lib/docker/volumes/my-db/_data

M
metajiji, 2019-09-30
@metajiji

docker volume ls
By default, volumes are stored in
/var/lib/docker/volumes/

M
Mikhail Vasilyev, 2019-09-29
@vasilyevmn

The port is open on the wrong interface, the fastest way is to hang it on the localhost like this:

ports:
- "127.0.0.1:3306:3306"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question