首页 \ 问答 \ 无法连接到通过docker-compose创建的MySQL docker容器(Can't connect to MySQL docker container created via docker-compose)

无法连接到通过docker-compose创建的MySQL docker容器(Can't connect to MySQL docker container created via docker-compose)

我想开始使用docker并使用nginx容器,PHP-FPM容器和MySQL容器创建一个简单的容器环境。

虽然nginx和PHP-FPM容器之间的链接运行良好,但我似乎无法将PHP应用程序服务器与数据库服务器链接。

我使用docker-compose来最小化手动终端工作。 我的docker-compose.yml看起来像这样:

web:
  image: tutorial/nginx
  ports:
    - "8080:80"
  volumes:
    - ./src:/var/www
    - ./src/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
  links:
    - php
php:
  image: nmcteam/php56
  volumes:
    - ./src/php-fpm.conf:/etc/php5/fpm/php-fpm.conf
    - ./src:/var/www
  links:
    - db
db:
  image: sameersbn/mysql
  volumes:
   - /var/lib/mysql
  environment:
   - DB_NAME=demoDb
   - DB_USER=demoUser
   - DB_PASS=demoPass

我尝试使用以下语句连接到DB:

$db = new \PDO('mysql:host=db;dbname=demoName', 'demoUser', 'demoPass');

MySQL容器本身正在工作,因为我可以连接到容器bash并使用MySQL CLI:

mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| demoDb             |
| mysql              |
| performance_schema |
+--------------------+

我只是得到一个500错误,并找不到为什么这不起作用的原因。 任何对我可能错过的帮助或建议都不胜感激。


I want to get started with docker and created a simple container environment with an nginx container, a PHP-FPM container and a MySQL container.

While the link between the nginx and PHP-FPM container works well I can't seem to link the PHP application server with the database server.

I use docker-compose to minimize the manual terminal work. My docker-compose.yml looks like this:

web:
  image: tutorial/nginx
  ports:
    - "8080:80"
  volumes:
    - ./src:/var/www
    - ./src/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
  links:
    - php
php:
  image: nmcteam/php56
  volumes:
    - ./src/php-fpm.conf:/etc/php5/fpm/php-fpm.conf
    - ./src:/var/www
  links:
    - db
db:
  image: sameersbn/mysql
  volumes:
   - /var/lib/mysql
  environment:
   - DB_NAME=demoDb
   - DB_USER=demoUser
   - DB_PASS=demoPass

While I try to connect to the DB with the following statement:

$db = new \PDO('mysql:host=db;dbname=demoName', 'demoUser', 'demoPass');

The MySQL container itself is working as I can connect to the containers bash and use the MySQL CLI:

mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| demoDb             |
| mysql              |
| performance_schema |
+--------------------+

I just get a 500 error and can't find a reason why this wouldn't work. Any help or suggestion of what I might have missed is more than appreciated.


原文:https://stackoverflow.com/questions/37463543
更新时间:2023-05-06 14:05

最满意答案

缺少分隔符:

preg_match("/^([0-9]{5}|[0-9]{5}\-[0-9]{4})$/", $zip);

Missing delimiters:

preg_match("/^([0-9]{5}|[0-9]{5}\-[0-9]{4})$/", $zip);

相关问答

更多
  • is_numeric()测试一个值是否是一个数字。 虽然它不一定是一个整数 - 它可以是十进制数或科学记数法中的数字。 你给出的preg_match()例子只检查一个值是否包含0到9的数字; 它们中的任意数量,以及任何顺序。 请注意,您给出的正则表达式也不是一个完美的整数检查器,就像你写的那样。 它不允许否定; 它确实允许一个零长度的字符串(即根本没有数字,这大概不应该是有效的?),并且它允许该数字具有任意数量的前导零,这也可能不是预期的。 [编辑] 根据您的评论,更好的正则表达式可能如下所示: /^[1- ...
  • 使用/(*UTF8)^(([0-8]\d|\d)°?(\s?([0-5]\d|\d))?)(N|S)?$/ 来自http://www.pcre.org/pcre.txt : 为了处理UTF-8字符串,您必须构建具有UTF支持的PCRE的8位库,此外,您必须使用PCRE_UTF8选项标志调用pcre_compile(),或者模式必须以序列(* UTF8)或(* UTF)。 在这些情况中的任何一种情况下,模式和与其匹配的任何主题字符串都被视为UTF-8字符串而不是单个1字节字符的字符串。 因此,PCRE引擎仍然 ...
  • 您不需要多次调用preg_match ,只需使用lookahead来强制执行规则,如此正则表达式: ^(?=.*?\d)(?=.*?[a-z])[-\wáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ0\d!@#$%^&*()+{}:"<>?|\[\];',./\x5c~]{6,30}$ RegEx演示 码: $re = "`^(?=.*?\\d)(?=.*?[a-z])[-\\wáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœ ...
  • 您没有分配新值。 尝试这个: $new_array[$results] = $away_goals; 然后你可以使用$new_array来获取数据。 You're not assigning a new value. Try this: $new_array[$results] = $away_goals; Then you could use $new_array to get the data afterwards.
  • 您可以使用此代码: $pattern = <<<'LOD' ~ (?> ### all that you want to skip ### ]*+ > # opening "a" tag (?> [^<]++ | <(?!/a>) )*+ # possible content between "a" tags # closing "a" tag | < ...
  • 缺少分隔符: preg_match("/^([0-9]{5}|[0-9]{5}\-[0-9]{4})$/", $zip); Missing delimiters: preg_match("/^([0-9]{5}|[0-9]{5}\-[0-9]{4})$/", $zip);
  • 你的PHP匹配字符串是 / ^ \ W {5} $ / 并且PHP匹配字符串由/字符包围,这些字符不是RegEx字符串本身的一部分。 根据评论你的问题是关于理解正则表达式,而不是PHP。 ^是行的开头,正确 $是行的结尾,正确 \ w任何单词字符(字母,数字,下划线) a {5,}表示5个或更多字符'a' 因此:如果用户名中有5个或更多单词字符,则该函数返回肯定结果。 甚至更简单:用户名需要包含至少五个任何单词字符。 详细了解正则表达式及其工作原理。 可以在此评论中找到一些解释。 Your PHP matc ...
  • 使用正则表达式更具体,以防止贪婪的表达: preg_match(":vimeo.\w{2,4}/(\d+):i", $url, $vimeoID); Be more specific with your regex to prevent a greedy expression: preg_match(":vimeo.\w{2,4}/(\d+):i", $url, $vimeoID);
  • 最好的解决方案可能是使用像DOM DOM Parser这样的DOM解析器库。 但你可以这样做: preg_match('/name="hosted_button_id"\s*value="(\w+)"/', $string, $match); $button_id = $match[1]; The best solution is probably to use a DOM parser library like Simple DOM Parser. But you can do: preg_match( ...
  • 您在双引号模式中使用美元表达式。 以下为我工作: if(preg_match('/&\$/',trim($lines),$matches)) 检查关于单引号和双引号之间差异的现有问题: PHP中单引号和双引号字符串之间的区别是什么? You are using a dollar expression within the double-quoted pattern. The following works for me: if(preg_match('/&\$/',trim($lines),$matche ...

相关文章

更多

最新问答

更多
  • Firebird客户端安装(Firebird client installation)
  • 如何检查一个文件是否已被C中的另一个进程打开?(How to check if a file is already open by another process in C?)
  • 将对象引用存储在控件标签属性确定中(Is storing an object reference in a controls Tag property OK)
  • 谁能介绍《商务谈判》课程的高职高专教材???谢谢!!!
  • 递归图像下载与请求(recursive image download with requests)
  • C ++对齐字符以便在任何实现的输入中整齐地显示(C++ aligning characters to display neatly with any input implemented)
  • 根据字段值插入一行(Insert a row, based on a fields value)
  • 在Ubuntu上使用TCP_REPAIR套接字选项编译代码[关闭](Compiling code with TCP_REPAIR socket option on Ubuntu [closed])
  • 在开发React应用程序时编译/转换代码(Compile/transpile code while developing React app)
  • 重庆软件开发培训 Java培训哪好
  • 将MultiPoint序列化为GeoJSON文件(Serialize MultiPoint to GeoJSON file)
  • 将文本从多个文件,相同的名称复制到bash(linux)中的不同路径(Copy text from multiple files, same names to different path in bash (linux))
  • 将字符串截断为特定数量的字符,忽略HTML(Truncate string to certain amount of characters, ignoring HTML)
  • 如何为崩溃的JNI调用编写JUnit测试?(How can I write a JUnit test for a JNI call that crashes?)
  • 多点触摸两个手指轻拍(Multi-touch two fingers taps)
  • Sqlserver监视时间的变化(Sqlserver watch for time change)
  • Srcset属性 - 最大宽度问题(Srcset attribute - max-width issue)
  • 如何验证数据库中是否存在记录?(How to verify existence of a record in database?)
  • SQL JOIN来自不同表的行具有相同的值(SQL JOIN row from different table with the same values)
  • NSTextField - 使用KVO进行输入验证?(NSTextField - Input validation with KVO?)
  • 如何使用DBPedia从根类别中提取不同级别的子类别?(How to extract sub-categories of different levels from a root category using DBPedia?)
  • 在Javascript中,如何检查数组是否有重复值?(In Javascript, how do I check if an array has duplicate values? [duplicate])
  • 有什么区别:焦点:必需:无效:焦点和:焦点:必需:无效?(What's the difference between :focus:required:invalid:focus and :focus:required:invalid?)
  • 使用OData创建新数据(Creating new data with OData)
  • 获取过滤器从spark数据帧中删除的行的示例(Get examples for rows that are removed by a filter from a spark dataframe)
  • 使用@RequestMapping编码到Spring Controller方法的接口(Coding to an interface on a Spring Controller method with @RequestMapping)
  • 如果Shape在屏幕外,是否执行Graphics2D.draw?(Is Graphics2D.draw performed if the Shape is offscreen?)
  • 与ajax的成功(success with ajax)
  • 如何获取在Javascript中单击的文本?(How can I get the text that was clicked on in Javascript?)
  • 如果没有头文件,如何定义静态成员?(How to define a static member in case there is not header file?)