Article Directory
1, the virtual machine (cloud server) connected to the docker mysql
A clear concept: the internal network and external network ip ip
Intranet IP: As the name suggests, is the ip address of the network, including the use of what is within the network, computers, for a router in a network, it is also known as local area networks, network ip can be repeated, because the local area network you can have many, of course, out of the local area network, you can not access the internal network ip up.
Public network ip: the ip is powerful, he is the only Internet address, repeat does not happen, the Internet and can be accessed directly to other computers, it is generally as a computer server, we must ensure that this computers have a public ip, so as to better provide services to external users.
See centos7 intranet ip : use ifconfig
or followed by the parameters, can be viewed within the network ip:
Check external network ip: using the curl ifconfig.me
command to view ip outside the network,
Other detailed view windows and linux intranet / extranet ip refer to blog:
https://blog.csdn.net/hmmmmm2929/article/details/81288898
So if you are loaded docker connection mysql in a virtual machine or a cloud server below, the use of a virtual machine (server) ip to connect mysql, using commands ip addr
p query, but this ip query out of the internal network ip, not be ip remote connections should be used outside the network server or virtual machine ip! ! ! Such as a server, that you connect Ali cloud above the external network ip
In this case there are three ip:
Windows主机 IP:192.168.1.229(采用ipconfig查询即可)
Linux 虚拟机 IP:192.168.233.129
Docker容器 IP:172.17.0.2
docker in mysql ip container can be used docker inspect apple_mysql
to facilitate the use of the test, we usually choose to install it in the Windows virtual machine host (where the election is VMware Workstation), deployed Docker containers in a virtual machine. And we deploy in Docker containers in the external network (the same level of network and Windows) is not accessible.
To solve this problem, the following this approach can be used.
Docker can choose own port mapping function can create a container across networks 172.17.0.2
and 192.168.233.129
communications. Command as follows, using other methods not repeated here the docker
docker run -di --name apple_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
-p 代表端口映射,格式为 宿主机映射端口:容器运行端口
-e 代表添加环境变量 MYSQL_ROOT_PASSWORD是root用户的登陆密码
Here we can access it through 192.168.233.129
the 33306
port mapping to 172.17.0.2
the 3306
port, which is the direct access to mysql
this time using the connection tool sqlyog connection (ip linux is a virtual machine ip) or using the command: mysql -u root -p -h192.168.233.129
2, windows down docker desktop software to connect mysql
This time only two ip:
Windows主机 IP:192.168.1.229
Docker容器 IP:172.17.0.2
Because you are using windows docker docker desktop software installed, so this time there is no ip virtual machine, this time connecting mysql, this address is your machine: localhost/127.0.0.1
can also be connected with the upper windows Host IP:192.168.1.229
important point
Note 1:
There are (https://blog.csdn.net/underclound/article/details/77117368) Speaking docker in the window does not use the address 127.0.0.1, but the use of 192.168.99.100 blog, but I can here using 127.0.0.1, rather invalid address connection back!
Note 2:
Here If your mysql mirrored pull the latest 8.0 version, this problem may occur the following:
reason: mysql 8.0 caching_sha2_password default authentication mechanism; the client does not support the new encryption method.
Solution:
modifying user (root) encryption method
steps:
1, into the interior of the container mysql
[root@localhost ~]# docker exec -it mysql01 bash
Enter the following command after entering:
root@8e74f086c2bb:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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.
And you will be prompted to enter the mysql container: enter the corresponding command
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | caching_sha2_password | $A$005$Z%@a}aqP.mjjK<t?SjMyCAGpzJJtUmnYFmTgPoqWOESUfAg9ojwPkd8HzP4 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | $A$005$Gr/wA/
PHFFzz``OkyuNlZo3K6eObfcdOORjQoG3zvTztdSnkIDOgg5ZkmzRb/ |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)
% host is not to limit use of the machine represented ip localhost plugin will need to modify the password non mysql_native_password
Enter the following command:
mysql>ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql>flush privileges;
Query again:
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | $A$005$Gr/wA/
PHFFzz``OkyuNlZo3K6eObfcdOORjQoG3zvTztdSnkIDOgg5ZkmzRb/ |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)
It has been revised to complete, reconnect mysql to success!
Docker for Windows use the configuration instructions
This article has presented very good:
https://blog.csdn.net/novanova2009/article/details/86529949
Reference article:
https://www.cnblogs.com/bobkingblog/p/11070062.html
https://blog.csdn.net/liwenxia626/article/details/80848377
https://blog.csdn.net/qq_40389276/article / details / 98871405