MariaDB连接被拒绝(NGINX,MariaDB,php-fpm)SQLSTATE[HY000] [2002]连接被拒绝

huangapple go评论96阅读模式
英文:

MariaDB Connection refused (NGINX , MariaDB, php-fpm) SQLSTATE[HY000] [2002] Connection refused

问题

这是你的文档中的一部分内容:

"Getting SQLSTATE[HY000] [2002] Connection refused when sending query to MariaDB"
在发送查询到MariaDB时出现SQLSTATE[HY000] [2002]连接被拒绝的错误。

以下是你的docker-compose构建的链接:
https://github.com/bocasoftware/docker-compose

这是data.sql文件的链接(不在GitHub存储库中,因为文件太大,但它在我的构建中存在):
https://gitlab.com/x-team-blog/docker-compose-php/blob/master/docker/database/data.sql

这是你的文档树/布局:

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文件中的查询部分的代码:

  1. <?php
  2. $value = "World";
  3. try {
  4. $db = new PDO('mysql:host=db;dbname=mydb;charset=utf8mb4', 'myuser', 'secret');
  5. $databaseTest = ($db->query('SELECT * FROM dockerSample'))->fetchAll(PDO::FETCH_OBJ);
  6. } catch (Exception $e) {
  7. print $e->getMessage() . "\n";
  8. }
  9. print "OK\n";
  10. ?>

这是你的docker-compose.yml文件的一部分:

  1. version: '3'
  2. services:
  3. app:
  4. build:
  5. context: ./app
  6. volumes:
  7. - ../src:/var/www
  8. nginx:
  9. build:
  10. context: ./nginx
  11. volumes:
  12. - ../src:/var/www
  13. - ./nginx/nginx.conf:/etc/nginx/nginx.conf
  14. - ./nginx/sites/:/etc/nginx/sites-available
  15. - ./nginx/conf.d/:/etc/nginx/conf.d
  16. depends_on:
  17. - app
  18. ports:
  19. - "80:80"
  20. - "443:443"
  21. db:
  22. build:
  23. context: ./db
  24. ports:
  25. - "3306:3306"
  26. environment:
  27. - MYSQL_DATABASE=mydb
  28. - MYSQL_USER=myuser
  29. - MYSQL_PASSWORD=secret
  30. - MYSQL_ROOT_PASSWORD=docker
  31. volumes:
  32. - ./db/data.sql:/docker-entrypoint-initdb.d/data.sql

这是你的Docker容器的状态,使用docker ps命令查看:

  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 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
  3. d84c3b348266 docker_app "docker-php-entrypoi…" About an hour ago Up 13 minutes 9000/tcp docker_app_1
  4. 5ba1e7d36f91 docker_db "docker-entrypoint.s…" About an hour ago Up 13 minutes 3306/tcp docker_db_1

这是你构建的Docker容器的日志信息。

英文:

Getting SQLSTATE[HY000] [2002] Connection refused when sending query to MariaDB

Below is the link to my docker-compose build

https://github.com/bocasoftware/docker-compose

--- IMPORTANT This is the data.sql file (NOT in the github repository file was too large but it is on my build):

https://gitlab.com/x-team-blog/docker-compose-php/blob/master/docker/database/data.sql

This is my document tree/layout:

  1. myapp
  2. ├── docker
  3. │&#160;&#160; ├── app
  4. │&#160;&#160; │&#160;&#160; └── Dockerfile
  5. │&#160;&#160; ├── db
  6. │&#160;&#160; │&#160;&#160; ├── data.sql
  7. │&#160;&#160; │&#160;&#160; └── Dockerfile
  8. │&#160;&#160; ├── docker-compose.yml
  9. │&#160;&#160; └── nginx
  10. │&#160;&#160; ├── conf.d
  11. │&#160;&#160; │&#160;&#160; └── default.conf
  12. │&#160;&#160; ├── Dockerfile
  13. │&#160;&#160; ├── nginx.conf
  14. │&#160;&#160; └── sites
  15. │&#160;&#160; └── default.conf
  16. └── src
  17. ├── html
  18. └── index.php

