MariaDB连接被拒绝(NGINX、MariaDB、php-fpm)SQLSTATE[HY000][2002]连接被拒绝
向MariaDB发送查询时,获取SQLSTATE[HY000][2002]连接被拒绝 下面是我的docker compose构建的链接 --重要信息:这是不在github存储库中的data.sql文件太大,但它位于我的构建中: 这是我的文档树/布局:MariaDB连接被拒绝(NGINX、MariaDB、php-fpm)SQLSTATE[HY000][2002]连接被拒绝,nginx,docker-compose,mariadb,fpm,Nginx,Docker Compose,Mariadb,Fpm,向MariaDB发送查询时,获取SQLSTATE[HY000][2002]连接被拒绝 下面是我的docker compose构建的链接 --重要信息:这是不在github存储库中的data.sql文件太大,但它位于我的构建中: 这是我的文档树/布局: myapp ├── docker │ ├── app │ │ └── Dockerfile │ ├── db │ │ ├── data.sql │ │ └── Dockerfile │ ├── docker-co
myapp
├── docker
│ ├── app
│ │ └── Dockerfile
│ ├── db
│ │ ├── data.sql
│ │ └── Dockerfile
│ ├── docker-compose.yml
│ └── nginx
│ ├── conf.d
│ │ └── default.conf
│ ├── Dockerfile
│ ├── nginx.conf
│ └── sites
│ └── default.conf
└── src
├── html
└── index.php
下面是我的index.php,其中包含以下查询:
<?php
$value = "World";
try {
$db = new PDO('mysql:host=db;dbname=mydb;charset=utf8mb4', 'myuser', 'secret');
$databaseTest = ($db->query('SELECT * FROM dockerSample'))-
>fetchAll(PDO::FETCH_OBJ);
} catch (Exception $e) {
print $e->getMessage() . "\n";
}
print "OK\n";
?>
<html>
<body>
<h1>Hello, <?= $value ?>!</h1>
<?php foreach($databaseTest as $row): ?>
<p>Hello, <?= $row->name ?></p>
<?php endforeach; ?>
</body>
</html>
我不明白发生了什么事。我似乎正在联系MariaDB,但它拒绝了我的连接,尽管我检查了端口、用户和密码,并修剪了卷,删除了容器,删除了图像,然后从头开始重新构建它们
docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23ae6d48e413 docker_nginx "nginx" About an hour ago Up 13 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 8080/tcp docker_nginx_1
d84c3b348266 docker_app "docker-php-entrypoi…" About an hour ago Up 13 minutes 9000/tcp docker_app_1
5ba1e7d36f91 docker_db "docker-entrypoint.s…" About an hour ago Up 13 minutes 3306/tcp docker_db_1
生成日志:
Building db
Step 1/3 : FROM mariadb:latest
---> 2f11cf2ec189
Step 2/3 : CMD ["mysqld"]
---> Using cache
---> 9f5dd0c62210
Step 3/3 : EXPOSE 3306
---> Using cache
---> fe82aa37cc8d
Successfully built fe82aa37cc8d
Successfully tagged docker_db:latest
Building app
Step 1/4 : FROM php:fpm-alpine
---> aac18a94faf7
Step 2/4 : RUN docker-php-ext-install pdo_mysql
---> Using cache
---> 886e00ce8edc
Step 3/4 : CMD ["php-fpm"]
---> Using cache
---> 90bf05e46cc5
Step 4/4 : EXPOSE 9000
---> Using cache
---> 2e9cb9be36b7
Successfully built 2e9cb9be36b7
Successfully tagged docker_app:latest
Building nginx
Step 1/3 : FROM nginx:alpine
---> a624d888d69f
Step 2/3 : CMD ["nginx"]
---> Using cache
---> 767651bd1614
Step 3/3 : EXPOSE 8080 443
---> Using cache
---> 6fc1df24d4e4
Successfully built 6fc1df24d4e4
Successfully tagged docker_nginx:latest
Starting docker_db_1 ...
Starting docker_db_1 ... done
Starting docker_app_1 ...
Starting docker_app_1 ... done
Starting docker_nginx_1 ...
Starting docker_nginx_1 ... done
Attaching to docker_db_1, docker_app_1, docker_nginx_1
db_1 | 2020-01-06 18:17:42+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.11+maria~bionic started.
app_1 | [06-Jan-2020 18:17:44] NOTICE: fpm is running, pid 1
db_1 | 2020-01-06 18:17:43+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1 | 2020-01-06 18:17:43+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.11+maria~bionic started.
app_1 | [06-Jan-2020 18:17:44] NOTICE: ready to handle connections
db_1 | 2020-01-06 18:17:43 0 [Note] mysqld (mysqld 10.4.11-MariaDB-1:10.4.11+maria~bionic) starting as process 1 ...
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Uses event mutexes
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Number of pools: 1
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1 | 2020-01-06 18:17:43 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: 10.4.11 started; log sequence number 6837896; transaction id 7512
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2020-01-06 18:17:44 0 [Note] Plugin 'FEEDBACK' is disabled.
db_1 | 2020-01-06 18:17:44 0 [Note] Server socket created on IP: '::'.
db_1 | 2020-01-06 18:17:44 0 [Warning] 'user' entry 'root@5ba1e7d36f91' ignored in --skip-name-resolve mode.
db_1 | 2020-01-06 18:17:44 0 [Warning] 'user' entry '@5ba1e7d36f91' ignored in --skip-name-resolve mode.
db_1 | 2020-01-06 18:17:44 0 [Warning] 'proxies_priv' entry '@% root@5ba1e7d36f91' ignored in --skip-name-resolve mode.
db_1 | 2020-01-06 18:17:44 0 [Note] Reading of all Master_info entries succeeded
db_1 | 2020-01-06 18:17:44 0 [Note] Added new Master_info '' to hash table
db_1 | 2020-01-06 18:17:44 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.4.11-MariaDB-1:10.4.11+maria~bionic' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
db_1 | 2020-01-06 18:17:45 0 [Note] InnoDB: Buffer pool(s) load completed at 200106 18:17:45
加
对于app,这是有道理的,但是现在我得到了SQLSTATE[HY000][2006]MySQL服务器已经离开了。请指定您正在执行哪些操作以显示此错误。我在.yml文件中向app添加了dependens\u on:-db,并在本地创建了一个新的buildLaunched docker容器。应用程序运行成功。您的日志与我的不同。比较入口点不适合您
Building db
Step 1/3 : FROM mariadb:latest
---> 2f11cf2ec189
Step 2/3 : CMD ["mysqld"]
---> Using cache
---> 9f5dd0c62210
Step 3/3 : EXPOSE 3306
---> Using cache
---> fe82aa37cc8d
Successfully built fe82aa37cc8d
Successfully tagged docker_db:latest
Building app
Step 1/4 : FROM php:fpm-alpine
---> aac18a94faf7
Step 2/4 : RUN docker-php-ext-install pdo_mysql
---> Using cache
---> 886e00ce8edc
Step 3/4 : CMD ["php-fpm"]
---> Using cache
---> 90bf05e46cc5
Step 4/4 : EXPOSE 9000
---> Using cache
---> 2e9cb9be36b7
Successfully built 2e9cb9be36b7
Successfully tagged docker_app:latest
Building nginx
Step 1/3 : FROM nginx:alpine
---> a624d888d69f
Step 2/3 : CMD ["nginx"]
---> Using cache
---> 767651bd1614
Step 3/3 : EXPOSE 8080 443
---> Using cache
---> 6fc1df24d4e4
Successfully built 6fc1df24d4e4
Successfully tagged docker_nginx:latest
Starting docker_db_1 ...
Starting docker_db_1 ... done
Starting docker_app_1 ...
Starting docker_app_1 ... done
Starting docker_nginx_1 ...
Starting docker_nginx_1 ... done
Attaching to docker_db_1, docker_app_1, docker_nginx_1
db_1 | 2020-01-06 18:17:42+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.11+maria~bionic started.
app_1 | [06-Jan-2020 18:17:44] NOTICE: fpm is running, pid 1
db_1 | 2020-01-06 18:17:43+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1 | 2020-01-06 18:17:43+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.11+maria~bionic started.
app_1 | [06-Jan-2020 18:17:44] NOTICE: ready to handle connections
db_1 | 2020-01-06 18:17:43 0 [Note] mysqld (mysqld 10.4.11-MariaDB-1:10.4.11+maria~bionic) starting as process 1 ...
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Uses event mutexes
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Number of pools: 1
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1 | 2020-01-06 18:17:43 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: 10.4.11 started; log sequence number 6837896; transaction id 7512
db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2020-01-06 18:17:44 0 [Note] Plugin 'FEEDBACK' is disabled.
db_1 | 2020-01-06 18:17:44 0 [Note] Server socket created on IP: '::'.
db_1 | 2020-01-06 18:17:44 0 [Warning] 'user' entry 'root@5ba1e7d36f91' ignored in --skip-name-resolve mode.
db_1 | 2020-01-06 18:17:44 0 [Warning] 'user' entry '@5ba1e7d36f91' ignored in --skip-name-resolve mode.
db_1 | 2020-01-06 18:17:44 0 [Warning] 'proxies_priv' entry '@% root@5ba1e7d36f91' ignored in --skip-name-resolve mode.
db_1 | 2020-01-06 18:17:44 0 [Note] Reading of all Master_info entries succeeded
db_1 | 2020-01-06 18:17:44 0 [Note] Added new Master_info '' to hash table
db_1 | 2020-01-06 18:17:44 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.4.11-MariaDB-1:10.4.11+maria~bionic' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
db_1 | 2020-01-06 18:17:45 0 [Note] InnoDB: Buffer pool(s) load completed at 200106 18:17:45
depends_on:
- db