Mysql deployed under docker for windows of windows container

This is mirrored in the windows container mysql deployment mode in win10 environment

Deployment steps are as follows:

1, in the version dockerhub pull winamd64 mysql mirror.

2, the start command

docker run -p 3307:80 -it --name mysqlContainer -d nanoserver/mysql:latest

Note that, my command is starting to map the port is set to 3307 external, internal container 80, please continue to look down.

3, after a successful start mysql container, the container can docker ps see the status
Here Insert Picture Description
next to enter the container

docker exec -it mysqlContainer cmd

Reset the root password of mysql

1、停止MySQL服务 cmd命令:net stop mysql
2、创建一个 mysql_init.txt 文件
加入一句话:ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
echo ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';>mysql_init.txt
引号里面的的信息为账号和密码
3、进入MySQL安装的bin目录 打开cmd控制台
执行:mysqld --init-file=C:/mysql_init.txt --console
console可以在控制台查看运行结果

4, because what we are mysql container, external access to our mysql database, you know ip container

docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" mysqlContainer 

5, with the container ip, you can use the tool to connect a database connection

Because 3307 is the port mapping settings: 80, should look container ip: 3307 to connect to such a database, but the connection port 3307 Rom, which is odd strange, this set of mapping port with no eggs.
Here Insert Picture Description
Use the following command to view the status of the container mysql

docker inspect mysqlContainer

The following segments have
Here Insert Picture Description
the host port is 3307, but 3307 is the Rom, but added 3306 can be connected, but the display is not remotely connected error 1130, because at the time of the start command, and did not add other additional commands.

6, 1130 resolved remote database connection can not be connected
into the container command

docker exec -it mysqlContainer cmd

Then in a container, enter the following commands:

mysql -u root -p
密码:root  
mysql>use mysql;  
mysql>select 'host' from user where user='root';  
mysql>update user set host = '%' where user ='root';  
mysql>flush privileges;  
mysql>select 'host'   from user where user='root';

The first sentence is the root user privileges to log
the second sentence: select mysql database
third sentence: View mysql database user table of host value (can be a host / IP connection to access the name)
fourth sentence: Modify the host value ( wildcard% increase in the content of the host / IP address), of course, can directly increase the IP address of the
fifth sentence: Flush the MySQL system privileges related tables
sixth sentence: when re-view the user list, there are changes. .
Remember Mysql service needs to be restarted (modified to ensure effective), or it may modify the results can not be reflected.

7, then you can normally use the database connectivity tools, and the connection is successful there will be a prompt mysql version, my version is 5.7.22 mysql container, the host is 8.0.18, pay attention not to connect wrong ip

You can mysql container, create a database, whether the test can be used normally.

8, now has a database can be used normally, but a bit puzzled that why I set up port mapping with no eggs?
9, if I add the startup command mysql container in the following

Mounting data volumes and change the root user password, you start being given, after must have the easiest start, reset the password inside the container, but now the volume of data persistence has not been resolved.

docker run -p 80:80 -v C:/Users/cq/Desktop/mysql:C:/mysql --name mysqlContainer -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=root -e MYSQL_PASSWORD=root -d nanoserver/mysql:latest
Published 45 original articles · won praise 1 · views 1075

Guess you like

Origin blog.csdn.net/lqq404270201/article/details/103761515