Here is my index.php which has the query:

  1. &lt;?php
  2. $value = &quot;World&quot;;
  3. try {
  4. $db = new PDO(&#39;mysql:host=db;dbname=mydb;charset=utf8mb4&#39;, &#39;myuser&#39;, &#39;secret&#39;);
  5. $databaseTest = ($db-&gt;query(&#39;SELECT * FROM dockerSample&#39;))-
  6. &gt;fetchAll(PDO::FETCH_OBJ);
  7. } catch (Exception $e) {
  8. print $e-&gt;getMessage() . &quot;\n&quot;;
  9. }
  10. print &quot;OK\n&quot;;
  11. ?&gt;
  12. &lt;html&gt;
  13. &lt;body&gt;
  14. &lt;h1&gt;Hello, &lt;?= $value ?&gt;!&lt;/h1&gt;
  15. &lt;?php foreach($databaseTest as $row): ?&gt;
  16. &lt;p&gt;Hello, &lt;?= $row-&gt;name ?&gt;&lt;/p&gt;
  17. &lt;?php endforeach; ?&gt;
  18. &lt;/body&gt;
  19. &lt;/html&gt;

Here is my docker-compose.yml:

version: '3'

  1. services:
  2. app:
  3. build:
  4. context: ./app
  5. volumes:
  6. - ../src:/var/www
  7. nginx:
  8. build:
  9. context: ./nginx
  10. volumes:
  11. - ../src:/var/www
  12. - ./nginx/nginx.conf:/etc/nginx/nginx.conf
  13. - ./nginx/sites/:/etc/nginx/sites-available
  14. - ./nginx/conf.d/:/etc/nginx/conf.d
  15. depends_on:
  16. - app
  17. ports:
  18. - &quot;80:80&quot;
  19. - &quot;443:443&quot;
  20. db:
  21. build:
  22. context: ./db
  23. ports:
  24. - &quot;3306:3306&quot;
  25. environment:
  26. - MYSQL_DATABASE=mydb
  27. - MYSQL_USER=myuser
  28. - MYSQL_PASSWORD=secret
  29. - MYSQL_ROOT_PASSWORD=docker
  30. volumes:
  31. - ./db/data.sql:/docker-entrypoint-initdb.d/data.sql

I do not understand what is going on. It seems I am reaching MariaDB but it's refusing my connection despite checking ports, user and password and pruning volumes, removing containers, deleting images and building them again from scratch.

docker ps:

  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 23ae6d48e413 docker_nginx &quot;nginx&quot; About an hour ago Up 13 minutes 0.0.0.0:80-&gt;80/tcp, 0.0.0.0:443-&gt;443/tcp, 8080/tcp docker_nginx_1
  3. d84c3b348266 docker_app &quot;docker-php-entrypoi…&quot; About an hour ago Up 13 minutes 9000/tcp docker_app_1
  4. 5ba1e7d36f91 docker_db &quot;docker-entrypoint.s…&quot; About an hour ago Up 13 minutes 3306/tcp docker_db_1

Build logs:

  1. Building db
  2. Step 1/3 : FROM mariadb:latest
  3. ---&gt; 2f11cf2ec189
  4. Step 2/3 : CMD [&quot;mysqld&quot;]
  5. ---&gt; Using cache
  6. ---&gt; 9f5dd0c62210
  7. Step 3/3 : EXPOSE 3306
  8. ---&gt; Using cache
  9. ---&gt; fe82aa37cc8d
  10. Successfully built fe82aa37cc8d
  11. Successfully tagged docker_db:latest
  12. Building app
  13. Step 1/4 : FROM php:fpm-alpine
  14. ---&gt; aac18a94faf7
  15. Step 2/4 : RUN docker-php-ext-install pdo_mysql
  16. ---&gt; Using cache
  17. ---&gt; 886e00ce8edc
  18. Step 3/4 : CMD [&quot;php-fpm&quot;]
  19. ---&gt; Using cache
  20. ---&gt; 90bf05e46cc5
  21. Step 4/4 : EXPOSE 9000
  22. ---&gt; Using cache
  23. ---&gt; 2e9cb9be36b7
  24. Successfully built 2e9cb9be36b7
  25. Successfully tagged docker_app:latest
  26. Building nginx
  27. Step 1/3 : FROM nginx:alpine
  28. ---&gt; a624d888d69f
  29. Step 2/3 : CMD [&quot;nginx&quot;]
  30. ---&gt; Using cache
  31. ---&gt; 767651bd1614
  32. Step 3/3 : EXPOSE 8080 443
  33. ---&gt; Using cache
  34. ---&gt; 6fc1df24d4e4
  35. Successfully built 6fc1df24d4e4
  36. Successfully tagged docker_nginx:latest
  37. Starting docker_db_1 ...
  38. Starting docker_db_1 ... done
  39. Starting docker_app_1 ...
  40. Starting docker_app_1 ... done
  41. Starting docker_nginx_1 ...
  42. Starting docker_nginx_1 ... done
  43. Attaching to docker_db_1, docker_app_1, docker_nginx_1
  44. db_1 | 2020-01-06 18:17:42+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.11+maria~bionic started.
  45. app_1 | [06-Jan-2020 18:17:44] NOTICE: fpm is running, pid 1
  46. db_1 | 2020-01-06 18:17:43+00:00 [Note] [Entrypoint]: Switching to dedicated user &#39;mysql&#39;
  47. db_1 | 2020-01-06 18:17:43+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.11+maria~bionic started.
  48. app_1 | [06-Jan-2020 18:17:44] NOTICE: ready to handle connections
  49. 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 ...
  50. db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Using Linux native AIO
  51. db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
  52. db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Uses event mutexes
  53. db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
  54. db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Number of pools: 1
  55. db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Using SSE2 crc32 instructions
  56. db_1 | 2020-01-06 18:17:43 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
  57. db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
  58. db_1 | 2020-01-06 18:17:43 0 [Note] InnoDB: Completed initialization of buffer pool
  59. 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().
  60. db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
  61. db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Creating shared tablespace for temporary tables
  62. db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Setting file &#39;./ibtmp1&#39; size to 12 MB. Physically writing the file full; Please wait ...
  63. db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: File &#39;./ibtmp1&#39; size is now 12 MB.
  64. db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: 10.4.11 started; log sequence number 6837896; transaction id 7512
  65. db_1 | 2020-01-06 18:17:44 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
  66. db_1 | 2020-01-06 18:17:44 0 [Note] Plugin &#39;FEEDBACK&#39; is disabled.
  67. db_1 | 2020-01-06 18:17:44 0 [Note] Server socket created on IP: &#39;::&#39;.
  68. db_1 | 2020-01-06 18:17:44 0 [Warning] &#39;user&#39; entry &#39;root@5ba1e7d36f91&#39; ignored in --skip-name-resolve mode.
  69. db_1 | 2020-01-06 18:17:44 0 [Warning] &#39;user&#39; entry &#39;@5ba1e7d36f91&#39; ignored in --skip-name-resolve mode.
  70. db_1 | 2020-01-06 18:17:44 0 [Warning] &#39;proxies_priv&#39; entry &#39;@% root@5ba1e7d36f91&#39; ignored in --skip-name-resolve mode.
  71. db_1 | 2020-01-06 18:17:44 0 [Note] Reading of all Master_info entries succeeded
  72. db_1 | 2020-01-06 18:17:44 0 [Note] Added new Master_info &#39;&#39; to hash table
  73. db_1 | 2020-01-06 18:17:44 0 [Note] mysqld: ready for connections.
  74. db_1 | Version: &#39;10.4.11-MariaDB-1:10.4.11+maria~bionic&#39; socket: &#39;/var/run/mysqld/mysqld.sock&#39; port: 3306 mariadb.org binary distribution
  75. db_1 | 2020-01-06 18:17:45 0 [Note] InnoDB: Buffer pool(s) load completed at 200106 18:17:45

答案1

得分: 1

add

  1. depends_on:
  2. - db

for app

英文:

add

  1. depends_on:
  2. - db

for app

huangapple
  • 本文由 发表于 2020年1月7日 00:13:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定