PHP代码无法获得容器信息,Dokcer 容器中php-fpm无法链接容器中mysql

描述 图片

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

365ceb8a7a6e mysql/mysql-server:5.6 "/entrypoint.sh my..." About an hour ago Up 34 seconds (healthy) 0.0.0.0:3306->3306/tcp gm-mysql-server-5.6

9708e4a75af1 gaoming13/php:7.1.9-fpm "docker-php-entryp..." 3 hours ago Up 26 seconds 0.0.0.0:9000->9000/tcp gm-php-7.1.9-fpm

本机mac宿机 ip 172.17.0.1

mysql容器 ip 172.17.0.2

php容器 ip 172.17.0.3

mysql容器构建命令

docker run \

--name gm-mysql-server-5.6 \

-v /Users/zhaoliming/gaoming13/mysql-server-5.6/my.cnf:/etc/my.cnf \

-v /Users/zhaoliming/gaoming13/mysql-server-5.6/mysqld.log:/var/log/mysqld.log \

-p 3306:3306 \

-e MYSQL_ROOT_PASSWORD=67839888 \

-d \

mysql/mysql-server:5.6

php容器构建命令

docker run \

--name gm-nginx-1.9.9 \

-v /Users/zhaoliming/gaoming13/nginx-1.9.9/html:/usr/share/nginx/html \

-v /Users/zhaoliming/gaoming13/nginx-1.9.9/log:/var/log/nginx \

-v /Users/zhaoliming/gaoming13/nginx-1.9.9/conf.d:/etc/nginx/conf.d:ro \

-v /Users/zhaoliming/gaoming13/nginx-1.9.9/nginx.conf:/etc/nginx/nginx.conf:ro \

-p 80:80 \

-d \

nginx:1.9.9

1.php内容

$mysqli = new mysqli('172.17.0.1', 'root', '6783988');

if (mysqli_connect_error()) {

echo mysqli_connect_error();

}

//设置编码

$mysqli->set_charset("utf8");//或者 $mysqli->query("set names 'utf8'")

//关闭连接

$mysqli->close();

exit();

本机mac连接mysql 127.0.0.1,可以连接成功

$ mysql -h 127.0.0.1 -u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 10

Server version: 5.6.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

本机mac连接mysql,连接不成功

gaoming13-mac:localhost zhaoliming$ mysql -h 172.17.0.1 -u root -p

Enter password:

一直未响应

php容器连接mysql,连接不成功

$ docker exec -it gm-php-7.1.9-fpm php /var/www/html/localhost/1.php

PHP Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0

Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'172.17.0.1' (using password: YES) in /var/www/html/localhost/1.php on line 2

Call Stack:

0.0006 362872 1. {main}() /var/www/html/localhost/1.php:0

0.0007 363256 2. mysqli->__construct() /var/www/html/localhost/1.php:2

探索解决过程

怀疑是mysql远程连接没有打开,就打开mysql远程链接试试

mysql> select * from user;

+-----------+---------------+-------------------------------------------

| Host | User | Password

+-----------+---------------+-------------------------------------------

| % | root | *5648C6F06A0EDFE16052FCAC7078E0A2B630AED7

| localhost | healthchecker | *36C82179AFA394C4B9655005DD2E482D30A4BDF7

+-----------+---------------+-------------------------------------------

2 rows in set (0.00 sec)

my.cnf

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]

#

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

# innodb_buffer_pool_size = 128M

#

# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

# log_bin

#

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

# join_buffer_size = 128M

# sort_buffer_size = 2M

# read_rnd_buffer_size = 2M

skip-host-cache

skip-name-resolve

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

secure-file-priv=/var/lib/mysql-files

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

bind-address=0.0.0.0

结果,输入密码后卡死不动:

$ mysql -h 172.17.0.2 -u root -p

Enter password:

^C

$ mysql -h 172.17.0.1 -u root -p

Enter password:

^C

奇怪,又弄了个centos7容器,ip 172.17.0.4

试着在centos7容器内连接mysql,竟然都可以连接成功

似乎是我的php容器有问题

[root@fabce272f9ca /]# mysql -h 172.17.0.1 -u root -p

Enter password:

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MySQL connection id is 45

Server version: 5.6.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> exit;

Bye

[root@fabce272f9ca /]# mysql -h 172.17.0.2 -u root -p

Enter password:

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MySQL connection id is 46

Server version: 5.6.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> exit;

Bye

[root@fabce272f9ca /]# ifconfig | grep 172

inet 172.17.0.4 netmask 255.255.0.0 broadcast 0.0.0.0

[root@fabce272f9ca /]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值