用户“root”Mysql 容器的访问被拒绝

时间:2021-03-30 13:13:44

标签: mysql laravel docker containers

我正在尝试 dockerize 一个 laravel 项目 我的 MySQL 容器的一个小问题。 我无法使用 root 用户或简单用户连接到我的数据库的容器。

我只使用了 docker-compose build && docker-compose up -d

docker exec -it ica-backoffice_attendize_db_1 /bin/bash

mysql -u root -p

Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

这是我的docker日志:docker logs ica-backoffice_attendize_db_1

2021-03-30 12:20:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
2021-03-30 12:20:44+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-03-30 12:20:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
2021-03-30T12:20:44.525563Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-03-30T12:20:44.527413Z 0 [Note] mysqld (mysqld 5.7.33) starting as process 1 ...
2021-03-30T12:20:44.532377Z 0 [Note] InnoDB: PUNCH HOLE support available
2021-03-30T12:20:44.532396Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2021-03-30T12:20:44.532402Z 0 [Note] InnoDB: Uses event mutexes
2021-03-30T12:20:44.532407Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2021-03-30T12:20:44.532425Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2021-03-30T12:20:44.532430Z 0 [Note] InnoDB: Using Linux native AIO
2021-03-30T12:20:44.532739Z 0 [Note] InnoDB: Number of pools: 1
2021-03-30T12:20:44.532879Z 0 [Note] InnoDB: Using CPU crc32 instructions
2021-03-30T12:20:44.535236Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2021-03-30T12:20:44.546880Z 0 [Note] InnoDB: Completed initialization of buffer pool
2021-03-30T12:20:44.549449Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2021-03-30T12:20:44.561014Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2021-03-30T12:20:44.562673Z 0 [Note] InnoDB: Log scan progressed past the checkpoint lsn 12665801
2021-03-30T12:20:44.562685Z 0 [Note] InnoDB: Doing recovery: scanned up to log sequence number 12665810
2021-03-30T12:20:44.562690Z 0 [Note] InnoDB: Database was not shutdown normally!
2021-03-30T12:20:44.562695Z 0 [Note] InnoDB: Starting crash recovery.
2021-03-30T12:20:44.713371Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2021-03-30T12:20:44.713417Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2021-03-30T12:20:44.713562Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2021-03-30T12:20:45.454122Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2021-03-30T12:20:45.456067Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2021-03-30T12:20:45.456099Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2021-03-30T12:20:45.456761Z 0 [Note] InnoDB: Waiting for purge to start
2021-03-30T12:20:45.507075Z 0 [Note] InnoDB: 5.7.33 started; log sequence number 12665810
2021-03-30T12:20:45.507543Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2021-03-30T12:20:45.507941Z 0 [Note] Plugin 'FEDERATED' is disabled.
2021-03-30T12:20:45.511918Z 0 [Note] InnoDB: Buffer pool(s) load completed at 210330 12:20:45
2021-03-30T12:20:45.524384Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2021-03-30T12:20:45.524418Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2021-03-30T12:20:45.526363Z 0 [Warning] CA certificate ca.pem is self signed.
2021-03-30T12:20:45.526435Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2021-03-30T12:20:45.527538Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2021-03-30T12:20:45.527717Z 0 [Note] IPv6 is available.
2021-03-30T12:20:45.527762Z 0 [Note]   - '::' resolves to '::';
2021-03-30T12:20:45.527810Z 0 [Note] Server socket created on IP: '::'.
2021-03-30T12:20:45.578895Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2021-03-30T12:20:45.602259Z 0 [Note] Event Scheduler: Loaded 0 events
2021-03-30T12:20:45.602848Z 0 [Note] mysqld: ready for connections.
Version: '5.7.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
2021-03-30T12:20:56.582691Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2021-03-30T12:21:22.098439Z 3 [Note] Access denied for user 'dhia'@'localhost' (using password: YES)
2021-03-30T12:21:26.702866Z 4 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2021-03-30T12:21:56.174284Z 5 [Note] Access denied for user 'root'@'localhost' (using password: YES)

.env 文件:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=preprodica
DB_USERNAME=root
DB_PASSWORD=Str0ngDBP@ssw0rd

Docker 组合:

services:

  attendize_app:
    build:
      context: . # The path of the Dockerfile
    volumes:
      - .:/usr/src/app
    depends_on:
      - attendize_db
    working_dir: /usr/src/app
    environment:
      SERVICE_NAME: attendize-app
      DB_CONNECTION: mysql
      DB_DRIVER: mysql
      DB_HOST: attendize_db
      DB_PORT: 3306
    networks:
      - app_network

  attendize_nginx:
    build:
      context: ./docker-nginx/. # The path of the Dockerfile
    volumes:
    - "./public:/usr/src/app/public"
    environment:
      NGINX_FPM_HOST: attendize_app
      NGINX_ROOT: /usr/src/app/public
    ports:
      - "8000:80"
    depends_on:
      - attendize_app
    networks:
      - app_network

  attendize_db:
    image: mysql:5
    restart: always
    
    environment:
      MYSQL_DATABASE: "preprodica"
      MYSQL_ROOT_PASSWORD: "Str0ngDBP@ssw0rd"
      MYSQL_USER: "dhia"
      MYSQL_PASSWORD: "dhia"
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"      
    ports:
      - "3306:3306"
    volumes:
      - "./storage/db-data:/var/lib/mysql"
    networks:
      - app_network
networks:
  app_network:
    driver: bridge
 

2 个答案:

答案 0 :(得分:0)

Docker-compose 做了额外的工作来在运行之间保留卷(从而保留数据库);您可能想尝试 docker-compose rm -v 删除所有内容并尝试重新启动它。命令 docker-compose rm -v 反映在容器中,但不在映射的卷中。因此,以下命令将删除容器,并且由于它不会删除卷,因此创建新用户的命令文件。

$ docker-compose rm -v
Going to remove backendphp_qgPhpMyAdmin_1, backendphp_qgMySQLServer_1, backendphp_qgMysqlData_1
Are you sure? [yN] y

Removing backendphp_qgMysqlData_1...
Removing backendphp_qgMySQLServer_1...
Removing backendphp_qgPhpMyAdmin_1...

此时,唯一正确的方法是删除您映射 mysql 数据的目录。

$ sudo rm -rf mysql-data

只有在那之后,该命令才创建了一个正确设置用户的新数据库! 您也可以查看此链接 https://www.programmersought.com/article/16512825562/

答案 1 :(得分:0)

我固定使用:

sudo rm -r db-data/

docker-compose up -d

docker-compose down -v

祝你好运!

相关问题