无法连接到通过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);

相关问答

更多

PHP is_numeric或preg_match 0-9验证(PHP is_numeric or preg_match 0-9 validation)

is_numeric()测试一个值是否是一个数字。 虽然它不一定是一个整数 - 它可以是十进制数或科学记数法中的数字。 你给出的preg_match()例子只检查一个值是否包含0到9的数字; 它们中的任意数量,以及任何顺序。 请注意,您给出的正则表达式也不是一个完美的整数检查器,就像你写的那样。 它不允许否定; 它确实允许一个零长度的字符串(即根本没有数字,这大概不应该是有效的?),并且它允许该数字具有任意数量的前导零,这也可能不是预期的。 [编辑] 根据您的评论,更好的正则表达式可能如下所示: ...

PHP preg_match()PCRE逻辑问题?(PHP preg_match() PCRE logic issue?)

使用/(*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字节字符的字符串。 因此,PCR...

php:preg_match因特殊字符模式而失败(php: preg_match failed with special character pattern)

您不需要多次调用preg_match ,只需使用lookahead来强制执行规则,如此正则表达式: ^(?=.*?\d)(?=.*?[a-z])[-\wáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ0\d!@#$%^&*()+{}:"<>?|\[\];',./\x5c~]{6,30}$ RegEx演示 码: $re = "`^(?=.*?\\d)(?=.*?[a-z])[-\\wáàâäãåçéèêëíìîïñóòôöõúùû...

使用preg_match php [关闭]将数据输入到单独的索引数组中(Inputting data into separate index array using preg_match php [closed])

您没有分配新值。 尝试这个: $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.

preg_match需要帮助5 [关闭](preg_match assistance needed 5 [closed])

您可以使用此代码: $pattern = <<<'LOD' ~ (?> ### all that you want to skip ### <a\b [^>]*+ > # opening "a" tag (?> [^<]++ | <(?!/a>) )*+ # possible content between "a" tags </a> # closing "a" tag | ...

在PHP中遇到preg_match()问题[关闭](Having an issue with preg_match() in PHP [closed])

缺少分隔符: 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 - preg_match解释[关闭](PHP - preg_match explanation [closed])

你的PHP匹配字符串是 / ^ \ W {5} $ / 并且PHP匹配字符串由/字符包围,这些字符不是RegEx字符串本身的一部分。 根据评论你的问题是关于理解正则表达式,而不是PHP。 ^是行的开头,正确 $是行的结尾,正确 \ w任何单词字符(字母,数字,下划线) a {5,}表示5个或更多字符'a' 因此:如果用户名中有5个或更多单词字符,则该函数返回肯定结果。 甚至更简单:用户名需要包含至少五个任何单词字符。 详细了解正则表达式及其工作原理。 可以在此评论中找到一些解释。 Your PHP...

为什么PHP preg_match返回空?(Why is the PHP preg_match returning empty? [closed])

使用正则表达式更具体,以防止贪婪的表达: 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);

PHP复杂的preg_match(PHP Complicated preg_match)

最好的解决方案可能是使用像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_m...

用于“&$”的PHP中的preg_match(preg_match in PHP for “&$”)

您在双引号模式中使用美元表达式。 以下为我工作: 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),$m...

相关文章

更多

Docker 容器创建并运行MySQL

下载MySQL镜像:docker pull centos/mysql‐57‐centos7(docke ...

揭开Docker神秘的面纱-传智播客Docker视频教程

Docker是基于LXC的虚拟化技术,它为IT行业中的运维人员和开发人员都带来了非常大的帮助,对于运维 ...

MySQL最佳实践

MySQL是我们最常见且常用的关系型数据库之一,其相关的优化和最佳实践的tips,网上早已泛滥了,但其 ...

my php & mysql FAQ

php中文字符串长度及定长截取问题使用str_len(&quot;中国&quot;) 结果为6,php ...

mysql问题

利用PreparedStatement执行插入,更新的操作时候: 如: PreparedState ...

shell操作mysql初步

shell操作mysql的两种方式,方式一:echo "${SQL}" | ${CONNECT_MYS ...

MapReduce直接连接MySQL获取数据

MySQL中数据: mysql&gt; select * from linuxidc_tbls; +- ...

MySQL 5.6 my.cnf 配置文件详解

MySQL 5.6 my.cnf 配置文件详解,很详细,可以直接复制使用 [client]port ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本