描述 图片
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内容
<?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 /]#
容器之间是相互隔离的,你需要用 --link 参数将两个容器连接起来,
并且连接的时候直接使用容器名就能访问对应的容器,使用 ip 在某些场景是有问题的,应避免使用