目标:在docker里安装mysql5.7并且可以通过工具访问
安装mysql:
查询:
docker search mysql
下载(卡的直接停了再重新下,事实证明我卡了两天都没下好,Ctrl+C 停了再下30s):
docker pull mysql
指定版本下载命令:
docker pull mysql:5.7
查看镜像:
docker image ls
运行mysql
docker run -p 3307:3306 --name mysql5.7 -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:5.7
参数:
-p 3307:3306 将主机3307端口映射到容器3306端口
-e MYSQL_ROOT_PASSWORD=abc123 设置远程登录的root用户密码为abc123
--name 可选,设置容器别名
mysql 镜像名称
docker run -p 3307:3306 --name mysql5.7 -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:5.7 --character-set-server=utf8 --collation-server=utf8_unicode_ci
指定编码集
查看运行结果:
然后就跟本地安装一样,设置远程登录:
进入容器:
docker exec -it 39dfe9a9c741 bash
39dfe9a9c741是容器的短id
全部的命令行:
root@39dfe9a9c741:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> select host,user,plugin,authentication_string from mysql.user;
+-----------+---------------+-----------------------+-------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+---------------+-----------------------+-------------------------------------------+
| localhost | root | mysql_native_password | *A4B6157319038724E3560894F7F932C8886EBFCF |
| localhost | mysql.session | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| % | root | mysql_native_password | *A4B6157319038724E3560894F7F932C8886EBFCF |
+-----------+---------------+-----------------------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit;
Bye
root@39dfe9a9c741:/# exit
exit
提取:
1:mysql -uroot -p //登录mysql
2:select host,user,plugin,authentication_string from mysql.user;//查询权限
3:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;//更新登录权限
4:ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';//设置密码
5:flush privileges;//刷新权限
6:quit;//退出mysql
7:exit//退出容器
设置密码有版本区分,具体可自行百度
都设置完毕后,重启容器。
docker restart 39dfe9a9c741
因为我是win7,用docker的话必须是虚拟机,所以查询docker的ip:
docker-machine ip default
用Navicat连接一下: