2019独角兽企业重金招聘Python工程师标准>>>
1. 打开 PowerShell
2.搜索mysql镜像
docker search mysql
3.下载mysql镜像(默认最新版本)
docker pull mysql
4. 运行镜像,设置root账号初始密码(system123456),映射本地宿主机端口3310到Docker端口3306
不挂载本地磁盘(重启后数据会丢失)
docker run -it --rm --name mysql8.0 -e MYSQL_ROOT_PASSWORD=system123456 -p 3310:3306 -d mysql
挂载本地磁盘(不随系统自动启动)
docker run -it --rm -v D:/docker/mysql/data:/var/lib/mysql -v D:/docker/mysql/config/my.cnf:/etc/mysql/my.cnf --name mysql8.0 -e MYSQL_ROOT_PASSWORD=system123456 -p 3310:3306 -d mysql
挂载本地磁盘(随系统自动启动)
docker run -it -v D:/docker/mysql/data:/var/lib/mysql -v D:/docker/mysql/config/my.cnf:/etc/mysql/my.cnf --restart=always --name mysql8.0 -e MYSQL_ROOT_PASSWORD=system123456 -p 3310:3306 -d mysql
注意:挂载本地磁盘时本地路径需要手动创建;本地配置文件不能为空,需要添加必要的配置信息比如如下配置:
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#服务端使用的字符集默认为8比特编码的latin1字符集
character_set_server = utf8
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#设置不区分大小写
# 必须在安装好MySQL后 修改mySQL配置文件设置为不敏感,一旦启动后,再设置是无效的,而且启动报错;
# 如果已经晚了,那必须把MySQL数据库文件全部 删除,修改配置文件再启动。
lower_case_table_names=1
5.查看已运行的容器
docker ps -a
6.进入mysql容器
docker exec -it mysql8.0 bash
7.在容器内登陆Mysql
mysql -u root -p
8.查看用户信息
select host,user,plugin,authentication_string from mysql.user;
备注:host为 % 表示不限制ip localhost表示本机使用 plugin非mysql_native_password 则需要修改密码
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'system123456';
FLUSH PRIVILEGES;
9.查看用户信息
10.退出mysql
exit;
12.常用操作
| 功能 | 指令 | | 查看本机所安装的容器 | docker images | | 查看正在运行的容器 | docker ps -a | | 删除所有的容器 | docker rm docker ps -a -q
| | 停止、启动、杀死一个容器 | docker stop Name/ID
docker start Name/ID
docker kill Name/ID | | 交互式进入容器中 | docker run -i -t image_name /bin/bash | | 在容器中安装新的程序 | docker run image_name apt-get install -y app_name | |
13. 常用配置
Docker Hub 会比较慢 我们可能需要使用国内代理
阿里云的镜像一直很给力 https://dev.aliyun.com/search.html
可以在阿里云的网站上获取到加速地址 我这里获取的是
https://j0andt2p.mirror.aliyuncs.com