首页 \ 问答 \ 无法连接到通过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 ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)